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

133 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. Our Web Designing Course and other IT Training Programs give professionals the opportunity to attend our open- sourced academic courses on the go which

    ultimately helps them build the skills and knowledge they need to reach their professional goals.

    WordPress Training
    Web Designing Course in Delhi
    SEO Course
    Digital Marketing Training in Delhi
    SMO Training
    PPC Institute in Delhi
    Joomla Course in Delhi
    JQuery Institute in Delhi
    JavaScript Course

    ReplyDelete
  4. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  5. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  6. 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
  7. 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
  8. 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
  9. Its a wonderful post and very helpful, thanks for all this information.
    Javascript Training in Delhi

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

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

    ReplyDelete
  12. 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
  13. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab Python training in pune your rss feed to stay informed of any updates.

    ReplyDelete
  14. 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
  15. 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
  16. This comment has been removed by the author.

    ReplyDelete
  17. 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
  18. 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
  19. 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
  20. Hiiii...Thanks for sharing Great info...Nice post...Keep move on...
    Angular JS Training in Hyderabad

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

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

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

    ReplyDelete
  25. 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
  26. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.javascript training in bangalore

    ReplyDelete
  27. Your articles really impressed for me,because of all information so nice.angularjs training in bangalore

    ReplyDelete
  28. 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
  29. 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
  30. 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
  31. 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
  32. Your articles really impressed for me,because of all information so nice.sap tm training in bangalore

    ReplyDelete
  33. 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
  34. 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
  35. Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.sap ewm training in bangalore

    ReplyDelete
  36. This is really an awesome post, thanks for it. Keep adding more information to this.sap mm training in bangalore

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

    ReplyDelete
  38. 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
  39. 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
  40. 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
  41. Its a wonderful post and very helpful, thanks for all this information.
    Fashion Designing Institutes in Uttam Nagar

    ReplyDelete
  42. 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
  43. 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
  44. 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 in Bangalore

    ReplyDelete
  45. 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
  46. Thanks for Sharing a very Nice Post & It’s really helpful for everyone. Keep on updating these kinds of
    Informative things Otherwise If anyone Want to Learn SAP Training Course Basic to Adv. Level So Contact THERE- 9599118710

    Some Best SAP Training Center in Delhi, India

    sap training institute in delhi
    sap training center in delhi
    sap training in delhi
    sap course in delhi

    ReplyDelete
  47. Thanks for giving me the time to share such nice information. Thanks for sharing.
    Data Science Course

    ReplyDelete
  48. 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
  49. Attend The Artificial Intelligence course From ExcelR. Practical Artificial Intelligence course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Artificial Intelligence course.
    Artificial Intelligence course

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

    ReplyDelete

  51. 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
  52. 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
  53. I like how this article is written. Your points are sound, original, fresh and interesting. This information has been made so clear there's no way to misunderstand it. Thank you.
    Best Data Science training in Mumbai

    Data Science training in Mumbai


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

    ReplyDelete
  56. 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
  57. Hey, i liked reading your article. You may go through few of my creative works here
    Jobs.fsoma
    Lucillerickman

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

    ReplyDelete
  60. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  61. Appsinvo is the Mobile App Development Company. With the help of our team passion and hard work we have come a long way and many milestones are still to achieve in the coming days. We serve clients ranging from startups, SMEs to large enterprises. We build the applications as per the clients’ requirements but we give them a different touch by using the trendy designs, latest technologies and agile methodologies
    Mobile App development company in Asia
    Top Mobile App Development Company
    Top Mobile App Development Company in India
    Top Mobile App Development Company in Noida
    Mobile App Development Company in Delhi
    Top Mobile App Development Companies in Australia
    Top Mobile App Development Company in Qatar
    Top Mobile App Development Company in kuwait
    Top Mobile App Development Companies in Sydney
    Mobile App Development Company in Europe
    Mobile App Development Company in Dubai

    ReplyDelete
  62. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  63. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  64. 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
  65. I have bookmarked your Website since this site contains important data in it. You rock for guardianship incredible stuff. I am a lot of grateful of this site.

    Python Training Institute in Delhi
    Python Training Course in Delhi
    Python Training Center in Delhi

    ReplyDelete
  66. We provide valuable & economical digital solutions for the customers in areas such as website designing, development & maintenance, graphic designing, digital marketing and mobile applications development. Codeaxia provides best solutions for -
    Ecommerce Web Development
    SEO Services
    Social Media Marketing

    ReplyDelete
  67. 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
  68. 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
  69. 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
  70. 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
  71. Webocity is best website designing company in delhi , Best Website development company in Delhi, We Offer Best Digital Marketing services in Delhi.

    ReplyDelete
  72. Thanks for this blog. It really provides awesome information to all readers. keep it up and keep posting these types of blogs on digital marketing services, it's really helpful for those users who search for the best digital marketing company in Delhi.

    ReplyDelete
  73. 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
  74. 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
  75. thank you for useful information.
    keep sharing.

    50% Off on Selected Imported Furniture for Living Room, Dining, Office. Cherrypick India, the best place to buy furniture in Bangalore

    Best furniture stores in Bangalore
    Marble Dining Table
    Leather Sofa Set
    Fabric Sofa

    ReplyDelete
  76. 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
  77. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
    Best Data Science Courses in Hyderabad

    ReplyDelete
  78. 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
  79. 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
  80. 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
  81. 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
  82. Thanks for posting the best information and the blog is very informative.python course in Bangalore

    ReplyDelete
  83. 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
  84. Outstanding blog appreciating your endless efforts in coming up with an extraordinary content. Which perhaps motivates the readers to feel excited in grasping the subject easily. This obviously makes every readers to thank the blogger and hope the similar creative content in future too.
    360DigiTMG Data Analytics Course

    ReplyDelete
  85. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support in this area. same as your blog i found another one Mobile Marketing .Actually I was looking for the same information on internet for Mobile Marketing and came across your blog. I am impressed by the information that you have on this blog. Thanks once more for all the details.

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

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

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

    by cognex AWS Training in chennai

    ReplyDelete
  89. 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
  90. 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
  91. 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
  92. 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
  93. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....

    deep learning course in delhi

    ReplyDelete
  94. 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
  95. 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.