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

Button to open new PDF and populate fields

Explorer ,
Jun 22, 2020 Jun 22, 2020

I am guessing this is a 2 part question/answer:

 

1) I am looking to use a button to open a new pdf fillable form

2) When it opens that new form I want it to take the info on the form open and popluate certain fields

 

Is this possible?

TOPICS
Acrobat SDK and JavaScript
1.7K
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 ,
Jun 22, 2020 Jun 22, 2020

It's possible but there are better and worse ways of accomplishing this. Will the second form always be known to you? In other words are the two forms always associated with each other?

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
Explorer ,
Jun 22, 2020 Jun 22, 2020

Yes both forms will always be used.  One is a quote form than when it needs to be updated to an other I wouild like the user to use the button to create an invoice that would auto populate with the needed fields.

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 ,
Jun 22, 2020 Jun 22, 2020

Ok - Then you want to make the invoice template an attachment to the quote form. This will let you open it programmatically without a user being prompted to find the file on their hard drive and won't require a privileged context.  The code below will give you a handle to the invoice document.

var invoiceDoc = this.openDataObject("invoiceTemplate.pdf");

You will need to add some code at the document level of the invoiceTemplate to disclose it to other documents so you can send it JavaScript.

this.disclosed = true;

 From there it's just a matter of setting the field values.

invoiceDoc.getField("myField").value = this.getField("myField").value

 Then you'll want to add some code to save the file.

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
Explorer ,
Jun 22, 2020 Jun 22, 2020

Thanks for your help!  I am very much a beginner so sorry for all the questions.  I am trying the first part.  I put the code you listed into the Document JavaScript, I add the Invoice tab and add the script changing the pdf name to what I have it saved as.  I than went into the documnet action and added the second script you put in.  Nothing happened.  I am very much a beginner when it comes to coding so sorry for the questions

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 ,
Jun 22, 2020 Jun 22, 2020

Check the JS Console (Ctrl+J) for errors.

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 ,
Jun 22, 2020 Jun 22, 2020

You won't be able to use the code I entered verbatim except for the disclosed line. Those are examples.

 

I suggest you go here and read a bit before attempting to script two forms to talk to each other. It's not complicated but it's not trivial either.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

This is the error I am getting:


TypeError: Invalid argument type.
Doc.openDataObject:1:Document-Level:Invoice
===> Parameter cName.

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 ,
Jun 23, 2020 Jun 23, 2020

That means you specified the wrong data-object name.

To get the correct name run this code from the JS Console:

this.dataObjects[0].name

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

Got it thanks!...now on to step 2

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

Joel,  So for the second part transferring data from one pdf to the other.  Am i putting the below script in the original pdf (one I am exporting the data from) or the new pdf (one I want teh data imported to)?

invoiceDoc.getfield("myField").value = this.getField ("myField).value

 

invoicedoc (is that the attachemnt name?  "myfield" i assume is the text field name i want to transfer over.  Do I put this code right in the Action Mouse Down insert Java Script field?

 

Thanks in advance

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 ,
Jun 23, 2020 Jun 23, 2020

When working with multiple documents it's not a good idea to use the "this" keyword, as you can't be certain to which one it points. Use a separate variable to refer to the original doc, like this:

var originalDoc = this;

And later on:

invoiceDoc.getField("myField").value = originalDoc.getField("myField").value;

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 ,
Jun 23, 2020 Jun 23, 2020

And invoiceDoc is the name of the variable that's pointing to the Document object you opened using openDataObject.

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 ,
Jun 23, 2020 Jun 23, 2020

The code to push the field values to the second form goes in the original. You'd add it right after the code that opens the attachment. If you want to be very clear about it in your code, prior to opening the attachment, add the line...

var sourceDoc = this;
invoiceDoc.getfield("myField").value = sourceDoc.getField("myField).value

 That way regardless of which document is "on top" you'll always be referencing the source and the target.

Replace "myField" with the names of each field you want to push or use a loop to go through all the fields.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

I am getting an error:

 

ReferenceError: NewInvoiceFormpdf is not defined
3:Field:Mouse Up

 

Any thoughts?

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 ,
Jun 23, 2020 Jun 23, 2020

You need to post your full code.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

var NewQuoteFormpdf = this;

NewInvoiceFormpdf.getField("Bill to Name").value = NewQuoteFormpdf.getField("Bill to Name").value;

 

I was just trying the first part before I keep going...

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 ,
Jun 23, 2020 Jun 23, 2020

Where's the code where you open the attached file?

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

this.exportDataObject({cName: "New Invoice Form.pdf", nLaunch: 2});

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 ,
Jun 23, 2020 Jun 23, 2020

It was not in the code you posted. Please post your FULL code.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

See below screen shot:

RHathaway_1-1592937105698.pngexpand image

 

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 ,
Jun 23, 2020 Jun 23, 2020

exporting the attachment doesn't return a variable. Use my original code to open the attachment so you can reference it by the variable.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

When I use your script the pdf does not open, see below

RHathaway_0-1592938031507.pngexpand image

 

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 ,
Jun 23, 2020 Jun 23, 2020

That's OK - You don't need to see it to populate the data. You only want to use Export at the end of the process.

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

So that is what I am using and it does not open up the new pdf (New Invoice Form).  Not sure if it would populate it or not... I get the below:

RHathaway_0-1592939594901.pngexpand image

 

Thanks again for your help

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