Page size

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

31st October 2015
admin-marketing

Switch code for this example

UnequalPages.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23117200/itext-create-document-with-unequal-page-sizes
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class UnequalPages {
  21.  
  22.     public static final String DEST = "results/objects/unequal_pages.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new UnequalPages().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         Rectangle one = new Rectangle(70,140);
  35.         Rectangle two = new Rectangle(700,400);
  36.         document.setPageSize(one);
  37.         document.setMargins(2, 2, 2, 2);
  38.         document.open();
  39.         Paragraph p = new Paragraph("Hi");
  40.         document.add(p);
  41.         document.setPageSize(two);
  42.         document.setMargins(20, 20, 20, 20);
  43.         document.newPage();
  44.         document.add(p);
  45.         document.close();
  46.     }
  47. }
ChangeMargin.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30328489/itextsharp-change-margins-no-document-setpagesize
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. @WrapToTest
  22. public class ChangeMargin {
  23.    
  24.     public static final String DEST = "results/objects/change_margin.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException,
  27.             DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ChangeMargin().createPdf(DEST);
  31.     }
  32.    
  33.    
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         float left = 30;
  37.         float right = 30;
  38.         float top = 60;
  39.         float bottom = 0;
  40.         Document document = new Document(PageSize.A4, left, right, top, bottom);
  41.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         document.open();
  43.         document.setMargins(left, right, 0, bottom);
  44.         for (int i = 0; i < 60; i++) {
  45.             document.add(new Paragraph("This is a test"));
  46.         }
  47.         document.close();
  48.     }
  49. }
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