Reusing images

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

31st May 2016
admin-marketing

Switch code for this example

RepeatImage.java
/*

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

*/

/**
 * This code sample was written by Bruno Lowagie in answer to this question:
 * http://stackoverflow.com/questions/21720802/how-to-make-text-invisible-in-an-existing-pdf
 * 

* In this example, we take an image that is present in the background, * and we add the same image (by its reference) to the foreground so that * it covers the OCR'd text. */ package com.itextpdf.samples.sandbox.images; import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.pdf.*; import com.itextpdf.kernel.pdf.xobject.PdfImageXObject; import com.itextpdf.layout.Document; import com.itextpdf.layout.border.SolidBorder; import com.itextpdf.layout.element.Image; import com.itextpdf.samples.GenericTest; import com.itextpdf.test.annotations.type.SampleTest; import org.junit.experimental.categories.Category; import java.io.File; @Category(SampleTest.class) public class RepeatImage extends GenericTest { public static final String DEST = "./target/test/resources/sandbox/images/repeat_image.pdf"; public static final String SRC = "./src/test/resources/pdfs/chinese.pdf"; public static void main(String[] args) throws Exception { File file = new File(DEST); file.getParentFile().mkdirs(); new RepeatImage().manipulatePdf(DEST); } @Override protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); PdfDictionary pageDict = pdfDoc.getFirstPage().getPdfObject(); PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources); PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject); PdfName imgRef = pageXObjects.keySet().iterator().next(); PdfStream imgStream = pageXObjects.getAsStream(imgRef); PdfImageXObject imgObject = new PdfImageXObject(imgStream); Image image = new Image(imgObject); image.setFixedPosition(0, 0); image.setBorder(new SolidBorder(Color.BLACK, 5)); image.scaleAbsolute(pdfDoc.getFirstPage().getPageSize().getWidth(), pdfDoc.getFirstPage().getPageSize().getHeight()); doc.add(image); doc.close(); } }

ReuseImage.java
/*

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

*/

package com.itextpdf.samples.sandbox.images;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;

import org.junit.experimental.categories.Category;

import java.io.File;

@Category(SampleTest.class)
public class ReuseImage extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/images/reuse_image.pdf";
    public static final String SRC = "./src/test/resources/pdfs/single_image.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReuseImage().manipulatePdf(DEST);
    }

    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));

        PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
        // Note that it is not necessary to create new PageSize object,
        // but for testing reasons (connected to parallelization) we call constructor here
        Document doc = new Document(resultDoc, new PageSize(PageSize.A4).rotate());

        PdfDictionary pageDict = srcDoc.getFirstPage().getPdfObject();
        PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
        PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
        PdfName imgRef = pageXObjects.keySet().iterator().next();
        PdfStream imgStream = pageXObjects.getAsStream(imgRef);
        PdfImageXObject imgObject = new PdfImageXObject(imgStream.copyTo(resultDoc));

        srcDoc.close();

        Image image = new Image(imgObject);
        image.scaleToFit(842, 595);
        image.setFixedPosition((842 - image.getImageScaledWidth()) / 2, (595 - image.getImageScaledHeight()) / 2);
        doc.add(image);

        doc.close();
    }
}
Contact

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