Reusing images

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

31st May 2016
admin-marketing

Switch code for this example

RepeatImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/21720802/how-to-make-text-invisible-in-an-existing-pdf
  11.  *
  12.  * In this example, we take an image that is present in the background,
  13.  * and we add the same image (by its reference) to the foreground so that
  14.  * it covers the OCR'd text.
  15.  */
  16. package com.itextpdf.samples.sandbox.images;
  17.  
  18. import com.itextpdf.kernel.color.Color;
  19. import com.itextpdf.kernel.pdf.*;
  20. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.border.SolidBorder;
  23. import com.itextpdf.layout.element.Image;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class RepeatImage extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/images/repeat_image.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/chinese.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new RepeatImage().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc);
  46.         PdfDictionary pageDict = pdfDoc.getFirstPage().getPdfObject();
  47.         PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
  48.         PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
  49.         PdfName imgRef = pageXObjects.keySet().iterator().next();
  50.         PdfStream imgStream = pageXObjects.getAsStream(imgRef);
  51.         PdfImageXObject imgObject = new PdfImageXObject(imgStream);
  52.         Image image = new Image(imgObject);
  53.         image.setFixedPosition(0, 0);
  54.         image.setBorder(new SolidBorder(Color.BLACK, 5));
  55.         image.scaleAbsolute(pdfDoc.getFirstPage().getPageSize().getWidth(), pdfDoc.getFirstPage().getPageSize().getHeight());
  56.         doc.add(image);
  57.         doc.close();
  58.     }
  59. }
ReuseImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.images;
  9.  
  10. import com.itextpdf.kernel.geom.PageSize;
  11. import com.itextpdf.kernel.pdf.*;
  12. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.Image;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class ReuseImage extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/images/reuse_image.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/single_image.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ReuseImage().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  36.  
  37.         PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
  38.         // Note that it is not necessary to create new PageSize object,
  39.         // but for testing reasons (connected to parallelization) we call constructor here
  40.         Document doc = new Document(resultDoc, new PageSize(PageSize.A4).rotate());
  41.  
  42.         PdfDictionary pageDict = srcDoc.getFirstPage().getPdfObject();
  43.         PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
  44.         PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
  45.         PdfName imgRef = pageXObjects.keySet().iterator().next();
  46.         PdfStream imgStream = pageXObjects.getAsStream(imgRef);
  47.         PdfImageXObject imgObject = new PdfImageXObject(imgStream.copyTo(resultDoc));
  48.  
  49.         srcDoc.close();
  50.  
  51.         Image image = new Image(imgObject);
  52.         image.scaleToFit(842, 595);
  53.         image.setFixedPosition((842 - image.getImageScaledWidth()) / 2, (595 - image.getImageScaledHeight()) / 2);
  54.         doc.add(image);
  55.  
  56.         doc.close();
  57.     }
  58. }
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