Page Labels

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

31st May 2016
iText PDF

Switch code for this example

PageLabels.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. package com.itextpdf.samples.sandbox.objects;
  9.  
  10. import com.itextpdf.io.font.FontConstants;
  11. import com.itextpdf.kernel.font.PdfFont;
  12. import com.itextpdf.kernel.font.PdfFontFactory;
  13. import com.itextpdf.kernel.geom.PageSize;
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.action.PdfAction;
  17. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  18. import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.AreaBreak;
  23. import com.itextpdf.layout.element.Link;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.io.IOException;
  32.  
  33. @Category(SampleTest.class)
  34. public class PageLabels extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/objects/page_labels.pdf";
  36.  
  37.     public static void main(String[] args) throws IOException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new PageLabels().manipulatePdf(DEST);
  41.     }
  42.  
  43.     public void manipulatePdf(String dest) throws IOException {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         PdfViewerPreferences viewerPreferences = new PdfViewerPreferences();
  48.         viewerPreferences.setPrintScaling(PdfViewerPreferences.PdfViewerPreferencesConstants.NONE);
  49.         pdfDoc.getCatalog().setPageMode(PdfName.UseThumbs);
  50.         pdfDoc.getCatalog().setPageLayout(PdfName.TwoPageLeft);
  51.         pdfDoc.getCatalog().setViewerPreferences(viewerPreferences);
  52.  
  53.         doc.add(new Paragraph("Hello World"));
  54.         doc.add(new Paragraph("Hello People"));
  55.         doc.add(new AreaBreak());
  56.  
  57.         PdfFont bf = PdfFontFactory.createFont(FontConstants.HELVETICA);
  58.  
  59.         // we add the text to the direct content, but not in the right order
  60.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getPage(2));
  61.         canvas.beginText();
  62.         canvas.setFontAndSize(bf, 12);
  63.         canvas.moveText(88.66f, 788);
  64.         canvas.showText("ld");
  65.         canvas.moveText(-22f, 0);
  66.         canvas.showText("Wor");
  67.         canvas.moveText(-15.33f, 0);
  68.         canvas.showText("llo");
  69.         canvas.moveText(-15.33f, 0);
  70.         canvas.showText("He");
  71.         canvas.endText();
  72.         PdfFormXObject tmp = new PdfFormXObject(new Rectangle(250, 25));
  73.         new PdfCanvas(tmp, pdfDoc).beginText().
  74.                 setFontAndSize(bf, 12).
  75.                 moveText(0, 7).
  76.                 showText("Hello People").
  77.                 endText();
  78.         canvas.addXObject(tmp, 36, 763);
  79.  
  80.         pdfDoc.setDefaultPageSize(new PageSize(PageSize.A4).rotate());
  81.         doc.add(new AreaBreak());
  82.         doc.add(new Paragraph("Hello World"));
  83.  
  84.         pdfDoc.setDefaultPageSize(new PageSize(842, 595));
  85.  
  86.         doc.add(new AreaBreak());
  87.         doc.add(new Paragraph("Hello World"));
  88.  
  89.         pdfDoc.setDefaultPageSize(PageSize.A4);
  90.         doc.add(new AreaBreak());
  91.         pdfDoc.getLastPage().setCropBox(new Rectangle(40, 40, 525, 755));
  92.         doc.add(new Paragraph("Hello World"));
  93.  
  94.         doc.add(new AreaBreak());
  95.         pdfDoc.getLastPage().getPdfObject().put(PdfName.UserUnit, new PdfNumber(5));
  96.         doc.add(new Paragraph("Hello World"));
  97.  
  98.         doc.add(new AreaBreak());
  99.         pdfDoc.getLastPage().setArtBox(new Rectangle(36, 36, 523, 770));
  100.         Link link = new Link("World", PdfAction.createURI("http://maps.google.com"));
  101.         Paragraph p = new Paragraph("Hello ");
  102.         p.add(link);
  103.         doc.add(p);
  104.         PdfAnnotation a = new PdfTextAnnotation(
  105.                 new Rectangle(36, 755, 30, 30))
  106.                 .setTitle(new PdfString("Example"))
  107.                 .setContents("This is a post-it annotation");
  108.         pdfDoc.getLastPage().addAnnotation(a);
  109.         pdfDoc.getPage(1).setPageLabel(PageLabelNumberingStyleConstants.UPPERCASE_LETTERS, null);
  110.         pdfDoc.getPage(3).setPageLabel(PageLabelNumberingStyleConstants.DECIMAL_ARABIC_NUMERALS, null);
  111.         pdfDoc.getPage(4).setPageLabel(PageLabelNumberingStyleConstants.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);
  112.  
  113.         doc.close();
  114.     }
  115. }
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