[javascript]Reading/Writing to Data Objects

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

lordhanoji
User
Posts: 1
Joined: Mon Jun 02, 2025 8:14 pm

[javascript]Reading/Writing to Data Objects

Post by lordhanoji »

Hello!

I am trying to attach a database of some kind to a PDF for a seating chart. I have created a PDF of my company's floorplan with numbered textbox's overlayed on each cube in a grid pattern. I then have colored textboxes on top of the grid to represent an employee sitting there. I had a working solution(doc level scripts attached to buttons) but now would like to move to a OOP(global js code that's doc specific) approach with a database linked to the PDF itself. I have tried several methods of storing the data(json, txt, csv, xml) I am more unsure on how to access said data in the PDF programmatically. Attached is my chart code(what I have so far), and a text document with an example of my data from my xml file. I found a couple examples to follow but I am not 100% sure how to create a data object from an existing xml file then link it to a PDF document and read/write from it to update the data contained. If anyone has experience with this and can give me some pointers on best practices, that would be greatly appreciated.
seating_chart.zip

*DISCLAIMER*
I am not well versed in JavaScript and have vibe coded my chart code. I plan to revise and rework my classes once I have a working solution to read/write data into the PDF.

*EXAMPLE 1*

Code: Select all

// get the file stream object of the attachment
var acrobat = this.getDataObjectContents("acrobat.xml");
// convert to a string
var cAcrobat = util.stringFromStream(acrobat, "utf-8");
// parse this and get XFAObject
var myXML = XMLData.parse(cAcrobat,false);
// change the value of grandad's income 
myXML.family.grandad.personal.income.value = "300000"; 
// save XML document as string, cAcrobat 
var cAcrobat = myXML.saveXML('pretty');
// convert to a file stream
var acrobat = util.streamFromString(cAcrobat, "utf-8");
// now "update" the attachment acrobat.xml with this file stream 
this.setDataObjectContents("acrobat.xml", acrobat);
*EXAMPLE 2*

Code: Select all

function DumpDataObjectInfo(dataobj)
{
    for (var i in dataobj)
    console.println(dataobj.name + "[" + i + "]=" + dataobj[i]);
}
// Prompt the user for a data file to embed.
this.importDataObject("MyData"); 
DumpDataObjectInfo(this.getDataObject("MyData"));
// Embed Foo.xml (found in parent director for this doc). 
this.importDataObject("MyData2", "../Foo.xml"); 
DumpDataObjectInfo(this.getDataObject("MyData2"));
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 606
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript]Reading/Writing to Data Objects

Post by Mathew »

I'm not sure if you are planning to use a local file, or HTTP to get the data file. If a local file, util.readFileIntoStream(...) will read file data. On PDF-XChange I've found it easier to use JSON than xml because the program doesn't implement the XMLData object. 1ang.js does this.

Saving it back to the file system, though, is much more difficult - intentionally. I don't think there's a way to write to a file without user interaction, except if it's a pdf file - even then there will be a security warning. You can create a temporary data object in the pdf document, use doc.exportDataObject() to interactively save it, then delete the data object. See the document script in my translation helper tool for an example.

For personal use, where I can save trusted locations so there's no security warning bugging me, I've used the rather roundabout method of:
  • use a pdf with a data object(s) attached,
  • open that pdf doc = app.openDoc(...) (it can be opened hidden if that's desired), then
  • get the data object with doc.getDataObjectContents(...), manipulating it as needed, then
  • save the data object back to the pdf doc.setDataObjectContents(...) -- or easier doc.removeDataObject(...) if it exists then doc.createDataObject(...), and
  • save the pdf doc.saveAs(...) or app.execMenuItem('Save',doc), then
  • close the document doc.closeDoc(true).
In the above, the parameters for the various methods are covered in the javascript api https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html

I think a much more robust solution revolves around using the Net.HTTP object, but you need to set up a way to serve the data.
Last edited by Mathew on Tue Jun 03, 2025 4:21 pm, edited 8 times in total.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: [javascript]Reading/Writing to Data Objects

Post by Stefan - PDF-XChange »

Hello Mathew,

Thanks for your feedback!
Indeed JS is deliberately limited in what it can do with the file system - and that is for security reasons mostly!

I hope that the suggestiong Mathew gave would be useful to @lordhanoji as well!

Kind regards,
Stefan
Mathew
User
Posts: 606
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript]Reading/Writing to Data Objects

Post by Mathew »

Stefan - PDF-XChange wrote: Tue Jun 03, 2025 11:59 am Indeed JS is deliberately limited in what it can do with the file system - and that is for security reasons mostly!
Important point: most of the above methods will not (and should not) work as part of a document script because they require privileged functions.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

[javascript]Reading/Writing to Data Objects

Post by Stefan - PDF-XChange »

:)