Sunday, February 26, 2012

How to Embed JavaScript into PDF

1. Introduction
In this article I continue from my last post How to Manually Create a PDF. I explain how to embed JavaScript into a PDF document and how to extract the JavaScript from a document as well. Malicious code is often embedded as JavaScript inside a PDF document and extraction of the JavaScript is a useful method to isolate and reverse engineer the code for security professionals.

2. AlertBox Example
In the last article I explained the format for a PDF and provided a simple example that printed “Hello World!” at the top of the document. It contained only the bare essentials and we will now add on to the example and include JavaScript. Below is the code for the original example created in the last post.



In order to introduce JavaScript to the PDF we need to modify the original example and add three new objects. The first object will be an indirect object that has a reference to the JavaScript object.


As you can see in the figure above, object 6 has the JavaScript tag and points to the object 7. That is all we need to include for the first object. The second object will be another pointer. It will include the “Name” tag and this allows us to name the JavaScript code we will introduce to the PDF document. As you can see we have a reference to the third object which is object number 8. The name I give the JavaScript code I will introduce is “My Code”.


The third object I need to introduce to embed JavaScript is an object with the actual JavaScript code. In my example this is object number 8.



Object 8 has three tags we need to include. The first is the “/JS ” tag which stands for JavaScript and holds the JavaScript code we wish to run. In this example I utilize the app object and use the method alert. This allows me to display an alert box when the PDF is opened. “cMsg” defines the message I wish to display within the textbox and “cTitle” is the title header for the textbox. The second tag is “/S” which describes the action dictionary which leads to my third tag which is “/JavaScript”. In order for my JavaScript to run we must make final adjustments to the PDF document. We must update the xref section of our document to account for the three new objects added.


The x ref tag will now include 9 objects and the size tag will change to 9 as well. The last modification to make is in our catalog object which is our root object 1.


We must include a reference to our JavaScript object which is object 6. We use the “/Name” tag to set the pointer. Now we have a fully functional PDF that will run the JavaScript when the document is opened. Below is a screenshot of the alert box that is produced by the JavaScript in my example.


3. TextBox Example
Let us look at another example that uses JavaScript to introduce a text box into the PDF document. We can use the template from above and make one simple change to the JavaScript section. In order to modify our JavaScript we only need to change object 8.


Here we have the same tags and only modify what is inside the parenthesis. In order to add a text box I use the document object and gain access to it by using “this”. “this” is a pointer to the current document and I am able to create a text box by using the “addField” method. Line 64 shows how I implement this method. “addField” takes four parameters. The first is the name for my textbox and in my example it is simply “My Text Box”. The second parameter is the type of field we wish to add. Since we require a textbox I use “text”, however others such as button are also available. The third parameter is the page of the document. It is an index that begins at zero and since I want the textbox on the first page, the value of the parameter is 0. The last parameter is the position of the text box. Previously on line 63 I initialize the coordinates of the textbox. Position takes a list of four numbers, measuring the box from left-top corner, right-top corner, bottom-left corner, and bottom-right corner. After this change we have a document that produces a textbox from the JavaScript we just created. Below you can see a screenshot of the PDF example. In the upper-left corner is the text box that is displayed in gray.


4. Conclusion
To sum up, we have two examples of how to incorporate JavaScript into a PDF document. I utilized JavaScript to display an alertbox and a textbox. Many more objects and methods can be controlled using JavaScript and the full reference can be found in Acrobat JavaScript Scripting Reference [1]. In the next article I will continue to show how to extract JavaScript from a PDF and how to decode a PDF which is normally encoded with filters such as "flatDecode".

References
[1] "Document Management - Portable Document Format", Available at
[2] "Acrobat JavaScript Scripting", Available at 
       http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJSGuide.pdf

