Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
6

Embed the file name into the field

Participant ,
Apr 03, 2024 Apr 03, 2024

Hi everyone,

I have a question that may seem trivial, but I'm wondering if it's possible to synchronize the file name with a field within a PDF form. For example, if the file name changes, could it automatically update the assigned field within the form? If this is possible, any guidance would be greatly appreciated.

Thanks a lot.

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
956
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Apr 03, 2024 Apr 03, 2024

Except that the field isn't update when the change is made. Good point about the dirty flag. 

Here's a redo of the script:

this.getField("FileNameField").value = this.documentFileName;

this.dirty = false;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 03, 2024 Apr 03, 2024

I tried to apply the following:

 

function setFieldValueOnOpen() {
var fileName = this.path.replace(/^.*[\\\/]/, '');

this.getField("FileNameField").value = fileName;

this.calculateNow();
}this.setAction("WillSave", "setFieldValueOnOpen()");

 

But still, the field doesn't update automatically when the file is opened

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2024 Apr 03, 2024

You can use a script at document open.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 03, 2024 Apr 03, 2024

Thank you @Bernd Alheit I tried this but didn't work either, not sure what I'm doing wrong?


function setFieldValueOnOpen() {
var fileName = this.path.replace(/^.*[\\\/]/, '');

this.getField("FileNameField").value = fileName;

this.calculateNow();
}

setFieldValueOnOpen();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2024 Apr 03, 2024

Do not use the script you posted. The only script needed is this one:

 

this.getField("FileNameField").value = this.documentFileName;

 

The "WillSave" script doesn't work because it is called before the name is changed. It is the "DidSave" script that should be used.  Just enter the line of code above into the "DidSave" document action. 

 

As Bernd states, the script should also be in a document script, which is run when the PDF is opened.

 

An alternate solution is to use the calcuation script on the "FileNameField" text field. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2024 Apr 03, 2024

But using the Did Save event will make the file "dirty", which will mean the user will need to save it again, and so on and so on. I think doing it when the file is opened is a better option.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2024 Apr 03, 2024

Except that the field isn't update when the change is made. Good point about the dirty flag. 

Here's a redo of the script:

this.getField("FileNameField").value = this.documentFileName;

this.dirty = false;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 03, 2024 Apr 03, 2024
LATEST

Thank you so much all of you, really appreciat your help so far 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines