PageLabels

Sample C# 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 Server SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.SDF;
8using pdftron.PDF;
9
10//-----------------------------------------------------------------------------------
11// The sample illustrates how to work with PDF page labels.
12//
13// PDF page labels can be used to describe a page. This is used to
14// allow for non-sequential page numbering or the addition of arbitrary
15// labels for a page (such as the inclusion of Roman numerals at the
16// beginning of a book). PDFNet PageLabel object can be used to specify
17// the numbering style to use (for example, upper- or lower-case Roman,
18// decimal, and so forth), the starting number for the first page,
19// and an arbitrary prefix to be pre-appended to each number (for
20// example, "A-" to generate "A-1", "A-2", "A-3", and so forth.)
21//-----------------------------------------------------------------------------------
22namespace PageLabelsTestCS
23{
24 class Class1
25 {
26 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
27 static Class1() {}
28
29 // Relative path to the folder containing test files.
30 const string input_path = "../../../../TestFiles/";
31 const string output_path = "../../../../TestFiles/Output/";
32
33 /// <summary>
34 /// The main entry point for the application.
35 /// </summary>
36 [STAThread]
37 static void Main(string[] args)
38 {
39 PDFNet.Initialize(PDFTronLicense.Key);
40 try
41 {
42 //-----------------------------------------------------------
43 // Example 1: Add page labels to an existing or newly created PDF
44 // document.
45 //-----------------------------------------------------------
46 {
47 using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
48 {
49 doc.InitSecurityHandler();
50
51 // Create a page labeling scheme that starts with the first page in
52 // the document (page 1) and is using uppercase roman numbering
53 // style.
54 doc.SetPageLabel(1, PageLabel.Create(doc, PageLabel.Style.e_roman_uppercase, "My Prefix ", 1));
55
56 // Create a page labeling scheme that starts with the fourth page in
57 // the document and is using decimal arabic numbering style.
58 // Also the numeric portion of the first label should start with number
59 // 4 (otherwise the first label would be "My Prefix 1").
60 PageLabel L2 = PageLabel.Create(doc, PageLabel.Style.e_decimal, "My Prefix ", 4);
61 doc.SetPageLabel(4, L2);
62
63 // Create a page labeling scheme that starts with the seventh page in
64 // the document and is using alphabetic numbering style. The numeric
65 // portion of the first label should start with number 1.
66 PageLabel L3 = PageLabel.Create(doc, PageLabel.Style.e_alphabetic_uppercase, "My Prefix ", 1);
67 doc.SetPageLabel(7, L3);
68
69 doc.Save(output_path + "newsletter_with_pagelabels.pdf", SDFDoc.SaveOptions.e_linearized);
70 Console.WriteLine("Done. Result saved in newsletter_with_pagelabels.pdf...");
71 }
72 }
73
74 //-----------------------------------------------------------
75 // Example 2: Read page labels from an existing PDF document.
76 //-----------------------------------------------------------
77 {
78 using (PDFDoc doc = new PDFDoc(output_path + "newsletter_with_pagelabels.pdf"))
79 {
80 doc.InitSecurityHandler();
81
82 PageLabel label;
83 int page_num = doc.GetPageCount();
84 for (int i=1; i<=page_num; ++i)
85 {
86 Console.Write("Page number: {0}", i);
87 label = doc.GetPageLabel(i);
88 if (label.IsValid()) {
89 Console.WriteLine(" Label: {0}", label.GetLabelTitle(i));
90 }
91 else {
92 Console.WriteLine(" No Label.");
93 }
94 }
95 }
96 }
97
98 //-----------------------------------------------------------
99 // Example 3: Modify page labels from an existing PDF document.
100 //-----------------------------------------------------------
101 {
102 using (PDFDoc doc = new PDFDoc(output_path + "newsletter_with_pagelabels.pdf"))
103 {
104 doc.InitSecurityHandler();
105
106 // Remove the alphabetic labels from example 1.
107 doc.RemovePageLabel(7);
108
109 // Replace the Prefix in the decimal lables (from example 1).
110 PageLabel label = doc.GetPageLabel(4);
111 if (label.IsValid()) {
112 label.SetPrefix("A");
113 label.SetStart(1);
114 }
115
116 // Add a new label
117 PageLabel new_label = PageLabel.Create(doc, PageLabel.Style.e_decimal, "B", 1);
118 doc.SetPageLabel(10, new_label); // starting from page 10.
119
120 doc.Save(output_path + "newsletter_with_pagelabels_modified.pdf", SDFDoc.SaveOptions.e_linearized);
121 Console.WriteLine("Done. Result saved in newsletter_with_pagelabels_modified.pdf...");
122
123 int page_num = doc.GetPageCount();
124 for (int i=1; i<=page_num; ++i)
125 {
126 Console.Write("Page number: {0}", i);
127 label = doc.GetPageLabel(i);
128 if (label.IsValid()) {
129 Console.WriteLine(" Label: {0}", label.GetLabelTitle(i));
130 }
131 else {
132 Console.WriteLine(" No Label.");
133 }
134 }
135 }
136 }
137
138 //-----------------------------------------------------------
139 // Example 4: Delete all page labels in an existing PDF document.
140 //-----------------------------------------------------------
141 {
142 using (PDFDoc doc = new PDFDoc(output_path + "newsletter_with_pagelabels.pdf"))
143 {
144 doc.GetRoot().Erase("PageLabels");
145 // ...
146 }
147 }
148 }
149 catch (pdftron.Common.PDFNetException e)
150 {
151 Console.WriteLine(e.Message);
152 }
153 PDFNet.Terminate();
154 }
155 }
156}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales