Replacing PDF objects

This is a code example of iText PDF, discover more.

31st May 2016

Switch code for this example

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV


 * Example written by Bruno Lowagie in answer to:

* This is only a partial answer. It's a quick and dirty method showing how to * change a stream inside a PDF. Obviously, you'll have to detect words that are * stored in Form XObjects too, and you can seriously screw up the layout when * you manipulate the content stream as is done in this example. */ package com.itextpdf.samples.sandbox.stamper; import com.itextpdf.kernel.pdf.*; import com.itextpdf.samples.GenericTest; import com.itextpdf.test.annotations.type.SampleTest; import org.junit.experimental.categories.Category; import; @Category(SampleTest.class) public class ReplaceStream extends GenericTest { public static final String DEST = "./target/test/resources/sandbox/stamper/replace_stream.pdf"; public static final String SRC = "./src/test/resources/pdfs/hello.pdf"; public static void main(String[] args) throws Exception { File file = new File(DEST); file.getParentFile().mkdirs(); new ReplaceStream().manipulatePdf(DEST); } @Override protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST)); PdfPage page = pdfDoc.getFirstPage(); PdfDictionary dict = page.getPdfObject(); PdfObject object = dict.get(PdfName.Contents); if (object instanceof PdfStream) { PdfStream stream = (PdfStream) object; byte[] data = stream.getBytes(); stream.setData(new String(data).replace("Hello World", "HELLO WORLD").getBytes("UTF-8")); } pdfDoc.close(); } }


Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now