Edit/Modify existing PDF using Java(iText)

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

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

public class EditExistingPDF {

public static void main(String[] args) throws IOException, DocumentException {
PdfReader reader = new PdfReader("C:\\signed.pdf"); // input PDF PdfStamper stamper = new PdfStamper(reader,new FileOutputStream( "C:\\test.pdf"));
// output PDF
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
// set font
// loop on pages (1-based)
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
// get object for writing over the existing content;
// you can also use getUnderContent for writing in the bottom
// layer
PdfContentByte over = stamper.getOverContent(i);
// write text
over.beginText();
over.setFontAndSize(bf, 10); // set font and size
over.setTextMatrix(107, 400); // set x,y position (0,0 is at
// the
// bottom left)
over.showText("Hello World"); // set text
over.endText();
}
stamper.close();

}

}

Comments

Popular posts from this blog

Verify Digital Signature in PDF using Java(iText) and cacerts

Steps to create SOAP Web service using Eclipse