How to insert function in PDF document? - javascript

Is anyone familiar with plugging a function into a PDF document (specifically, the "insert custom code" feature in Adobe Acrobat Pro)? I know it's possible to initiate a function on an event...but I'm not sure what event would best handle this. I'd assume a button press. Also, does anyone know which language is compatible with PDFs? Is Javascript the only one? I don't imagine a server-side code (like PHP or Ruby) would work. Or the use of a framework like JQuery. Maybe it's only ActionScript or some strict language like C#. I dunno :S

Have a look at this page on the Adobe Acrobat developer site:
http://www.adobe.com/devnet/acrobat/overview.html#JavaScript
This explains that the language you're referring to is JavaScript with a number of additional objects related to Acrobat itself exposed.
So if you want to add functionality inside a PDF file (commonly either as the result of a button press indeed, but you can also do field validation, things at document open etc...) it will have to be JavaScript. Read this page to get all details about JavaScript (including the reference):
http://www.adobe.com/devnet/acrobat/javascript.html

Related

call a js function from links in pdf

We use iframe to show pdf in our angular webapp. These pdfs have href links inside them that actually are calls to javascript function.
It looks like below.
href="javascript:openMyLink('stringOfInformation')"
function openMyLink is supposed to be present in my webapp and it will use passed stringOfInformation to make further REST calls.
The problem is I'm not able to figure out where and how do I write this function. This function may be of global space but it should also be able to access my
angular services inorder to make REST calls.
I tried writing it inside my controller and even inside a separate file but it is not getting invoked.
Please guide me on this.
Acrobat does support its own flavor of JavaScript (see the Acrobat JavaScript documentation, which is part of the Acrobat SDK, downloadable from the Adobe website).
When there is some JavaScript within the PDF, it has to be Acrobat JavaScript, and follow the according document object model.
The issue is more that in order to make use of that, your users will need a PDF viewer which is smart enough to deal with Acrobat JavaScript (and that number is quite small for Windows/macOS, and very small for iOS/Android, and it is null for the PDF viewers integrated in the web browsers.
Now, if it is a simple link, you won't even need Acrobat JavaScript. Instead of that specify the link within the PDF, using the Link tool. This may be understood by even a bit a bigger range of PDF viewers.

Attach PDF to PDF as attachment (not as a page) via Javascript in HTML (not in Acrobat)

I would like to generate a PDF portfolio using JS from an HTML/CSS page on a local machine. I would use a PDF template file which includes a PDF portfolio Navigator in SWF form. I have successfully accomplished this using C# and a command line program, but can not identify the proper Javascipt components to do this browser-side or pseuo-server with Node.js. Basically, I am looking for something which will allow me to append a PDF to a new or existing PDF via configuration choices and an 'assemble' action using a JS or HTML button. iTextSharp provides the required PDF interaction functionality, but I can not figure out to run this inside an HTML to allow configuration via the HTML/CSS DOM (i.e. checkboxes, text field desciptors, etc...). Does a library with this type of functionality exist?
So you want to create a PDF using JavaScript?
On a quick google search, I found what appears to be a javascript library for creating and manipulating PDFs call jsPDF
If you want information on how to upload files with JavaScript alone, here is an article on how to do that. It also shows you how to use the file element.
For style, I recommend using a CSS Framework is you don't know much about CSS. I personally use Twitter Bootstrap for quickly prototyping things. It's quick and easy, and has good documentation. You can also use this to see how to make a form in HTML. I haven't got any good starter tutorials for HTML off the top of my list, sorry.
If you don't know much about JavaScript, when it comes to getting the options from the form, so that you can use them as configuration options, I'd suggest using the jQuery framework. It'll help you get up and running quickly enough
Note, all of this shouldn't replace basic training in JavaScript and HTML/CSS. Frameworks make things simpler, but if you don't know how to do something without a framework, you're going to have a hard time with a lot of the more complicated things. This goes for every language

How can I put JavaScript code into a PDF document?

How do I insert JavaScript code into a PDF file?
I can code JavaScript, I just wish to know how to put it into a file to, for example, display the current date, use a combobox, etc.
JavaScript is typically tied to objects in a PDF document. For example, if you want the user to be able to print the PDF by clicking a Print button, follow the steps below:
Add a button to the form (done under advanced editing)
Right click on the button and go to properties
Click the actions tab
Under Select Action: choose Run a JavaScript
put print(); in the code window
Here's a link to an old (Acrobat 7.0.5) Acrobat JavaScript Scripting
Reference to get you started.
Fresh Links 22/06/2020
Adobe Acrobat 7.0 JavaScript Scripting Reference
JavaScript for Acrobat API Reference
You didn't stipulate a constraint for language, so you can use Python to achieve this. The library PyPDF2 has a class called addJS that works. Here is an example from the REPL:
mypdf.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
and here is a link to the documentation. Note, that you need to have python3 installed (2.7 is going away at the end of 2019) and then run pip3 install PyPDF2 or similar to install the library and then load it via import PyPDF2. Hopefully that helps.

Adobe JavaScript in HTML: Step ZERO

I am trying to start learning Adobe JavaScript and use it in HTML. I downloaded the Acrobat JavaScript Scripting Guide and Reference to help me with the code but I don't know where to put that code. I wish it can be something I put in a web browser.
I don't think I can simply embed a pdf in my HTML and use JavaScript to refer to it because the manual says:
HTML JavaScript cannot access PDF
elements, only Acrobat JavaScript
can
(I am not sure though)
Now what should I do to use Acrobat JavaScript in HTML (how to include the code)?
JavaScript code requires a host application to execute. The host application has a JavaScript engine which will execute JavaScript code in documents that the host application is fed.
The JavaScript engine in a browser will execute JavaScript code in a web page. A
PDF viewer or plugin will execute Javascript code inside a PDF document.
The JavaScript code in a web page cannot interact with the JavaScript code in a PDF document and vice versa, as they are executed by different JavaScript engines.
The execution environment for Acrobat JavaScript is within a PDF. Some PDF viewers other than Acrobat/Reader support this object model to some degree or another, but for the Real Deal, you need an Adobe PDF program.
You can download a free trial of Acrobat Pro from adobe.com, that'll get you started.
Once you have Acrobat Pro, open up any old PDF, add a button to it (NOT with LiveCycle Designer),
Edit that button's actions (last tab)
I suggest you pick the mouse-up event (the default), though there are several others to choose from.
You then select the action type "Run A JavaScript", you'll have to scroll down a bit.. and click "add"
Type your JS into the resulting JS editing window. Their JS editor is Weak.
Start typing.
Close the editor, and close the button properties.
click on the button (with the hand tool, like a normal user)
Check the JS console for where you went wrong. ;) ctrl+j to bring it up.
You can also type script directly into the JS console and hit ctrl+enter to evaluate it, but this is really only useful for one-liners.
Your magic debugging function is console.println(). Note that the JS console actually supports things like setting break points, stepping through code, and so forth. All very handy.
A good script to start might be something as trivial as:
app.alert( "hello world" );
or
console.println( this );
You'll find that this is almost always the document object.
What are you trying to do with your PDF script?
PS: There actually IS a way to communicate between a web page and a PDF, but it's a little convoluted, and I've never actually seen anyone do it. Both the page and the PDF have to use specifically named functions that are called by The Other Environment.
Other handy code snippet:
// assuming 'this' is the "doc", which as I noted earlier, is generally the case
this.getField("fieldName").value
function whatAllDoesThisThingHave(thing) {
console.println( thing );
for(var i in thing) {
console.println( "\t" + i + ": " + thing[i] );
}
}
this.submitForm(url, bazillion, other, parameters, check, the, docs);
To access the PDF control, I think your best bet would be something I found here:
Recommended way to embed PDF in HTML?
Specifically:
<div id="pdf">
<object width="400" height="500" type="application/pdf" data="/my_pdf.pdf?#zoom=85&scrollbar=0&toolbar=0&navpanes=0" id="pdf_content">
<p>Insert your error message here, if the PDF cannot be displayed.</p>
</object>
</div>
I believe you can access the ActiveX control via getElementById("pdf_content"), though I could be mistaken. I'm a PDF Guy.
I seem to have found something from one of Adobe's manuals relevant to this question.
It is not a total answer, but at least it can help.
On Windows, the only OLE automation
supported for Adobe Reader is the
PDF browser controls interface, which
enables you to treat a PDF document as
an ActiveX document within an external
application. This makes it possible to
load a file, move to various pages
within the file, highlight a text
selection, and specify various print
and display options, as shown below.
If this helps to manipulate PDF from HTML JavaScript, the question is now:
how could I access PDF browser controls interface??
The manual says I should use, AxAcroPDFLib.AxAcroPDF this PDF browser controls but how should I access that??
I think all the puzzle pieces are on the table, I just don't know how to put them together!!

