Raw images

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

31st May 2016
iText PDF

Switch code for this example

RawImages.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 sample is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/27070222/how-to-create-unique-images-with-image-getinstance
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.geom.PageSize;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Image;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class RawImages extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/images/raw_images.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new RawImages().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         // Note that it is not necessary to create new PageSize object,
  41.         // but for testing reasons (connected to parallelization) we call constructor here
  42.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  43.  
  44.         Image gray = new Image(ImageDataFactory.create(1, 1, 1, 8,
  45.                 new byte[]{(byte) 0x80}, null));
  46.         gray.scaleToFit(30, 30);
  47.  
  48.         Image red = new Image(ImageDataFactory.create(1, 1, 3, 8,
  49.                 new byte[]{(byte) 255, (byte) 0, (byte) 0}, null));
  50.         red.scaleToFit(30, 30);
  51.  
  52.         Image green = new Image(ImageDataFactory.create(1, 1, 3, 8,
  53.                 new byte[]{(byte) 0, (byte) 255, (byte) 0}, null));
  54.         green.scaleToFit(30, 30);
  55.  
  56.         Image blue = new Image(ImageDataFactory.create(1, 1, 3, 8,
  57.                 new byte[]{(byte) 0, (byte) 0, (byte) 255}, null));
  58.         blue.scaleToFit(30, 30);
  59.  
  60.         Image cyan = new Image(ImageDataFactory.create(1, 1, 4, 8,
  61.                 new byte[]{(byte) 255, (byte) 0, (byte) 0, (byte) 0}, null));
  62.         cyan.scaleToFit(30, 30);
  63.  
  64.         Image magenta = new Image(ImageDataFactory.create(1, 1, 4, 8,
  65.                 new byte[]{(byte) 0, (byte) 255, (byte) 0, (byte) 0}, null));
  66.         magenta.scaleToFit(30, 30);
  67.  
  68.         Image yellow = new Image(ImageDataFactory.create(1, 1, 4, 8,
  69.                 new byte[]{(byte) 0, (byte) 0, (byte) 255, (byte) 0}, null));
  70.         yellow.scaleToFit(30, 30);
  71.  
  72.         Image black = new Image(ImageDataFactory.create(1, 1, 4, 8,
  73.                 new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 255}, null));
  74.         black.scaleToFit(30, 30);
  75.  
  76.         doc.add(gray);
  77.         doc.add(red);
  78.         doc.add(green);
  79.         doc.add(blue);
  80.         doc.add(cyan);
  81.         doc.add(magenta);
  82.         doc.add(yellow);
  83.         doc.add(black);
  84.  
  85.         doc.close();
  86.     }
  87. }
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