Background images

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

31st May 2016
iText PDF

Switch code for this example

BackgroundImage.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 code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/21731027/backgroundimage-in-landscape-and-cover-whole-pdf-with-itextsharp
  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.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class BackgroundImage extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/images/background_image.pdf";
  31.     public static final String IMAGE = "./src/test/resources/img/berlin2013.jpg";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new BackgroundImage().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  42.         // Note that it is not necessary to create new PageSize object,
  43.         // but for testing reasons (connected to parallelization) we call constructor here
  44.         PageSize pageSize = new PageSize(PageSize.A4).rotate();
  45.         Document doc = new Document(pdfDoc, pageSize);
  46.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  47.         canvas.addImage(ImageDataFactory.create(IMAGE), pageSize, false);
  48.         doc.add(new Paragraph("Berlin!"));
  49.         doc.close();
  50.     }
  51. }
BackgroundTransparent.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 code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/27241731/change-background-image-in-itext-to-watermark-or-alter-opacity-c-sharp-asp-net
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class BackgroundTransparent extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/images/background_transparent.pdf";
  33.     public static final String IMAGE = "./src/test/resources/img/berlin2013.jpg";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new BackgroundTransparent().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  44.  
  45.         // Note that it is not necessary to create new PageSize object,
  46.         // but for testing reasons (connected to parallelization) we call constructor here
  47.         PageSize pageSize = new PageSize(PageSize.A4).rotate();
  48.         Document doc = new Document(pdfDoc, pageSize);
  49.  
  50.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  51.         ImageData image = ImageDataFactory.create(IMAGE);
  52.         canvas.saveState();
  53.         PdfExtGState state = new PdfExtGState();
  54.         state.setFillOpacity(0.6f);
  55.         canvas.setExtGState(state);
  56.         canvas.addImage(image, 0, 0, pageSize.getWidth(), false);
  57.         canvas.restoreState();
  58.  
  59.         doc.add(new Paragraph("Berlin!"));
  60.  
  61.         doc.close();
  62.     }
  63. }
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