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

93 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

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

    ReplyDelete
  3. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  4. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  5. Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.
    AWS Online Training
    AWS Training in Bangalore

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

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

    ReplyDelete
  8. 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
  9. Thanks a lot for writting such a great article. It's really has lots of insights and valueable informtion.
    If you wish to get connected with AI world, we hope the below information will be helpful to you.
    Python Training Institute in Pune
    Python Interview Questions And Answers For Freshers
    Data -Science
    ML(Machine Learning) related more information then meet on EmergenTeck Training Institute .
    Machine Learning Interview Questions And Answers for Freshers
    Thank you.!

    ReplyDelete
  10. I like viewing web sites which comprehend the price of delivering the excellent useful resource Python classes in pune free of charge. I truly adored reading your posting. Thank you!

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

    ReplyDelete
  12. 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
  13. 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
  14. 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
  15. Hiiii...Thanks for sharing Great info...Nice post...Keep move on...
    Angular JS Training in Hyderabad

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

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

    ReplyDelete
  18. 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
  19. Very useful and information content has been shared out here, Thanks for sharing it.php training in bangalore

    ReplyDelete
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. Your articles really impressed for me,because of all information so nice.sap tm training in bangalore

    ReplyDelete
  26. Linking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.sap simple logistics training in bangalore

    ReplyDelete
  27. 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
  28. This is really an awesome post, thanks for it. Keep adding more information to this.sap mm training in bangalore

    ReplyDelete
  29. Thanks for sharing this blog. This very important and informative blog.hadoop training institutes in bangalore

    ReplyDelete
  30. 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
  31. 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
  32. Post is very useful. Thank you, this useful information.

    eTechno Soft Solutions offers the industry recognized Job Oriented Training in Bangalore that combines corporate training, online training, and classroom training effectively to fulfill the educational demands of the students worldwide.

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

    ReplyDelete
  34. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    angularjs online training
    top angularjs online training
    best angularjs online training

    ReplyDelete
  35. 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
  36. 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
  37. Expected to form you an almost no word to thank you once more with respect to the decent recommendations you've contributed here.
    Machine Learning Training In Hyderabad

    Machine Learning Course In Hyderabad

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

    ReplyDelete

  39. 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
  40. 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
  41. 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
  42. Nice blog Post ! This post contains very informative and knowledgeable. Thanks for sharing the most valuable information.
    Data Science Training in Hyderabad

    ReplyDelete
  43. 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
  44. Good Explanation Is Very Good And Nice Informative Topic You Have Choosen..
    AWS Course Training in Hyderabad

    ReplyDelete
  45. I like your post. Everyone should do read this blog. Because this blog is important for all now I will share this post. Thank you so much for share with us.
    DevOps Training in Hyderabad
    DevOps Course in Hyderabad

    ReplyDelete
  46. Thank you so much for sharing your brilliant thoughts with us. Visit Ogen Infosystem for creative Website Designing and SEO Services in Delhi, India.
    Website Designing Company in Delhi

    ReplyDelete
  47. 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
  48. Attend The Business Analytics Course From ExcelR. Practical Business Analytics Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course.
    Business Analytics Course

    ReplyDelete
  49. 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
  50. 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
  51. 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
  52. I have been searching to find a comfort or effective procedure to complete this process and I think this is the most suitable way to do it effectively. ExcelR Data Science Course In Pune

    ReplyDelete
  53. 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
  54. 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
  55. ExcelR provides Business Analytics Courses. It is a great platform for those who want to learn and become a Business Analytics. Students are tutored by professionals who have a degree in a particular topic. It is a great opportunity to learn and grow.

    Business Analytics Courses

    ReplyDelete
  56. 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
  57. 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
  58. Thank you for sharing this valuable content.
    I love your content it's very unique.
    DigiDaddy World

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

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

    by cognex AWS Training in chennai

    ReplyDelete
  61. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Training in Bangalore

    ReplyDelete
  62. I Want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavors.
    data science institute in bangalore

    ReplyDelete
  63. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data analytics course in bangalore

    ReplyDelete
  64. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    cyber security training in bangalore

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

    deep learning course in delhi

    ReplyDelete
  66. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
    data analytics training in bangalore

    ReplyDelete
  67. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete

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