Adding backgrounds using page events

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

31st May 2016
admin-marketing

Switch code for this example

PageBackgrounds.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/30211043/change-the-color-of-pdf-pages-alternatively-using-itext-pdf-in-java
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfPage;
  21. import com.itextpdf.kernel.pdf.PdfWriter;
  22. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  23. import com.itextpdf.layout.Document;
  24. import com.itextpdf.layout.element.AreaBreak;
  25. import com.itextpdf.layout.element.Paragraph;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34.  
  35. @Category(SampleTest.class)
  36. public class PageBackgrounds extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/events/page_backgrounds.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new PageBackgrounds().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public static List getFactors(int n) {
  46.         List factors = new ArrayList();
  47.         for (int i = 2; i
PageBorder.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/25749828/how-to-draw-border-for-whole-pdf-pages-using-itext-library-5-5-2
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.AreaBreak;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33.  
  34. @Category(SampleTest.class)
  35. public class PageBorder extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/events/page_border.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new PageBorder().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public static List getFactors(int n) {
  45.         List factors = new ArrayList();
  46.         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