Copy link to clipboard
Copied
Hello
I am trying to import multiple (6) rows into a text field from a text file. If i add 2 or more rows it gives me an ouptut of 0. Is there another way to achieve this?
I have tried this:
var i;
var full;
for (i = 0; i < 7; i++) {
full += importTextData("/C/Users/testfile.txt",i);
}
event.value = full;
This only read the 6th row in the file.
I have also tried this:
var a = importTextData("/C/Users/testfile.txt",0);
var b = importTextData("/C/Users/testfile.txt",1);
var c = importTextData("/C/Users/testfile.txt",2);
var d = importTextData("/C/Users/testfile.txt",3);
var e = importTextData("/C/Users/testfile.txt",4);
var f = importTextData("/C/Users/testfile.txt",5);
event.value = a+b+c+d+e+f;
This only outputted the last row in the file.
Please advise.
Using the Doc.importTextData() method you can only import one row at a time. The number of fields in your document needs to match the columns in your tab separated values file, and the column names have to match the text field names.
Based on your script, it looks like you are trying to do something different. The method does not return the value it reads, it returns a status code that is documented here: Acrobat DC SDK Documentation
Copy link to clipboard
Copied
Using the Doc.importTextData() method you can only import one row at a time. The number of fields in your document needs to match the columns in your tab separated values file, and the column names have to match the text field names.
Based on your script, it looks like you are trying to do something different. The method does not return the value it reads, it returns a status code that is documented here: Acrobat DC SDK Documentation
Copy link to clipboard
Copied
Thanks Karl for your reply. Is there a work around for this. I want to import multiple rows from one column into one text field. Any ideas on how I could achieve this?
Copy link to clipboard
Copied
This would require doing in using a custom-made script, because if you use the importTextData command it will just overwrite itself each time you run it.
Copy link to clipboard
Copied
Thom beat me to it, he outlined the two out of the three solutions I would have recommended too. I've used both approaches, and depending on your programming background, one may be easier to understand than the other one. For the first one (hidden fields), you can use the same approach that I've documented here: Batch-Import Excel Data into PDF Forms - KHKonsulting LLC
There is a third approach, and that requires to create a temporary PDF form with just the fields you need (it is similar to the hidden field approach). Take a look here for how that can be done: Batch-Import List Data into PDF Form - KHKonsulting LLC
Copy link to clipboard
Copied
I see two possible solutions.
1. Create a set of hidden fields that match the import data. Then import each line, copying the data to the correct form fields.
2. Use a folder level script to load the raw file data with "util.readFileIntoStream", then parse the data and place it where you need it.
Either way this is a complex scripting job.
Copy link to clipboard
Copied
Thom,
In regards to the second option, I had setup my Folder Level Script. I was able to add a Javascript to this folder with the following code you had posted on acrobatusers:
app.addMenuItem({cName:"JS Ref", cParent:"Help", cExec:"app.openDoc('/C/Users/Desktop/js_api_reference.pdf');" });
This created an option in the Help menu which redirected me to the JS Acrobat API pdf.
So, then I tried implementing util.readFileIntoStream using the following code:
var rStream = util.readFileIntoStream("/C/Users/Desktop/myData.txt");
var cFile = util.stringFromStream(rStream);
this.getField("myTextField").value = cFile;
I then saved the JavaScript as FirstTest.js
I closed Adobe and reopened it and got the following error:
NotAllowedError: Security settings prevent access to this property or method.
Util.readFileIntoStream:1:Folder-Level:User:FirstTest.js
I also went to the Security (Enhanced) option and added the JavaScript Folder as a privileged location. I also added my text file ("myData") as a privliged location as well. However i still get the same error.
Please advise
By the way I am using Acrobat Pro XI.
Thanks
Copy link to clipboard
Copied
From the documentation of readFileIntoStream:
If the cDIPath parameter is specified, this method can only be executed in privileged
context, during a batch or console event (or when the document is certified with a
certificate trusted to execute "embedded high privileged JavaScript").
Copy link to clipboard
Copied
Great point!
So I need to certify the document with a certificate trusted to execute embedded high privileged JavaScript.
Can you point me to a tutorial that goes about this process. The ones I found assume that I have a trusted certificate setup; however, i do not.
Thanks
Copy link to clipboard
Copied
Do not certify your PDF, use a folder level trusted function.
You'll find all the answers here:
Trust and Privilege in Acrobat Scripts
https://www.pdfscripting.com/public/ExcelAndAcrobat.cfm
Automating Acrobat can save you loads of time.
Copy link to clipboard
Copied
Google is always to blame for all wrongs !
British in the UK
Find more inspiration, events, and resources on the new Adobe Community
Explore Now