Skip to main content
Inspiring
April 3, 2024
Answered

Embed the file name into the field

  • April 3, 2024
  • 2 replies
  • 1308 views

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.

This topic has been closed for replies.
Correct answer Thom Parker

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.


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;

 

 

2 replies

Bernd Alheit
Community Expert
April 3, 2024

You can use a script at document open.

Thom Parker
Community Expert
April 3, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
KaxkulAuthor
Inspiring
April 3, 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;

 

 


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

KaxkulAuthor
Inspiring
April 3, 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