Large Image examples

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

31st May 2016
admin-marketing

Switch code for this example

LargeImage1.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.geom.Rectangle;
  12. import com.itextpdf.kernel.pdf.*;
  13. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23.  
  24. @Category(SampleTest.class)
  25. public class LargeImage1 extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/images/large_image1.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/large_image.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new LargeImage1().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  38.         Rectangle rect = srcDoc.getFirstPage().getPageSize();
  39.         if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
  40.             System.out.println("The size of the PDF document is within the accepted limits");
  41.             System.exit(0);
  42.         }
  43.         PdfDictionary pageDict = srcDoc.getFirstPage().getPdfObject();
  44.         PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
  45.         PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
  46.         PdfName imgRef = pageXObjects.keySet().iterator().next();
  47.         PdfStream imgStream = pageXObjects.getAsStream(imgRef);
  48.  
  49.         //create instance for new document
  50.         PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
  51.  
  52.         PdfImageXObject imgObject = new PdfImageXObject(imgStream.copyTo(resultDoc));
  53.  
  54.         Image image = new Image(imgObject);
  55.         image.scaleToFit(14400, 14400);
  56.         image.setFixedPosition(0, 0);
  57.  
  58.         srcDoc.close();
  59.  
  60.         Document doc = new Document(resultDoc, new PageSize(image.getImageScaledWidth(), image.getImageScaledHeight()));
  61.         doc.add(image);
  62.         doc.close();
  63.     }
  64. }
LargeImage2.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.geom.Rectangle;
  12. import com.itextpdf.kernel.pdf.PdfDictionary;
  13. import com.itextpdf.kernel.pdf.PdfDocument;
  14. import com.itextpdf.kernel.pdf.PdfName;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfStream;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Image;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import java.io.File;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class LargeImage2 extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/images/large_image2.pdf";
  31.     public static final String SRC = "./src/test/resources/pdfs/large_image.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new LargeImage2().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfReader reader = new PdfReader(SRC);
  42.         reader.setCloseStream(false);
  43.  
  44.         File tmp = File.createTempFile("large_image", ".pdf", new File("."));
  45.         tmp.deleteOnExit();
  46.  
  47.         PdfDocument tempDoc = new PdfDocument(reader, new PdfWriter(tmp.getAbsolutePath()));
  48.         Rectangle rect = tempDoc.getFirstPage().getPageSize();
  49.  
  50.         if (rect.getWidth() < 14400 && rect.getHeight() < 14400) {
  51.             System.out.println("The size of the PDF document is within the accepted limits");
  52.             System.exit(0);
  53.         }
  54.  
  55.         PdfDictionary pageDict = tempDoc.getFirstPage().getPdfObject();
  56.         PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
  57.         PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
  58.         PdfName imgRef = pageXObjects.keySet().iterator().next();
  59.         PdfStream imgStream = pageXObjects.getAsStream(imgRef);
  60.         PdfImageXObject imgObject = new PdfImageXObject(imgStream);
  61.  
  62.         Image img = new Image(imgObject);
  63.         img.scaleToFit(14400, 14400);
  64.         img.setFixedPosition(0, 0);
  65.         tempDoc.addNewPage(1, new PageSize(img.getImageScaledWidth(), img.getImageScaledHeight()));
  66.         new Document(tempDoc)
  67.                 .add(img)
  68.                 .close();
  69.  
  70.         // Open temporary document only for reading
  71.         tempDoc = new PdfDocument(new PdfReader(tmp.getAbsolutePath()));
  72.  
  73.         PdfDocument resultDoc = new PdfDocument(new PdfWriter(DEST));
  74.         tempDoc.copyPagesTo(1, 1, resultDoc);
  75.  
  76.         resultDoc.close();
  77.         tempDoc.close();
  78.     }
  79. }
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