Clipping pages in an existing PDF

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

31st May 2016
admin-marketing

Switch code for this example

ClipPdf.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/26773942/itext-crop-out-a-part-of-pdf-file
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22. import java.util.Locale;
  23.  
  24. @Category(SampleTest.class)
  25. public class ClipPdf extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/clip_pdf.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/hero.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ClipPdf().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.         PdfDictionary page;
  40.         PdfArray media;
  41.         for (int p = 1; p

Resources

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