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

Embed the file name into the field

Explorer ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

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

Views

209

Translate

Translate

Report

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

correct answers 1 Correct answer

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;

 

 

Votes

Translate

Translate
Explorer ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

You can use a script at document open.

Votes

Translate

Translate

Report

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
Explorer ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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