Changing page sizes of existing PDFs

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

31st May 2016
admin-marketing

Switch code for this example

AddExtraMargin.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 example was written by Bruno Lowagie in answer to the following StackOverflow question:
  10.  * http://stackoverflow.com/questions/29775893/watermark-in-a-pdf-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.color.DeviceGray;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24. @Category(SampleTest.class)
  25. public class AddExtraMargin extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_extra_margin.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddExtraMargin().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  38.         int n = pdfDoc.getNumberOfPages();
  39.         PdfCanvas 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