Large Image examples

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

9th October 2015
admin-marketing

Switch code for this example

LargeImage1.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.Rectangle;
  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 LargeImage1 {
  22.  
  23.     public static final String SRC = "resources/pdfs/large_image.pdf";
  24.     public static final String DEST = "results/images/large_image1.pdf";
  25.    
  26.     public static void main(String[] args) throws DocumentException, IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new LargeImage1().manipulatePdf(SRC, DEST);
  30.     }
  31.    
  32.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  33.         PdfReader reader = new PdfReader(src);
  34.         // The width and the height of a PDF page may not exceed 14400 user units:
  35.         Rectangle rect = reader.getPageSize(1);
  36.         if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
  37.             System.out.println("The size of the PDF document is within the accepted limits");
  38.             System.exit(0);
  39.         }
  40.         // We assume that there's a single large picture on the first page
  41.         PdfDictionary page = reader.getPageN(1);
  42.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  43.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  44.         PdfName imgRef = xobjects.getKeys().iterator().next();
  45.         PRStream imgStream = (PRStream) xobjects.getAsStream(imgRef);
  46.         // We now create a new Image object based on the bytes in the stream
  47.         PdfImageObject imgObject = new PdfImageObject(imgStream);
  48.         Image img = Image.getInstance(imgObject.getImageAsBytes());
  49.         img.scaleToFit(14400, 14400);
  50.         img.setAbsolutePosition(0, 0);
  51.         reader.close();
  52.         // We create a new document with the correct size
  53.         Document document = new Document(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
  54.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  55.         document.open();
  56.         document.add(img);
  57.         document.close();
  58.     }
  59. }
LargeImage2.java
  1. package sandbox.images;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Image;
  5. import com.itextpdf.text.Rectangle;
  6. import com.itextpdf.text.pdf.PRIndirectReference;
  7. import com.itextpdf.text.pdf.PdfDictionary;
  8. import com.itextpdf.text.pdf.PdfName;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class LargeImage2 {
  20.  
  21.     public static final String SRC = "resources/pdfs/large_image.pdf";
  22.     public static final String DEST = "results/images/large_image2.pdf";
  23.    
  24.     public static void main(String[] args) throws DocumentException, IOException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new LargeImage2().manipulatePdf(SRC, DEST);
  28.     }
  29.    
  30.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  31.         PdfReader reader = new PdfReader(src);
  32.         // The width and the height of a PDF page may not exceed 14400 user units:
  33.         Rectangle rect = reader.getPageSize(1);
  34.         if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
  35.             System.out.println("The size of the PDF document is withing the accepted limits");
  36.             System.exit(0);
  37.         }
  38.         // We assume that there's a single large picture on the first page
  39.         PdfDictionary page = reader.getPageN(1);
  40.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  41.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  42.         PdfName imgName = xobjects.getKeys().iterator().next();
  43.         Image img = Image.getInstance((PRIndirectReference)xobjects.getAsIndirectObject(imgName));
  44.         img.scaleToFit(14400, 14400);
  45.         img.setAbsolutePosition(0, 0);
  46.         // We create a temporary file that will reuse the image
  47.         File tmp = File.createTempFile("large_image", ".pdf", new File("results"));
  48.         tmp.deleteOnExit();
  49.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tmp));
  50.         // We create a new first page and add the image
  51.         stamper.insertPage(1, new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
  52.         stamper.getOverContent(1).addImage(img);
  53.         stamper.close();
  54.         reader.close();
  55.         // We create a new file that only contains the new first page
  56.         reader = new PdfReader(tmp.getAbsolutePath());
  57.         reader.selectPages("1");
  58.         stamper = new PdfStamper(reader, new FileOutputStream(dest));
  59.         stamper.close();
  60.         reader.close();
  61.     }
  62. }
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