59 comments:

  1. Love the blog post. Or people could use JS2PDFInjector if they just want to inject a file into a PDF easily.

    ReplyDelete
  2. Great Post!
    Thanks a lot for sharing!
    I found this blog to be very useful!!
    JAVA training in Bangalore

    ReplyDelete
  3. Its a wonderful post and very helpful, thanks for all this information.
    Javascript Training institute in Noida

    ReplyDelete
  4. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. The article you have shared here very awesome, I have finally found a worth able content to read, your information in this blog is impressive. Keep sharing more like this.artificial intelligence course

    ReplyDelete
  7. It’s very informative and useful blog about Embed JavaScript . It’s a great list of Blog. I just loved exploring this amazing list of articles. Thanks for Sharing. Digital Marketing Course

    ReplyDelete
  8. Attend The Data Analytics Course Bangalore From ExcelR. Practical Data Analytics Course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course Bangalore.
    ExcelR Data Analytics Course Bangalore

    ReplyDelete
  9. Thanks for sharing such a great blog Keep posting.. 
    Javascript Training in Delhi

    ReplyDelete
  10. Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.html training in bangalore

    ReplyDelete
  11. Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.css training in bangalore

    ReplyDelete
  12. Very useful and information content has been shared out here, Thanks for sharing it.php training in bangalore

    ReplyDelete
  13. I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.mysql training in bangalore

    ReplyDelete
  14. Linking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.angular 2 training in bangalore

    ReplyDelete
  15. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.angular 4 training in bangalore

    ReplyDelete
  16. Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.angular 7 training in bangalore

    ReplyDelete
  17. I know that it takes a lot of effort and hard work to write such an informative content like this.node.js training in bangalore

    ReplyDelete
  18. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.sap wm training in bangalore

    ReplyDelete
  19. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

    Become an Expert In DBA Training in Bangalore! The most trusted and trending Programming Language. Learn from experienced Trainers and get the knowledge to crack a coding interview, @Bangalore Training Academy Located in BTM Layout.

    ReplyDelete
  20. such a great information for blogger i am a professional blogger thanks...

    Upgrade your career Learn SharePoint Developer Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  21. Its a wonderful post and very helpful, thanks for all this information.
    Fashion Designing Institutes in Uttam Nagar

    ReplyDelete
  22. I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic ai online course.

    ReplyDelete
  23. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. artificial intelligence course

    ReplyDelete
  24. Study ExcelR Data Analytics Course in Bangalore where you get a great experience and better knowledge.
    Data Analytics Course in Bangalore

    ReplyDelete

  25. This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. Artificial Intelligence training in Chennai.


    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  26. Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
    aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery

    ReplyDelete
  27. Thanks for the insight , with so indepth knowledge.

    Some of the SAP Institute i know are :

    SAP Training Institute Delhi

    Sap training institute in Delhi for S4 HANA FICO


    ReplyDelete
  28. Great Post!! This is very informative post, thanks for share it with us Amazing Post!! This is very impressive post.

    digital marketing coaching in hubli

    ReplyDelete
  29. Good Explanation Is Very Good And Nice Informative Topic You Have Choosen..
    AWS Course Training in Hyderabad

    ReplyDelete
  30. I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    Devops Training in USA

    Hadoop Training in Hyderabad

    Python Training in Hyderabad

    ReplyDelete
  31. Hello,This is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information……nice…….
    Aws Training in Chennai

    Aws Training in Velachery

    Aws Training in Tambaram

    Aws Training in Porur

    Aws Training in Omr

    Aws Training in Annanagar

    ReplyDelete
  32. This is excellent information. It is amazing and wonderful to visit your site. Thanks for sharing this information, this is useful to me...
    Looking for the best ppc course in Bangalore, India? Learn PPC from Ranjan Jena, 10+ Years Expert Google Ads Trainer. 1000+ Students Trained @ eMarket Education, Koramangala, Bangalore.
    Best Online Digital Marketing Courses in Bangalore, India
    Best Digital Marketing Institute in Bangalore

    ReplyDelete
  33. Awesome post. You Post is very informative. Thanks for Sharing.

    Looking for the best PPC course in Bangalore India? Learn PPC from Ranjan Jena, 10+ Years Expert Google Ads Trainer. 1000+ Students Trained @ eMarket Education, Koramangala, Bangalore.

    Best Online Digital Marketing Course in Bangalore, India
    Best Digital Marketing Institute in Bangalore

    ReplyDelete
  34. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    Best Digital Marketing Courses in Bangalore

    ReplyDelete
  35. It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. ExcelR Business Analytics Courses

    ReplyDelete
  36. I think this is a standout amongst the most critical data for me. What"s more, i"m happy perusing your article. Be that as it may, ought to comment on some broad things ExcelR Business Analytics Courses

    ReplyDelete
  37. Thanks mate. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also event marketing and thank you letter to vendors after an event

    ReplyDelete
  38. Thank you for sharing this valuable content.
    I love your content it's very unique.
    DigiDaddy World

    ReplyDelete
  39. Cognex offers AWS Training in chennai using classroom and AWS Online Training globally.

    ReplyDelete
  40. Thanks for the blog, it was a useful content shared by author.

    by cognex AWS Training in chennai

    ReplyDelete
  41. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....

    deep learning course in delhi

    ReplyDelete

Note: Only a member of this blog may post a comment.