Run "importTextData" on a form button
Link in Zwischenablage kopieren
Kopiert
Hello!
I've been making a PDF-form to make invoices. And to make it a bit easier when filling my customers data, I did a spreadsheet with the data and saved it as "Tab Delimited Text" file. The import works fine but when I try to add a button called "Customer List" and run JavaScript to trigger the pop-up to choose which data I want imported, the pop-up won't trigger.
Oddly enough though, when I open the JavaScript console and press the Enter key on my numpad, the pop-up gets then triggered (as shown below)
Obviously I'm missing something to make it work, and I'll be very thankful for any help or if you could point me to the right direction where to learn this since my JavaScript knowledge is very limited! Thanks in advance!
Link in Zwischenablage kopieren
Kopiert
From the documentation of this method:
Note: (Acrobat 8.0) If cPath is specified, this method can only be executed during batch and console
events. See Privileged versus non-privileged context for details. The event object contains a
discussion of JavaScript events.
Link in Zwischenablage kopieren
Kopiert
Ok, I see.
Do you have any suggestions on an alternative? Because the "import form data" function only lets me import .fdf-files.
Link in Zwischenablage kopieren
Kopiert
This doesn't mean you can't use it, you just have to go about it differently.
To provide the code a "privileged context" you can, for example, call it from a .js file that's installed in the application's JavaScripts folder, and then it should work. See: https://acrobatusers.com/tutorials/trust-and-privilege-in-acrobat-scripts
Link in Zwischenablage kopieren
Kopiert
Ah ok!
I read the tutorial, but I don't seem to understand how to fix this "privilege" issued when running the JavaScript code.
Link in Zwischenablage kopieren
Kopiert
I've tried giving the code "privileged context" in a file "config.js" that I created. And it's placed in the Javacscripts folder on my "user" path. Yet I don't seem to make it work. I'm really new to JavaScripting, so I'll be glad if i could get some help!
Link in Zwischenablage kopieren
Kopiert
You're headed the right way. Did you create a trusted function? Do you get any errors in the console? What code did you add?
Link in Zwischenablage kopieren
Kopiert
I get a "NotAllowedError". And this is the code that I used.
var ImportTextData = app.trustedFunction(function(cPath)
{
app.beginPriv();
this.importTextData("/c/user/data/customer_data.txt", cPath);
app.endPriv();
});
Link in Zwischenablage kopieren
Kopiert
You either pass the path to the text file as a parameter, or you hard-code it into the code.
Link in Zwischenablage kopieren
Kopiert
Where does you place the code?
Link in Zwischenablage kopieren
Kopiert
On a config.js-file in the "user" javascripts folder path
Link in Zwischenablage kopieren
Kopiert
stefano.bianque schrieb
On a config.js-file in the "user" javascripts folder path
In following path?
C:\Users\<user>\AppData\Roaming\Adobe\Acrobat\Privileged\DC\Javascripts
Link in Zwischenablage kopieren
Kopiert
https://forums.adobe.com/people/Bernd+Alheit skrev
stefano.bianque schrieb
On a config.js-file in the "user" javascripts folder path
In following path?
C:\Users\<user>\AppData\Roaming\Adobe\Acrobat\Privileged\DC\Javascripts
Exactly on that path. Also it should be noted, that the folder didn't exist in my directory so I have to manually create it.
Link in Zwischenablage kopieren
Kopiert
When do you get the message? When config.js is loaded (Acrobat startup)? Or when you call the function? What is your function call and how do you call it?
Link in Zwischenablage kopieren
Kopiert
https://forums.adobe.com/people/Test+Screen+Name wrote
When do you get the message? When config.js is loaded (Acrobat startup)? Or when you call the function? What is your function call and how do you call it?
I get the "NotAllowedError" when I start Adobe and when I call the function in the PDF-form button I made. And the code I put was:
this.ImportTextData("/c/user/data/customer_data.txt");
Link in Zwischenablage kopieren
Kopiert
Try this:
var myImportTextData = app.trustedFunction(function(cPath)
{
app.beginPriv();
this.importTextData(cPath);
app.endPriv();
});
myImportTextData("/c/user/data/customer_data.txt");
Link in Zwischenablage kopieren
Kopiert
You will always get the error if you code this.ImportTextData. Adding your startup script cannot change that. You must actually use the function that you created. This is complicated stuff but you do need to read and understand the instructions. So, let's focus on that. Please try to follow them, and if anything doesn't make sense, ask us about that. When you have a full understanding, you will be ready to write the script.
Link in Zwischenablage kopieren
Kopiert
I've been reading these guides here:
Using Trusted Functions
https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf
yet, I fail to see how I'm supposed to implement the "ImportDataText" function. My JavaScript-knowledge is non-existent, so yes, it doesn't make any sense right now.
Link in Zwischenablage kopieren
Kopiert
The function you're defining does not become a method of the Document object (to which "this" refers to). It's an independent function.
Also, I highly recommend you change the name from ImportTextData to something else, as that is very similar to the built-in function's name and can cause confusion. Call my something like "myImportTextData", or "trustedImportTextData", or something like that (just like in Bernd's code above).
Link in Zwischenablage kopieren
Kopiert
We can't tell you everything about JavaScript in a few replies, so if you want to do this task focus on the guide. Please start at the beginning of Using Trusted Functions . Read through it carefully and stop at the first thing you do not fully understand. DO NOT SKIP ANYTHING, this is why your solutions are not working. Once you understand that point we can move to the second point, and so on. BEFORE you can understand this you have to know what a function is in JavaScript, so if you don't know that please say.

