PageLabels

Sample Java code for using Apryse SDK to work with PDF page labels. PDF page labels can be used to describe a page, which is used to allow for non-sequential page numbering or the addition of arbitrary labels for a page (such as the inclusion of Roman numerals at the beginning of a book). Learn more about our Android SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.PDFDoc;
13import com.pdftron.pdf.PageLabel;
14import com.pdftron.sdf.SDFDoc;
15
16import java.util.ArrayList;
17
18/**
19 * The sample illustrates how to work with PDF page labels.
20 * <p>
21 * PDF page labels can be used to describe a page. This is used to
22 * allow for non-sequential page numbering or the addition of arbitrary
23 * labels for a page (such as the inclusion of Roman numerals at the
24 * beginning of a book). PDFNet PageLabel object can be used to specify
25 * the numbering style to use (for example, upper- or lower-case Roman,
26 * decimal, and so forth), the starting number for the first page,
27 * and an arbitrary prefix to be pre-appended to each number (for
28 * example, "A-" to generate "A-1", "A-2", "A-3", and so forth.)
29 */
30public class PageLabelsTest extends PDFNetSample {
31
32 private static OutputListener mOutputListener;
33
34 private static ArrayList<String> mFileList = new ArrayList<>();
35
36 public PageLabelsTest() {
37 setTitle(R.string.sample_pagelabels_title);
38 setDescription(R.string.sample_pagelabels_description);
39 }
40
41 @Override
42 public void run(OutputListener outputListener) {
43 super.run(outputListener);
44 mOutputListener = outputListener;
45 mFileList.clear();
46 printHeader(outputListener);
47
48 try {
49 //-----------------------------------------------------------
50 // Example 1: Add page labels to an existing or newly created PDF
51 // document.
52 //-----------------------------------------------------------
53 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
54 doc.initSecurityHandler();
55
56 // Create a page labeling scheme that starts with the first page in
57 // the document (page 1) and is using uppercase roman numbering
58 // style.
59 doc.setPageLabel(1, PageLabel.create(doc, PageLabel.e_roman_uppercase, "My Prefix ", 1));
60
61 // Create a page labeling scheme that starts with the fourth page in
62 // the document and is using decimal arabic numbering style.
63 // Also the numeric portion of the first label should start with number
64 // 4 (otherwise the first label would be "My Prefix 1").
65 PageLabel L2 = PageLabel.create(doc, PageLabel.e_decimal, "My Prefix ", 4);
66 doc.setPageLabel(4, L2);
67
68 // Create a page labeling scheme that starts with the seventh page in
69 // the document and is using alphabetic numbering style. The numeric
70 // portion of the first label should start with number 1.
71 PageLabel L3 = PageLabel.create(doc, PageLabel.e_alphabetic_uppercase, "My Prefix ", 1);
72 doc.setPageLabel(7, L3);
73
74 doc.save(Utils.createExternalFile("newsletter_with_pagelabels.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
75 mOutputListener.println("Done. Result saved in newsletter_with_pagelabels.pdf...");
76 }
77
78 //-----------------------------------------------------------
79 // Example 2: Read page labels from an existing PDF document.
80 //-----------------------------------------------------------
81 try (PDFDoc doc = new PDFDoc((Utils.createExternalFile("newsletter_with_pagelabels.pdf", mFileList).getAbsolutePath()))) {
82 doc.initSecurityHandler();
83
84 PageLabel label;
85 int page_num = doc.getPageCount();
86 for (int i = 1; i <= page_num; ++i) {
87 mOutputListener.println("Page number: " + i);
88 label = doc.getPageLabel(i);
89 if (label.isValid()) {
90 mOutputListener.println(" Label: " + label.getLabelTitle(i));
91 } else {
92 mOutputListener.println(" No Label.");
93 }
94 }
95 }
96
97 //-----------------------------------------------------------
98 // Example 3: Modify page labels from an existing PDF document.
99 //-----------------------------------------------------------
100 try (PDFDoc doc = new PDFDoc((Utils.createExternalFile("newsletter_with_pagelabels.pdf", mFileList).getAbsolutePath()))) {
101 doc.initSecurityHandler();
102
103 // Remove the alphabetic labels from example 1.
104 doc.removePageLabel(7);
105
106 // Replace the Prefix in the decimal lables (from example 1).
107 PageLabel label = doc.getPageLabel(4);
108 if (label.isValid()) {
109 label.setPrefix("A");
110 label.setStart(1);
111 }
112
113 // Add a new label
114 PageLabel new_label = PageLabel.create(doc, PageLabel.e_decimal, "B", 1);
115 doc.setPageLabel(10, new_label); // starting from page 10.
116
117 doc.save(Utils.createExternalFile("newsletter_with_pagelabels_modified.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
118 mOutputListener.println("Done. Result saved in newsletter_with_pagelabels_modified.pdf...");
119
120 int page_num = doc.getPageCount();
121 for (int i = 1; i <= page_num; ++i) {
122 mOutputListener.print("Page number: " + i);
123 label = doc.getPageLabel(i);
124 if (label.isValid()) {
125 mOutputListener.println(" Label: " + label.getLabelTitle(i));
126 } else {
127 mOutputListener.println(" No Label.");
128 }
129 }
130 }
131
132 //-----------------------------------------------------------
133 // Example 4: Delete all page labels in an existing PDF document.
134 //-----------------------------------------------------------
135 try (PDFDoc doc = new PDFDoc((Utils.createExternalFile("newsletter_with_pagelabels.pdf", mFileList).getAbsolutePath()))) {
136 doc.getRoot().erase("PageLabels");
137 // ...
138 }
139
140 } catch (Exception e) {
141 mOutputListener.printError(e.getStackTrace());
142 }
143
144
145 for (String file : mFileList) {
146 addToFileList(file);
147 }
148 printFooter(outputListener);
149 }
150
151}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales