Replacing PDF objects

Tags: inspect PDFstream objectextract contentreplace contentiText 5
Files: 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/21617218/itext-or-itextsharp-rudimentary-text-edit
 * 
 * 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 sandbox.stamper;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ReplaceStream {
    public static final String SRC = "resources/pdfs/hello.pdf";
    public static final String DEST = "results/stamper/hello_changed.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReplaceStream().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);
        if (object instanceof PRStream) {
            PRStream stream = (PRStream)object;
            byte[] data = PdfReader.getStreamBytes(stream);
            stream.setData(new String(data).replace("Hello World", "HELLO WORLD").getBytes());
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
    }
}
File nameRaw URLUpdated
ReplaceStream.javaReplaceStream.java2015-11-08 3:54 pm
Resources: 
File nameRaw URLUpdated
hello.pdfhello.pdf2015-10-11 3:52 pm
Results: 
File nameRaw URLUpdated
cmp_hello_changed.pdfcmp_hello_changed.pdf2015-11-08 3:56 pm