Raw images

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

1st November 2015
admin-marketing

Switch code for this example

RawImages.java
  1. /**
  2.  * This sample is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27070222/how-to-create-unique-images-with-image-getinstance
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.PageSize;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class RawImages {
  20.     public static final String DEST = "results/images/raw_images.pdf";
  21.    
  22.     public static void main(String[] args) throws DocumentException, IOException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new RawImages().createPdf(DEST);
  26.     }
  27.  
  28.     public void createPdf(String dest) throws IOException, DocumentException {
  29.         Document document = new Document(PageSize.A4.rotate());
  30.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  31.         document.open();
  32.         Image gray = Image.getInstance(1, 1, 1, 8, new byte[] { (byte)0x80 });
  33.         gray.scaleAbsolute(30, 30);
  34.         Image red = Image.getInstance(1, 1, 3, 8, new byte[] { (byte)255, (byte)0, (byte)0 });
  35.         red.scaleAbsolute(30, 30);
  36.         Image green = Image.getInstance(1, 1, 3, 8, new byte[] { (byte)0, (byte)255, (byte)0 });
  37.         green.scaleAbsolute(30, 30);
  38.         Image blue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte)0, (byte)0, (byte)255, });
  39.         blue.scaleAbsolute(30, 30);
  40.         Image cyan = Image.getInstance(1, 1, 4, 8, new byte[] { (byte)255, (byte)0, (byte)0, (byte)0 });
  41.         cyan.scaleAbsolute(30, 30);
  42.         Image magenta = Image.getInstance(1, 1, 4, 8, new byte[] { (byte)0, (byte)255, (byte)0, (byte)0 });
  43.         magenta.scaleAbsolute(30, 30);
  44.         Image yellow = Image.getInstance(1, 1, 4, 8, new byte[] { (byte)0, (byte)0, (byte)255, (byte)0 });
  45.         yellow.scaleAbsolute(30, 30);
  46.         Image black = Image.getInstance(1, 1, 4, 8, new byte[] { (byte)0, (byte)0, (byte)0, (byte)255 });
  47.         black.scaleAbsolute(30, 30);
  48.         document.add(gray);
  49.         document.add(red);
  50.         document.add(green);
  51.         document.add(blue);
  52.         document.add(cyan);
  53.         document.add(magenta);
  54.         document.add(yellow);
  55.         document.add(black);
  56.         document.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