Blog Archive

Modify existing PDF document using iText - Overlay images and Text

iText is very popular Java library for reading and manipulating PDF documents. In this tutorial we will take an existing document and overlay an image. The image can be put over the document or under it just like a watermark. Also we will add some text to the existing document. We can achieve all this with the help of the PdfStamper class.

PdfStamper applies extra content to the pages of a PDF document. This extra content can be all the objects allowed in PdfContentByte including pages from other Pdfs. The original PDF will keep all the interactive elements including bookmarks, links and form fields. Read more about the PdfStamper here - http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfStamper.html

Before image of the PDF document

Modify existing PDF document using iText - add watermark and Text

After image of the PDF document

Modify existing PDF document using iText - add watermark and Text

As you can see one image is over the text and the other one is under it. Also there is text added for the page number as the bottom right corner. Here is the source code ...
package com.as400samplecode;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;

public class PdfStamperExample {

    public static void main(String[] args) {
        try {
            PdfReader pdfReader = new PdfReader("data/FormW4.pdf");

            PdfStamper pdfStamper = new PdfStamper(pdfReader,
                    new FileOutputStream("data/FormW4-Stamped.pdf"));

            Image image = Image.getInstance("data/Approved.png");

            for(int i=1; i<= pdfReader.getNumberOfPages(); i++){

                //put content under
                PdfContentByte content = pdfStamper.getUnderContent(i);
                image.setAbsolutePosition(100f, 150f);
                content.addImage(image);

                //put content over
                content = pdfStamper.getOverContent(i);
                image.setAbsolutePosition(300f, 150f);
                content.addImage(image);

                //Text over the existing page
                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                        BaseFont.WINANSI, BaseFont.EMBEDDED);
                content.beginText();
                content.setFontAndSize(bf, 18);
                content.showTextAligned(PdfContentByte.ALIGN_LEFT,"Page No: " + i,430,15,0);
                content.endText();

            }

            pdfStamper.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

20 comments :

  1. Thank you very much for your share. It's very helpful for me.

    ReplyDelete
  2. wow this is so much simpler then the way I was trying to do it. Thank you for this.

    ReplyDelete
  3. This information is very use full for all Android Application Developers.this code to overlay images and Text.

    ReplyDelete
  4. Here is another way for that, edit PDF text and images, markup PDF content with highlight, underline, strikethrough and more.

    http://youtu.be/vYWDa_WBPLQ

    ReplyDelete
  5. Editing a PDF is not easy as it sounds to be. You must have a PDF editing Software in advance, but mostly of them are quite expensive. Take Adobe Acrobat Professional for example, it charges more than $400 which may exceed your budget. I am a medical consultant and I regularly needs to edit/format my PDF documents and fillable forms to email my patients.

    I uses a Fiverr professional called ChinEtti to do all my PDF Edits for $5. YESSS, just for $5 bucks! He is so professional and literally can do any PDF editing job. ChinEtti deliver fast with very high quality. I am extremely happy with his work up-to now and would recommend him to anyone who needs a professional PDF editing job done fast.

    You can check out his work here --> http://goo.gl/U2Snkz

    ReplyDelete
  6. If I add an image over content of pdf using stamper. is it possible to revert back again to original pdf using java code?
    Please reply.

    ReplyDelete
  7. The statement of purpose phd is my most favorite platform where i learn a lot to write best thesis gradually.

    ReplyDelete
  8. is it possible to revert back or remove added overlay text?

    ReplyDelete
  9. You can also read an interesting c# acrobat remove text from pdf on this page http://www.rasteredge.com/how-to/csharp-imaging/pdf-text-edit-delete/

    ReplyDelete
  10. c# erase pdf pages will easily redat page for your product, download and try this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-redact-page/

    ReplyDelete
  11. you may test c# add editable text box to pdf on rasteredge and download this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text/

    ReplyDelete
  12. The PDF files are mostly used for using the scanned pages in the documents. You can easily insert the scanned pages, using your scanner to be a part of the PDF document. Adobe CS5.5 Production Premium

    ReplyDelete