Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Import JSON database with Javascript

Explorer ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Hello,

I would like to be able to create a .json file on my Desktop and then to be able to import/read it in my feilds of my PDF. I want to be able to choose elements from this file. For example, I will have a feild which will get a result from a sum, then in second feild a reference will appear from a database (my json file) due to the result of the first feild. 

I use javascript in calculate area of feilds to run codes.

Also, I'm using Adobe Acrobat Standard DC, maybe I can upgrade it to Acrobat pro if I need it.

 

However, if this solution is not working I can try something else with database (I don't know anaything at all about it that but I can learn).

 

I'm looking for a good solution for fews days now, but I didn't find something that working for me. 

I hope that someone would be able to help me !

 

Thanks in advance,

 

Tanguy

TOPICS
How to, JavaScript, PDF forms

Views

711

Likes

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
LEGEND ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Acrobat JavaScript can no longer read from a database; this function was removed almost 20 years ago. Acrobat really isn't designed for this task.

Likes

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

A json file is a plain-text file, though, so it can be read, parsed and the data in it used in a script.

This is not a simple task, though.

Likes

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Okay thank you very much.

 

I can't doing that, here what i successed to do : 

var oStream = util.readFileIntoStream();
var strJSON = util.stringFromStream(oStream);
var oData = eval("("+strJSON+")");
console.println(oData);

 

Thanks to this code I did appear this text :

{"name": "John", "age":31, "city":"Paris"}

 

This is from my JSON file where there is : 

'{"name": "John", "age":31, "city":"Paris"}'

 

Now my question is how to do in order to get only the name or the age ?

Likes

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

oData.name ...

Likes

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Okay thanks it works, thank you ! (I used oData['name'])

 

However now, I would like to be able to don't have to choose the file myself, I want that it appears the value instantly when the previous feild get a value/or when I click on a button. How to automate it ?

Likes

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

- Actually, oData['name'] should have worked, too...

 

- To do that you need to specify the path of the file to read as a parameter of readFileIntoStream, but that requires running it from a privileged context, such as a trusted function in a folder-level script.

Likes

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
Adobe Community Professional ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

If you are using the latest Acrobat DC, then a better option for both generating and parsing the JSON file is to use the JSON object, which is built into core JS.

 

JSON.stringify() and JSON.parse()

 

As try67 pointed out. It's easy to read raw file data with the util.readFileIntoStream() function. However, saving the JSON file is more difficult.  For that there are a couple of awkward methods. One is to write the JSON to a file attachement with the "this.createDataObject()" function, or to create a PDF containing the JSON text and save the file as converted to plain text. I haven't tried this, so I don't know if there are punctuation/character encoding pitfalls.

 

For all of these operations you'll need to put your code into a folder level trusted function. Silently reading a file and saving files can only be done with privilege. You can read more about it here:

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Jun 28, 2021 Jun 28, 2021

Copy link to clipboard

Copied

LATEST

Thank you to both of you,

I will try all of these things and hopefully that is work !

Likes

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
LEGEND ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

On reflection, you could use Acrobat, and JavaScript, but not ONLY Acrobat and JavaScript.

 

Suggest you write an app using Visual Basic that handles the database access, then connects to Acrobat through the VB:JavaScript bridge. It can then set the fields in the PDF from the database values you already have.

Likes

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 ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Okay thank you very much for the answer !

 

Where can I learn easily how to doing that ?

Likes

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