newbie question about javascript embed code?

I am a javascript newbie. I am trying to write a requirements document, and need some help describing what I am looking for. We want our application to generate a javascript snippet like this:
<script src="http://www.jotform.com/jsform/10511502633"></script>
This will load a web form.
So my question is:
- How does a single script load an entire web form? Is this a JSON?
- What is this called? Is this a cross browser javascript?
- Can anyone point me in the direction of learning more about what this is?
Thank you for your help!
The javascript file is just hosted on an external site. It appears to be dynamically generated, so feel free to use some fancy words ;) But basically, you just include it here, as if it was on your own site.
You could say "The application will generate the required script-tags to include dynamically generated javascript file from an external, third-party site".
Offcourse you need to take special cautions for cases when the include won't work, because the other site is not reachable (site is down, DNS does not work, file is moved on other webserver, your application is on an intranet/behind a proxy/firewall...). Why can't you copy their file and mirror it locally? Or use a reliable Content Delivery Network, like Google or Amazon.
There are many names for this type of inclusion. The most common being widget.
What does it actually do:
take an id of some sort as parameter
use the id to fetch some specific data (most likely from a database)
generate some js and html based on the id/data
usually this involves iframes of some sort.
To use a script rather than an html iframe has multiple advantages
you can change what is actually delivered to the users browsers without changing the include
you can resize the iframe to fit certain predefined sizes
you can inject the necessary things into the page the widget is included (of course you need to make sure this is sanctioned)
We use this all the time and we never regreted it.
If you don't want to build the widget infrastructure yourself you can always use one of the widget providers like widgetbox:
http://www.widgetbox.com/widgets/make/
With those you are up and running in no time.
This is typically called a script include.
Google have lots of these types of items, and even they call them by many names,
widgets, custom javascript, snippets, custom code, etc. It really depending on who you are writing for... I would go with "cross platform embeddable javascript code" meaning that it would need to load all its dependancies. Also specify which browsers need to be supported and what should happen is the user has javascript turned off.
EDIT :
Actually since we are talking unique IDs, you will need 2 parts probably, the user/site unique "cross platform embeddable javascript code" and whatever serverside code to support it. Basically this is an API that is accessed using your own javascript widget. Feel free you point to examples in your requirements document, programmers love examples.

Categories

Resources