Scaling and rotating pages

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

31st May 2016
admin-marketing

Switch code for this example

Rotate90Degrees.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/27020542/rotating-pdf-90-degrees-using-itextsharp-in-c-sharp
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class Rotate90Degrees extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/stamper/rotate90degrees.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/pages.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new Rotate90Degrees().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  36.         int n = pdfDoc.getNumberOfPages();
  37.         PdfPage page;
  38.         PdfNumber rotate;
  39.         for (int p = 1; p
ScaleRotate.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/21871027/rotating-in-itextsharp-while-preserving-comment-location-orientation
  11.  *
  12.  * Example that shows how to scale an existing PDF using the UserUnit and how to remove the rotation of a page.
  13.  */
  14. package com.itextpdf.samples.sandbox.stamper;
  15.  
  16. import com.itextpdf.kernel.pdf.*;
  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 ScaleRotate extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/scale_rotate.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/pages.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ScaleRotate().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.         for (int p = 1; p
ShrinkPdf.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/25356302/shrink-pdf-pages-with-rotation-using-rectangle-in-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  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 ShrinkPdf extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/shrink_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 ShrinkPdf().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.         PdfPage page;
  40.         Rectangle crop;
  41.         Rectangle media;
  42.         for (int p = 1; p
ShrinkPdf2.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/25356302/shrink-pdf-pages-with-rotation-using-rectangle-in-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfResources;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25. import java.util.Locale;
  26.  
  27. @Category(SampleTest.class)
  28. public class ShrinkPdf2 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/stamper/shrink_pdf2.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/hero.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ShrinkPdf2().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.         int n = pdfDoc.getNumberOfPages();
  42.         float percentage = 0.8f;
  43.         for (int p = 1; p
ScaleDown.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/29152313/fix-the-orientation-of-a-pdf-in-order-to-scale-it
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.events.Event;
  15. import com.itextpdf.kernel.events.IEventHandler;
  16. import com.itextpdf.kernel.events.PdfDocumentEvent;
  17. import com.itextpdf.kernel.pdf.*;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class ScaleDown extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/events/scale_down.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/orientations.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ScaleDown().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         // Create the source document
  41.         PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  43.         float scale = 0.5f;
  44.         ScaleDownEventHandler eventHandler = new ScaleDownEventHandler(scale);
  45.         int n = srcDoc.getNumberOfPages();
  46.         pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
  47.  
  48.         PdfCanvas canvas;
  49.         PdfFormXObject page;
  50.         for (int p = 1; p
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