iText « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ iText ’

Using iText PdfStamper to insert Images in PDFs while preserving Acroforms

Posted by Vivek Krishna on November 29th, 2010

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 PdfStamper 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 this page. This was the perfect solution we were looking for.

Our service method looked something like

def populateFieldsAndSetImage(byte[] pdf){
        PdfReader reader = new PdfReader(pdf)
        ByteArrayOutputStream output = new ByteArrayOutputStream()
        def stamper = new PdfStamper(reader, output)
        AcroFields form= stamper.getAcroFields()
        form.setField('Address', 'Noida, India - 201301') //Set the form field
        PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages())
        Image image = Image.getInstance(new URL("Absolute URL to the image to be embedded"))

        image.setAbsolutePosition(450,650)
        image.scaleAbsolute(200,200)
        content.addImage(image)
        reader.close()
        stamper.close()
        return output.toByteArray() //returns the Byte Array of the PDF contents
    }

And the calling method had code which looked like this :

        File file = new File('/home/vivek/filledForm.pdf') //File to which the filled pdf will be written
        file.bytes = populateFieldsAndSetImage(new File('/home/vivek/form.pdf').bytes) // set the bytes of filledForm.pdf with the byte array returned by populateFieldsAndSetImage()

The PDF had pre filled forms and the identification image also got inserted.

Hope this helps.
Vivek

http://in.linkedin.com/in/svivekkrishna

  • Share/Bookmark
Posted in Java tools

Handling Password Protected Pdf with PdfReader

Posted by Hitesh Bhatia on August 19th, 2010

In one of our grails project , we had to attach cover to Pdf file . But since some of pdf’s uploaded were password protected.

To handle this scenario we added bouncyCastle.jar , so our version of iText was able to handle password protected pdf .
And Then to check whether pdf is password protected or not , we used “Boolean isOpenedWithFullPermission()” method.

PdfReader pdf = PdfReader("filePath")
Boolean editable = pdf.isOpenedWithFullPermissions()
 
if(editable){
//attach Cover
}else {
//skip  cover
}

As “isOpenedWithFullPermissions()” returns a Boolean variable ,
it was easy for us to recognize whether we would be able to edit (i.e attach cover in our case) depending on its output. (i,e true or false)
 

_________________________________
Hitesh Bhatia
Mail
LinkedIn,Facebook,Twitter
_________________________________

  • Share/Bookmark
Posted in Grails, Groovy