Editing PDF text using Java

replace text in pdf using java
edit pdf using java
pdf library java
itext pdf java
pdfbox documentation
how to write pdf file in java
itext replace text existing pdf
itext java

Is there a way I can edit a PDF from Java? I have a PDF document which contains placeholders for text that I need to be replaced using Java, but all the libraries that I saw created PDF from scratch and small editing functionality. Is there anyway I can edit a PDF or is this impossible?

You can do it with iText. I tested it with following code. It adds a chunk of text and a red circle over each page of an existing PDF.

/* requires itextpdf-5.1.2.jar or similar */
import java.io.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

public class AddContentToPDF {

    public static void main(String[] args) throws IOException, DocumentException {

        /* example inspired from "iText in action" (2006), chapter 2 */

        PdfReader reader = new PdfReader("C:/temp/Bubi.pdf"); // input PDF
        PdfStamper stamper = new PdfStamper(reader,
          new FileOutputStream("C:/temp/Bubi_modified.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.setFontAndSize(bf, 10);    // set font and size
            over.setTextMatrix(107, 740);   // set x,y position (0,0 is at the bottom left)
            over.showText("I can write at page " + i);  // set text

            // draw a red circle
            over.setRGBColorStroke(0xFF, 0x00, 0x00);
            over.ellipse(250, 450, 350, 550);



Editing PDF text using Java, The Apache PDFBox™ library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of​  Edit PDF Text, Image & Pages So Easy As In Word. Top Recommended PDF Editor!

Take a look at iText and this sample code

Apache PDFBox, (PdfReader reader, OutputStream os, char pdfVersion) Starts the process of adding extra content to an existing PDF document. Save Time Editing PDF Documents Online. No Installation Needed. Try Now!

Take a look at aspose and this sample code

PdfStamper, software Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. Automated Platform Lets You to Create, Edit & Sign Forms Online - Try Free Now

I modified the code found a bit and it was working as follows

public class Principal {
public static final String SRC = "C:/tmp/244558.pdf";
public static final String DEST = "C:/tmp/244558-2.pdf";

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    new Principal().manipulatePdf(SRC, DEST);

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary dict = reader.getPageN(1);
    PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
    PdfArray refs = null;
    if (dict.get(PdfName.CONTENTS).isArray()) {
        refs = dict.getAsArray(PdfName.CONTENTS);
    } else if (dict.get(PdfName.CONTENTS).isIndirect()) {
        refs = new PdfArray(dict.get(PdfName.CONTENTS));
    for (int i = 0; i < refs.getArrayList().size(); i++) {
        PRStream stream = (PRStream) refs.getDirectObject(i);
        byte[] data = PdfReader.getStreamBytes(stream);
        stream.setData(new String(data).replace("NULA", "Nulo").getBytes());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));


PDFbox is the free version?, Sample Java code to programmatically edit an existing PDF document at the object level by using the Consult legal.txt regarding legal and license information. Fill & Edit PDF with PDFfiller Google Docs App. Fast, Easy & Secure. Try Now!

You can do limited editing with Itext but PDF is an end file format so you cannot do anything too complex. I wrote an article explaining some of the limitations: PDF format and style information.

Pdfbox, This tutorial explains how to modify existing PDF files in Java using IText. used modifications - stamping an existing PDF with text or images. Is there a way I can edit a PDF from Java? I have a PDF document which contains placeholders for text that I need to be replaced using Java, but all the libraries that I saw created PDF from scratc

Java PDF Editor: Low-level API to Edit PDF, how to export Vietnamese text to PDF using iText.Can you help me? Read more. Show less. ReplyDuration: 10:00 Posted: Apr 30, 2013 If you want to “edit” a PDF, it’s advised that you change the original source of the document and remake the PDF. If the original document was written using Microsoft Word, change the Word document, and make the PDF from the new version of the Word document.

Java IText: Modifying Existing PDF Documents, PDFBox, an open-source Java tool for working with PDF documents, has merging, debugging, converting text to PDF and PDF to an image. PDFs are not meant for editing, certainly not for altering the textual contents. Adobe's Acrobat Pro can do that, but I'm not aware of any free Java libraries for that. My last post in this topic suggests a solution for DOC files. It'll probably be easier if you were to do this using RTF files.

Java and PDF - 06 - Editing (PdfStamper), Last few days I was trying to modify some PDF file using iText library.Major requirement was to append some dynamic data to a PDF.So my first try was to  Below are the steps and example to use iText. 1.Create PdfReader instance. 2.Create PdfStamper instance. 3.Create BaseFont instance. 4.Get the number of pages in pdf. 5.Iterate the pdf through pages. 6.Contain the pdf data using PdfContentByte. 7.Set text font and size.

  • This example overlays the page with your new text and the ellipse. Is there a way to modify the text in place? Is there way to search for, say a token, and replace it with my text rather than to overlay it?
  • error....... Fatal signal 7 (SIGBUS), code 2, fault addr 0xa290903f in tid 25590 (om.pdfgenerator)
  • I have failed editing text in the existing pdf. Extracted text is a bunch of values similar to: "(>) Tj". Tutorial link: developers.itextpdf.com/examples/…
  • @IgorG. concerning that "Tutorial link": You surely have seen the link at the top of the JavaDoc there. It points to this SO answer in which Bruno in particular states that if your PDFs are relatively simple you can use that code but that in real life, PDFs are never that simple... That you have failed editing text in the existing pdf, therefore, is only to be expected! If you only want to edit very specific documents, create an own question from that and supply examples. Don't expect a generic solution, though!
  • Hi, is there any way to do this in PHP?
  • Unfortunately this example requires fields in the existing PDF. See my answer.
  • both links are broken
  • Archive of the first link: web.archive.org/web/20130625054957/http://www.aspose.com/docs/… And the second one: web.archive.org/web/20130330113945/http://www.aspose.com/docs/… I also found this: products.aspose.com/pdf/java
  • That kind of text replacement works only under very specific circumstances (fonts in the pdf must use an ASCII-like encoding and must not be embedded as incomplete subsets only; furthermore the generator must not have applied kerning or a similar technique that splits text lines into separate chunks). And you also only change contents of page content streams.