Reusing images

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

7th November 2015
admin-marketing

Switch code for this example

RepeatImage.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/21720802/how-to-make-text-invisible-in-an-existing-pdf
  4.  *
  5.  * In this example, we take an image that is present in the background,
  6.  * and we add the same image (by its reference) to the foreground so that
  7.  * it covers the OCR'd text.
  8.  */
  9. package sandbox.images;
  10.  
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.pdf.PRIndirectReference;
  14. import com.itextpdf.text.pdf.PdfDictionary;
  15. import com.itextpdf.text.pdf.PdfName;
  16. import com.itextpdf.text.pdf.PdfReader;
  17. import com.itextpdf.text.pdf.PdfStamper;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22.  
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class RepeatImage {
  27.  
  28.     public static final String SRC = "resources/pdfs/chinese.pdf";
  29.     public static final String DEST = "results/images/chinese.pdf";
  30.    
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new RepeatImage().manipulatePdf(SRC, DEST);
  35.     }
  36.    
  37.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  38.         PdfReader reader = new PdfReader(src);
  39.         // We assume that there's a single large picture on the first page
  40.         PdfDictionary page = reader.getPageN(1);
  41.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  42.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  43.         PdfName imgName = xobjects.getKeys().iterator().next();
  44.         Image img = Image.getInstance((PRIndirectReference)xobjects.getAsIndirectObject(imgName));
  45.         img.setAbsolutePosition(0, 0);
  46.         img.scaleAbsolute(reader.getPageSize(1));
  47.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  48.         stamper.getOverContent(1).addImage(img);
  49.         stamper.close();
  50.         reader.close();
  51.     }
  52.  
  53. }
ReuseImage.java
  1. package sandbox.images;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Image;
  6. import com.itextpdf.text.PageSize;
  7. import com.itextpdf.text.pdf.PRStream;
  8. import com.itextpdf.text.pdf.PdfDictionary;
  9. import com.itextpdf.text.pdf.PdfName;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import com.itextpdf.text.pdf.parser.PdfImageObject;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ReuseImage {
  22.  
  23.     public static final String SRC = "resources/pdfs/single_image.pdf";
  24.     public static final String DEST = "results/images/image_on_A4.pdf";
  25.  
  26.     public static void main(String[] args) throws DocumentException, IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ReuseImage().manipulatePdf(SRC, DEST);
  30.     }
  31.    
  32.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  33.         PdfReader reader = new PdfReader(src);
  34.         // We assume that there's a single large picture on the first page
  35.         PdfDictionary page = reader.getPageN(1);
  36.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  37.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  38.         PdfName imgRef = xobjects.getKeys().iterator().next();
  39.         PRStream imgStream = (PRStream) xobjects.getAsStream(imgRef);
  40.         // We now create a new Image object based on the bytes in the stream
  41.         PdfImageObject imgObject = new PdfImageObject(imgStream);
  42.         reader.close();
  43.         Image img = Image.getInstance(imgObject.getImageAsBytes());
  44.         img.scaleToFit(842, 595);
  45.         img.setAbsolutePosition((842 - img.getScaledWidth()) / 2, (595 - img.getScaledHeight()) / 2);
  46.         // We create a new document with the correct size
  47.         Document document = new Document(PageSize.A4.rotate());
  48.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  49.         document.open();
  50.         document.add(img);
  51.         document.close();
  52.     }
  53. }
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