Replacing PDF objects

Tags: inspect PDFstream objectextract contentreplace contentiText 7
    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;
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);
        new ReplaceStream().manipulatePdf(DEST);
    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"));
File nameRaw URLUpdated
ReplaceStream.javaReplaceStream.java2016-08-09 10:09 am
File nameRaw URLUpdated
hello.pdfhello.pdf2016-08-04 11:45 am
File nameRaw URLUpdated
cmp_replace_stream.pdfcmp_replace_stream.pdf2016-08-09 10:11 am