Changing page sizes of existing PDFs

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

5th November 2015
admin-marketing

Switch code for this example

AddExtraMargin.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following StackOverflow question:
  3.  * http://stackoverflow.com/questions/29775893/watermark-in-a-pdf-with-itext
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.GrayColor;
  9. import com.itextpdf.text.pdf.PdfArray;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class AddExtraMargin {
  24.  
  25.     public static final String SRC = "resources/pdfs/primes.pdf";
  26.     public static final String DEST = "results/stamper/primes_extra_margin.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddExtraMargin().manipulatePdf(SRC, DEST);
  32.     }
  33.  
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         int n = reader.getNumberOfPages();
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         // properties
  39.         PdfContentByte over;
  40.         PdfDictionary pageDict;
  41.         PdfArray mediabox;
  42.         float llx, lly, ury;
  43.         // loop over every page
  44.         for (int i = 1; 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