I am trying to import data from a tab delimited text file into a PDF using javascript. I am attempting to do this (testing just the first line, at least) through the JavaScript Console using the following code:
var blah = this.importTextData("/C/Users/sbarry/Desktop/abc123.txt", 0)
console.println(blah)
My text file looks like this:
First Second
ABC 123
DEF 456
GHI 789
I made sure I had form fields labeled First and Second on my PDF and then executed my code. The result is:
undefined
It runs successfully.
Under my preferences I have the checkbox "Enable JavaScript Actions" checked, and I have "Enable Safe Reading Mode" unchecked in order to overcome any security restrictions. How can I make this work?
I am using Foxit PhantomPDF ver. 7.2.
UPDATE:
I got a response from someone at the Foxit team. Apparently their software recognized the function but is unable to implement it, so hopefully that will be addressed in the near future. For now, I am going to be using this.importAnFDF by generating an FDF file from scratch. (NOTE: This function does not have a return value, unlike this.importTextData.)
For information on FDF file structure, see section 12.7.7 of this document:
http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
The easiest way to see what an FDF file requires is to export one from your PDF editing software, open it with notepad or something similar and edit the field names and values to whatever is appropriate for your PDF form.
Related
I am attempting to have a button on a form that will launch file explorer for the user to select pdf files to insert into form.
The insertPages script will insert pages from a specific cPath, but I need the user to be able to select the pages to insert, as they will be different from case to case. Is there a way to accomplish this using javascript?
I am using Bluebeam, which is very similar to Acrobat. I have created several templates and javascript code using the Acrobat API Reference, and thus far the Bluebeam engine appears to operate nearly identically. In a perfect world, the button would launch the "Insert Pages" menu in Bluebeam.
Thanks in advance for the help!!
If this were Acrobat, I'd use app.browseForDoc(). The returned object has three properties...
cFS - A string containing the resulting file system name for the chosen file.
cPath - A string containing the resulting path for the chosen file.
cURL - A string containing the resulting URL for the chosen file.
Get the full path to the file from there then use insertPages.
In Acrobat, it can only be run in a Privileged context. I'm not sure if it will work the same way in BlueBeam though they have done a fairly good job of duplicating the form field related JavaScripts.
I have a large Acrobat file (Acrobat XI Pro) which I have Bookmarked. The resulting Bookmarks are, however, all over the map so the goal is to sort the Bookmarks A-Z. I searched for an appropriate Acrobat Action without success. I did, however, locate some possible JavaScript in the the Acrobat XI SDK:
http://www.adobe.com/devnet/acrobat.html
I downloaded it (at bottom of EULA for version XI):
http://www.adobe.com/devnet/acrobat/sdk/eula.html
I found a reference on batch sequences with three sections of interest: (1) Gather bookmarks to an array; (2) Sort Bookmark subroutine; and (3) Copy bookmarks from an array.
Ideally I would like to combine all scripts into a single JavaScript call in the Action. My first attempt was to create an Action with two separate JavaScript calls (1) as the GATHER and (2) & (3) as the COPY with the SORT integrated
The first JavaScript compiles after I put the script into the JavaScript call in the Action. However, the second JavaScript would not compile. The error was an "unterminated string literal" at this line of code:
+'var that =
app.openDoc("'+ global.bmArray[i][1]+'.pdf", [ERROR PRESENTS HERE]
this);\r\t'
+'if ( this != that ) this.closeDoc();\r'
I researched this error online and tried various solutions without success.
When I failed with building within an Action, I took the put the two JS scripts – the Gather and the Sort - into a .txt file and saved it as a .sequ which, when , should automatically populate into Acrobat as an Action. When I did this Acrobat reported it as corrupt and unloadable so that didn't work either.
My hope is a review of Adobe’s code with a solution to the unterminated string literal error a guide that the location guidance on the insert location of the SORT into the COPY part of the script.
My goal, as noted, is to have an Action that merely re-sorts the Bookmarks. The JavaScript I found seems to fit the bill as it marks the Bookmark links to the respective document. If I am incorrectly reading this and could be steered in the right direction, it would be appreciated.
What I need to do is:
Let user choose txt file from his disc
Get the text from it to let's say a variable
Send it (the variable value) via AJAX
For the first point I want to know if I should use normal input type (like if I would like to send file via POST) <input type="file">
For the second point I need to know how to get the name of the file user selected and then read text from it. Also I'm not good with javascript so I don't really know how long can a string be there (file will have about 15k lines on average)
For the third I need nothing to know if I can have the data stored in a variable or an array.
Thanks in advance.
P.S. I guess javascript is not a fast language, but (depending on the editor) it sometimes opens on my computer the way that I have all the needed data in first 5 or 6 lines. Is it possible to read only first few lines from the file?
It is possible to get what you want using the File API as #dandavis and other commentors have mentioned (and linked), but there are some things to consider about that solution, namely browser support. Bottom line is the File API is currently a working draft of the w3c. And bottom line is even w3c recommended things aren't always fully supported by all browsers.
What solution is "best" for you really boils down to what browser/versions you want to support. If it were my own personal project or for a "modern" site/audience, I would use the File API. But if this is for something that requires maximum browser support (for older browsers), I would not currently recommend using the File API.
So having said all that, here is a suggested solution that does NOT involve using the FIle API.
supply an input type file in a form for the user to specify file. User will have to select the file (javascript cannot do this)
use form.submit() or set the target attribute to submit the form. There is an iframe trick for submitting a form without refreshing the page.
use server-side language of choice to respond with the file info (name, contents, etc.). For example in php you'd access the posted file with $_FILES
then you can use javascript to parse the response. Normally you'd send it as a json encoded response. Then you can do whatever you want with the file info in javascript.
With Chrome and Firefox you can read the contents of a text file like this:
HTML:
<input type="file" id="in-file" />
JavaScript with jQuery:
var fileInput = $('#in-file');
fileInput.change(function(e) {
var reader = new FileReader();
reader.onload = function(e) {
console.log(reader.result);
}
reader.readAsText(fileInput[0].files[0]);
});
IE doesn't support the FileReader object.
For a select box on our website, we have changed the ajax call request to javascript array file which used to fill the city select box options. These are now filled from the same file containing only js array.
This js only array file is created dynamically on some other server and transferred to static server. Most of the times it works good, but fails occasionally and file got corrupted or not fully transferred. In that case the file contents look like
var cityArray = [{SID : "15",SN : "Rajasthan",cities : [{CID : "677",CN : "Jaipur"}, //syntax error, no eol
So, when we try to use this file, all js stop working.
My question here is how can i check that the included file contains errors and use the legacy method to fill the select box options?
Thanks in advance.
I'd cheat a little:
Use a XMLHttpRequest to fetch a copy of the file as a string instead of adding a <script> tag.
Check if the string has an EOL, if not - add one.
Eval() the string in a try-catch statement.
Not only should that let you use your particular type of 'broken' files, but your script won't fall over if its broken in other ways.
I would like to save a csv file from a web page. However, the link on the page
does not lead directly to the file, but it calls some kind of javascript, which leads
to the opening of the file. In other words, there is no explicit url address for the
file i want to download or at least I don't know what it should be.
I found a way to download a file by activating Internet Explorer,going to the web page
and pressing the link button and then saving the file through the dialog box.
This is pretty ugly, and I am wondering if there is a more elegant (and fast) method to retrieve a file without using internet explorer(e.g. by using urllib.retrieve method)
The javascript is of the following form (see the comment, it does not let publish the source code...):
"CSV"
Any ideas?
Sasha
You can look at what the javascript function is doing, and it should tell you exactly where it's downloading from.
I had exactly this sort of problem a year or two back; I ended up installing the rhino javascript engine; grepping the javascript out of the target document and evaluating the url within rhino, and then fetching the result.