Background images

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

1st November 2015
admin-marketing

Switch code for this example

BackgroundImage.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/21731027/backgroundimage-in-landscape-and-cover-whole-pdf-with-itextsharp
  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.Paragraph;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class BackgroundImage {
  23.     public static final String IMAGE = "resources/images/berlin2013.jpg";
  24.     public static final String DEST = "results/images/background_image.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new BackgroundImage().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document(PageSize.A4.rotate());
  33.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         document.add(new Paragraph("Berlin!"));
  36.         PdfContentByte canvas = writer.getDirectContentUnder();
  37.         Image image = Image.getInstance(IMAGE);
  38.         image.scaleAbsolute(PageSize.A4.rotate());
  39.         image.setAbsolutePosition(0, 0);
  40.         canvas.addImage(image);
  41.         document.close();
  42.     }
  43. }
BackgroundTransparent.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/27241731/change-background-image-in-itext-to-watermark-or-alter-opacity-c-sharp-asp-net
  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.Paragraph;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfGState;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class BackgroundTransparent {
  23.     public static final String IMAGE = "resources/images/berlin2013.jpg";
  24.     public static final String DEST = "results/images/background_transparent.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new BackgroundTransparent().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document(PageSize.A4.rotate());
  33.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         document.add(new Paragraph("Berlin!"));
  36.         PdfContentByte canvas = writer.getDirectContentUnder();
  37.         Image image = Image.getInstance(IMAGE);
  38.         image.scaleAbsolute(PageSize.A4.rotate());
  39.         image.setAbsolutePosition(0, 0);
  40.         canvas.saveState();
  41.         PdfGState state = new PdfGState();
  42.         state.setFillOpacity(0.6f);
  43.         canvas.setGState(state);
  44.         canvas.addImage(image);
  45.         canvas.restoreState();
  46.         document.close();
  47.     }
  48. }
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