How to modify existing PDF document using java iText library

In this tutorial you will learn how to modify the existing PDF document using iText java library. IText can modify existing PDF files in many ways. You can modify existing PDF document and can add header, footer or watermark in PDF document.

Please note we are using the iText version 5 for this example.

To modify the existing PDF documents you have to follow below steps.

First you have to read the PDF document using PdfReader object.

Second use the PdfStamper to modify the PDF document.

Below is the example of code which shows you how to modify the existing PDF documents.

import java.awt.Color;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class PDFModifyExample {
    public static void main(String[] args) throws IOException, DocumentException {
        PdfReader pdfReader = new PdfReader("PDF_Hello_World_Example.pdf");
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("PDF_Hello_World_Modified_Example.pdf"));
        PdfContentByte underContent = pdfStamper.getUnderContent(1);
        BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
        underContent.setFontAndSize(bf, 12);
        underContent.showTextAligned(Element.ALIGN_TOP, " This is footer ", 0, 2, 0);