Scaling and rotating pages

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

6th November 2015
admin-marketing

Switch code for this example

Rotate90Degrees.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27020542/rotating-pdf-90-degrees-using-itextsharp-in-c-sharp
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfDictionary;
  9. import com.itextpdf.text.pdf.PdfName;
  10. import com.itextpdf.text.pdf.PdfNumber;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class Rotate90Degrees {
  21.  
  22.     public static final String SRC = "resources/pdfs/pages.pdf";
  23.     public static final String DEST = "results/stamper/pages_rotated90degrees.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new Rotate90Degrees().manipulatePdf(SRC, DEST);
  29.     }
  30.  
  31.  
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         int n = reader.getNumberOfPages();
  35.         PdfDictionary page;
  36.         PdfNumber rotate;
  37.         for (int p = 1; p
ScaleRotate.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21871027/rotating-in-itextsharp-while-preserving-comment-location-orientation
  4.  *
  5.  * Example that shows how to scale an existing PDF using the UserUnit and how to remove the rotation of a page.
  6.  */
  7. package sandbox.stamper;
  8.  
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.pdf.PdfDictionary;
  11. import com.itextpdf.text.pdf.PdfName;
  12. import com.itextpdf.text.pdf.PdfNumber;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class ScaleRotate {
  24.  
  25.     public static final String SRC = "resources/pdfs/pages.pdf";
  26.     public static final String DEST = "results/stamper/pages_altered.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ScaleRotate().manipulatePdf(SRC, DEST);
  32.     }
  33.  
  34.  
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         int n = reader.getNumberOfPages();
  38.         PdfDictionary page;
  39.         for (int p = 1; p
ShrinkPdf.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/25356302/shrink-pdf-pages-with-rotation-using-rectangle-in-existing-pdf
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfArray;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfNumber;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ShrinkPdf {
  22.  
  23.     public static final String SRC = "resources/pdfs/hero.pdf";
  24.     public static final String DEST = "results/stamper/hero_shrink.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ShrinkPdf().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         int n = reader.getNumberOfPages();
  36.         PdfDictionary page;
  37.         PdfArray crop;
  38.         PdfArray media;
  39.         for (int p = 1; p
ShrinkPdf2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/25356302/shrink-pdf-pages-with-rotation-using-rectangle-in-existing-pdf
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class ShrinkPdf2 {
  18.  
  19.     public static final String SRC = "resources/pdfs/hero.pdf";
  20.     public static final String DEST = "results/stamper/hero_shrink2.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new ShrinkPdf2().manipulatePdf(SRC, DEST);
  26.     }
  27.  
  28.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  29.         PdfReader reader = new PdfReader(src);
  30.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  31.         int n = reader.getNumberOfPages();
  32.         float percentage = 0.8f;
  33.         for (int p = 1; p
ScaleDown.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/29152313/fix-the-orientation-of-a-pdf-in-order-to-scale-it
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfRectangle;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. /**
  24.  * This solution is suboptimal as it throws away all interactivity.
  25.  * If you want to keep the interactive elements (annotations, form fields,...),
  26.  * you need to do much more work. If you don't need to scale down, but instead
  27.  * only have to scale up, you should try the example that was written in
  28.  * answer to http://stackoverflow.com/questions/21871027/rotating-in-itextsharp-while-preserving-comment-location-orientation
  29.  */
  30. @WrapToTest
  31. public class ScaleDown {
  32.    
  33.     public static final String SRC = "resources/pdfs/orientations.pdf";
  34.     public static final String DEST = "results/events/scaled_down.pdf";
  35.        
  36.     public class ScaleEvent extends PdfPageEventHelper {
  37.        
  38.         protected float scale = 1;
  39.         protected PdfDictionary pageDict;
  40.        
  41.         public ScaleEvent(float scale) {
  42.             this.scale = scale;
  43.         }
  44.        
  45.         public void setPageDict(PdfDictionary pageDict) {
  46.             this.pageDict = pageDict;
  47.         }
  48.        
  49.         @Override
  50.         public void onStartPage(PdfWriter writer, Document document) {
  51.             writer.addPageDictEntry(PdfName.ROTATE, pageDict.getAsNumber(PdfName.ROTATE));
  52.             writer.addPageDictEntry(PdfName.MEDIABOX, scaleDown(pageDict.getAsArray(PdfName.MEDIABOX), scale));
  53.             writer.addPageDictEntry(PdfName.CROPBOX, scaleDown(pageDict.getAsArray(PdfName.CROPBOX), scale));
  54.         }
  55.     }
  56.    
  57.     public static void main(String[] args) throws IOException, DocumentException {
  58.         File file = new File(DEST);
  59.         file.getParentFile().mkdirs();
  60.         new ScaleDown().manipulatePdf(SRC, DEST);
  61.     }
  62.    
  63.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  64.         PdfReader reader = new PdfReader(src);
  65.         float scale = 0.5f;
  66.         ScaleEvent event = new ScaleEvent(scale);
  67.         event.setPageDict(reader.getPageN(1));
  68.        
  69.         int n = reader.getNumberOfPages();
  70.         Document document = new Document();
  71.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  72.         writer.setPageEvent(event);
  73.         document.open();
  74.         Image page;
  75.         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