{"id":2171,"date":"2010-11-29T17:58:15","date_gmt":"2010-11-29T12:28:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2171"},"modified":"2017-04-24T15:53:21","modified_gmt":"2017-04-24T10:23:21","slug":"using-itext-pdfstamper-to-insert-images-in-pdfs-while-preserving-acroforms","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-itext-pdfstamper-to-insert-images-in-pdfs-while-preserving-acroforms\/","title":{"rendered":"Using iText PdfStamper to insert Images in PDFs while preserving Acroforms"},"content":{"rendered":"<p>We had a requirement that a template PDF which had Acroform fields had to be pre-populated (like name, email id etc) and sent to the end user so that he\/she only needed to fill in the details which were expected to be filled by him\/her.<\/p>\n<p>After going through the iText library, we found that the <strong>PdfStamper<\/strong> class was the best way to go about it. However, we also discovered that we needed to embed a data matrix image in the document so that it can be identified by scanning the printed version of the document which is received back from the end user. After pondering on how to do it, we stumbled across <a href=\"http:\/\/threebit.net\/mail-archive\/itext-questions\/msg04434.html\" target=\"_blank\">this page<\/a>. This was the perfect solution we were looking for.<\/p>\n<p>Our service method looked something like<\/p>\n<p>[java]<br \/>\ndef populateFieldsAndSetImage(byte[] pdf){<br \/>\n        PdfReader reader = new PdfReader(pdf)<br \/>\n        ByteArrayOutputStream output = new ByteArrayOutputStream()<br \/>\n        def stamper = new PdfStamper(reader, output)<br \/>\n        AcroFields form= stamper.getAcroFields()<br \/>\n        form.setField(&#8216;Address&#8217;, &#8216;Noida, India &#8211; 201301&#8217;) \/\/Set the form field<br \/>\n        PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages())<br \/>\n        Image image = Image.getInstance(new URL(&quot;Absolute URL to the image to be embedded&quot;))<\/p>\n<p>        image.setAbsolutePosition(450,650)<br \/>\n        image.scaleAbsolute(200,200)<br \/>\n        content.addImage(image)<br \/>\n        reader.close()<br \/>\n        stamper.close()<br \/>\n        return output.toByteArray() \/\/returns the Byte Array of the PDF contents<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>And the calling method had code which looked like this :<\/p>\n<p>[java]<br \/>\n        File file = new File(&#8216;\/home\/vivek\/filledForm.pdf&#8217;) \/\/File to which the filled pdf will be written<br \/>\n        file.bytes = populateFieldsAndSetImage(new File(&#8216;\/home\/vivek\/form.pdf&#8217;).bytes) \/\/ set the bytes of filledForm.pdf with the byte array returned by populateFieldsAndSetImage()<br \/>\n[\/java]<\/p>\n<p>The PDF had pre-filled forms and the identification image also got inserted.<\/p>\n<p>Hope this helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We had a requirement that a template PDF which had Acroform fields had to be pre-populated (like name, email id etc) and sent to the end user so that he\/she only needed to fill in the details which were expected to be filled by him\/her. After going through the iText library, we found that the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":53},"categories":[1],"tags":[462,463,367,292],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2171"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2171"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2171\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}