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.
Copy link to clipboard
Copied
It's possible but non-trivial. How much JavaScript experience do you have?
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.
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);
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?
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.
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.
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.
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 = ...;
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!
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?
Copy link to clipboard
Copied
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?