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

Pulling JSON data from a third party web API to fill PDF's form fields

New Here ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

I have an active Web API that I want to refernce in my script within Adobe Acrobat so that I can pull data from and populate the form fields in my document. I have read around and saw similar questions on these forums but nothing that matched my need or even worked for me. Any help in this area? Thanks in advance.

TOPICS
How to , JavaScript , PDF forms

Views

3.2K

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 ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

It's possible but non-trivial. How much JavaScript experience do you have?

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
New Here ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

Thanks for your response. I am fairly confident in my JS abilities. I actually just got this code working which I saw parts of referenced somewhere else on this site and managed to stitch together to fit my need. However, it only works when run in the console, but I am attempting to add this to 'Run a Javascript' under the button option so when a user enters a specific Case Number it will pull from the API which sends back data in a JSON object and I can go forward with auto populating the other form fiels. Again, I am successfully getting the JSON object logged to the console but ONLY when running this script in the console. When I add this same code to 'Run a Javascript' button I recieve the error: SOAP.stringFromStream(stream) is not a function.

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
New Here ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

var API = "https://...{path}..CaseNum="
var caseno = this.getField("caseno");

var fullURL = API + caseno;


var getData = app.trustedFunction(function (cURL) {

    app.beginPriv();

    var params = {

       cVerb: "GET",

       cURL: cURL,

       oHandler: {

            response: function (msg, uri, e) {

                var stream = msg;

                var string = "";

                string = SOAP.stringFromStream(stream);

              console.println(string);
              var data = eval("(" + string + ")"); 
              console.println(data.caseNumber);
              
            }
        }
    };

    Net.HTTP.request(params);

    app.endPriv();

});

getData(fullURL);

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 ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

You can't use that code in a button. You can call it from a button but only if it is installed at the application level. Do you need to distribute these PDF files to desktops that you are not in control of? 

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
New Here ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

Yes, so I have put it in my app folder level as I have been reading about in the documentation.

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 ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

Yes. If you want to use the HTTP object. But there are other ways to achieve the same thing that will work when attached to a button. You'll just need to format the response as FDF instead of JSON.

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
New Here ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

Ok, so I figured out how to call my getData function from within the button successfully and it prints the info i need it to in the console but this is only if the full API is added as the parameter. However, I still need to be able to rely on this.getField function to access my 'caseno' form field. So whatever Case Number is input into the 'caseno' field gets pulled and added to the end of my API string.

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

The "this" keywords is context dependent. It references the current object. In most PDF scripts it is usually the current document only becuase most scripts operated at the document level.  When you create a function as an object member, the "this" keyword inside the function is that object. To use a particular document object in such a function, it needs to be either passed into the function or stored in an area accessible to the function. A good choice is to store it in the same object where the function is a member. 

For example:

 

var getData = app.trustedFunction(function (oDoc, cURL) {
    app.beginPriv();
    var params = {
       cVerb: "GET",
       cURL: cURL,
       oDoc: oDoc,
       oHandler: {
            response: function (msg, uri, e) {
                  ...
                  this.ODoc.getField("...").value = ...; 
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
New Here ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

I found this on stack overflow (Embedded Javascript Code for Adobe Acrobat: TypeError: this.getField is not a function) it seems like you are all over the internet helping us with your knowledge of Adobe!

 

I have the folder level script installed and it runs when the document opens. My new error is similar to this question where 'this.getField' is not a function. I removed my function call as was mentioned in the blog post but I am unsure how to reference my function now. When I try calling getData(fullURL) from within the button it says it is not defined. I do understand how that would be an issue, I just can not come up with the solution to reference the folder level script properly from within a button. Could you offer some assitance?

 

Thanks again for your help!

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 Beginner ,
Jan 11, 2023 Jan 11, 2023

Copy link to clipboard

Copied

Hello there, I am facing the exact same issue. I get JSON data from URL into the console inside Acrobat Reader but I am not able to fill fields with that data. Have you found a solution?

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 ,
Jan 11, 2023 Jan 11, 2023

Copy link to clipboard

Copied

LATEST

Be clear. You are saying that you are using the "Net.HTTP.Request()" function in a script in the console window in Acrobat Reader to acquire JSON from a server? 

If so, then what is stopping you from using another script in the Console Window to convert the JSON text into a JS object? Then using that object to fill fields on the PDF?

 

 

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