Page Labels

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

8th November 2015
admin-marketing

Switch code for this example

PageLabels.java
  1. package sandbox.objects;
  2.  
  3. import com.itextpdf.text.Anchor;
  4. import com.itextpdf.text.Annotation;
  5. import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.PageSize;
  8. import com.itextpdf.text.Paragraph;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.BaseFont;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfPageLabels;
  15. import com.itextpdf.text.pdf.PdfTemplate;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class PageLabels {
  26.  
  27.     public static final String DEST = "results/objects/pagelabels.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException,
  30.             DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new PageLabels().createPdf(DEST);
  34.     }
  35.    
  36.     public void createPdf(String filename) throws IOException, DocumentException {
  37.         Document document = new Document();
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  39.         writer.setViewerPreferences(PdfWriter.PageLayoutTwoPageLeft | PdfWriter.PageModeUseThumbs);
  40.         writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
  41.         PdfPageLabels labels = new PdfPageLabels();
  42.         labels.addPageLabel(1, PdfPageLabels.UPPERCASE_LETTERS);
  43.         labels.addPageLabel(3, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
  44.         labels.addPageLabel(4,
  45.             PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);
  46.         writer.setPageLabels(labels);
  47.         document.open();
  48.         document.add(new Paragraph("Hello World"));
  49.         document.add(new Paragraph("Hello People"));
  50.         document.newPage();
  51.  
  52.         // we add the text to the direct content, but not in the right order
  53.         PdfContentByte cb = writer.getDirectContent();
  54.         BaseFont bf = BaseFont.createFont();
  55.         cb.beginText();
  56.         cb.setFontAndSize(bf, 12);
  57.         cb.moveText(88.66f, 788);
  58.         cb.showText("ld");
  59.         cb.moveText(-22f, 0);
  60.         cb.showText("Wor");
  61.         cb.moveText(-15.33f, 0);
  62.         cb.showText("llo");
  63.         cb.moveText(-15.33f, 0);
  64.         cb.showText("He");
  65.         cb.endText();
  66.         // we also add text in a form XObject
  67.         PdfTemplate tmp = cb.createTemplate(250, 25);
  68.         tmp.beginText();
  69.         tmp.setFontAndSize(bf, 12);
  70.         tmp.moveText(0, 7);
  71.         tmp.showText("Hello People");
  72.         tmp.endText();
  73.         cb.addTemplate(tmp, 36, 763);
  74.                
  75.         document.setPageSize(PageSize.A4.rotate());
  76.         document.newPage();
  77.         document.add(new Paragraph("Hello World"));
  78.        
  79.         document.setPageSize(new Rectangle(842, 595));
  80.         document.newPage();
  81.         document.add(new Paragraph("Hello World"));
  82.         document.setPageSize(PageSize.A4);
  83.        
  84.         writer.setCropBoxSize(new Rectangle(40, 40, 565, 795));
  85.         document.newPage();
  86.         document.add(new Paragraph("Hello World"));
  87.  
  88.         writer.setCropBoxSize(null);
  89.         document.newPage();
  90.         writer.addPageDictEntry(PdfName.USERUNIT, new PdfNumber(5));
  91.         document.add(new Paragraph("Hello World"));
  92.  
  93.         writer.setBoxSize("art", new Rectangle(36, 36, 559, 806));
  94.         document.newPage();
  95.         Anchor anchor = new Anchor("World");
  96.         anchor.setReference("http://maps.google.com");
  97.         Paragraph p = new Paragraph("Hello ");
  98.         p.add(anchor);
  99.         document.add(p);
  100.         Annotation a = new Annotation("Example", "This is a post-it annotation");
  101.         document.add(a);
  102.        
  103.         document.close();
  104.     }
  105. }
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