Adding backgrounds using page events

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

25th October 2015
admin-marketing

Switch code for this example

PageBackgrounds.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/30211043/change-the-color-of-pdf-pages-alternatively-using-itext-pdf-in-java
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  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 java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class PageBackgrounds {
  26.     public static final String DEST = "results/events/page_backgrounds.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new PageBackgrounds().createPdf(DEST);
  32.     }
  33.    
  34.     public class Background extends PdfPageEventHelper {
  35.         @Override
  36.         public void onEndPage(PdfWriter writer, Document document) {
  37.             int pagenumber = writer.getPageNumber();
  38.             if (pagenumber % 2 == 1 && pagenumber != 1)
  39.                 return;
  40.             PdfContentByte canvas = writer.getDirectContentUnder();
  41.             Rectangle rect = document.getPageSize();
  42.             canvas.setColorFill(pagenumber < 3 ? BaseColor.BLUE : BaseColor.LIGHT_GRAY);
  43.             canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
  44.             canvas.fill();
  45.         }
  46.     }
  47.    
  48.     public void createPdf(String filename) throws IOException, DocumentException {
  49.         // step 1
  50.         Document document = new Document();
  51.         // step 2
  52.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  53.         Background event = new Background();
  54.         writer.setPageEvent(event);
  55.         // step 3
  56.         document.open();
  57.         document.add(new Paragraph("Prime Numbers"));
  58.         document.newPage();
  59.         document.add(new Paragraph("An overview"));
  60.         document.newPage();
  61.         // step 4
  62.         List factors;
  63.         for (int i = 2; i < 301; i++) {
  64.             factors = getFactors(i);
  65.             if (factors.size() == 1) {
  66.                 document.add(new Paragraph("This is a prime number!"));
  67.             }
  68.             for (int factor : factors) {
  69.                 document.add(new Paragraph("Factor: " + factor));
  70.             }
  71.             document.newPage();
  72.         }
  73.         // step 5
  74.         document.close();
  75.     }
  76.    
  77.     public static List getFactors(int n) {
  78.         List factors = new ArrayList();
  79.         for (int i = 2; i
PageBorder.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/25749828/how-to-draw-border-for-whole-pdf-pages-using-itext-library-5-5-2
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  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 java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class PageBorder {
  26.     public static final String DEST = "results/events/page_border.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new PageBorder().createPdf(DEST);
  32.     }
  33.    
  34.     public class RedBorder extends PdfPageEventHelper {
  35.         @Override
  36.         public void onEndPage(PdfWriter writer, Document document) {
  37.             PdfContentByte canvas = writer.getDirectContent();
  38.             Rectangle rect = document.getPageSize();
  39.             rect.setBorder(Rectangle.BOX); // left, right, top, bottom border
  40.             rect.setBorderWidth(5); // a width of 5 user units
  41.             rect.setBorderColor(BaseColor.RED); // a red border
  42.             rect.setUseVariableBorders(true); // the full width will be visible
  43.             canvas.rectangle(rect);
  44.         }
  45.     }
  46.    
  47.     public void createPdf(String filename) throws IOException, DocumentException {
  48.         // step 1
  49.         Document document = new Document();
  50.         // step 2
  51.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  52.         RedBorder event = new RedBorder();
  53.         writer.setPageEvent(event);
  54.         // step 3
  55.         document.open();
  56.         // step 4
  57.         List factors;
  58.         for (int i = 2; i < 301; i++) {
  59.             factors = getFactors(i);
  60.             if (factors.size() == 1) {
  61.                 document.add(new Paragraph("This is a prime number!"));
  62.             }
  63.             for (int factor : factors) {
  64.                 document.add(new Paragraph("Factor: " + factor));
  65.             }
  66.             document.newPage();
  67.         }
  68.         // step 5
  69.         document.close();
  70.     }
  71.    
  72.     public static List getFactors(int n) {
  73.         List factors = new ArrayList();
  74.         for (int i = 2; i
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