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 UWP SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class PageLabelsTest : Sample
18 {
19 public PageLabelsTest() :
20 base("PageLabels", "This example illustrates how to work with PDF page labels. PDF page labels can be used to describe a page. This 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).")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(async () => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting Annotation Test...");
29 WriteLine("--------------------------------\n");
30 try
31 {
32 //-----------------------------------------------------------
33 // Example 1: Add page labels to an existing or newly created PDF
34 // document.
35 //-----------------------------------------------------------
36 {
37 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
38 WriteLine("Opening input file " + input_file_path);
39 PDFDoc doc = new PDFDoc(input_file_path);
40 doc.InitSecurityHandler();
41
42 // Create a page labeling scheme that starts with the first page in
43 // the document (page 1) and is using uppercase roman numbering
44 // style.
45 doc.SetPageLabel(1, PageLabel.Create(doc.GetSDFDoc(), PageLabelStyle.e_roman_uppercase, "My Prefix ", 1));
46
47 // Create a page labeling scheme that starts with the fourth page in
48 // the document and is using decimal arabic numbering style.
49 // Also the numeric portion of the first label should start with number
50 // 4 (otherwise the first label would be "My Prefix 1").
51 PageLabel L2 = PageLabel.Create(doc.GetSDFDoc(), PageLabelStyle.e_decimal, "My Prefix ", 4);
52 doc.SetPageLabel(4, L2);
53
54 // Create a page labeling scheme that starts with the seventh page in
55 // the document and is using alphabetic numbering style. The numeric
56 // portion of the first label should start with number 1.
57 PageLabel L3 = PageLabel.Create(doc.GetSDFDoc(), PageLabelStyle.e_alphabetic_uppercase, "My Prefix ", 1);
58 doc.SetPageLabel(7, L3);
59
60 String output_file_path = Path.Combine(OutputPath, "newsletter_with_pagelabels.pdf");
61 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
62 WriteLine("Done. Results saved in " + output_file_path);
63 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
64 doc.Destroy();
65 }
66
67 //-----------------------------------------------------------
68 // Example 2: Read page labels from an existing PDF document.
69 //-----------------------------------------------------------
70 {
71 string input_file_path = Path.Combine(OutputPath, "newsletter_with_pagelabels.pdf");
72 WriteLine("Opening file " + input_file_path);
73 PDFDoc doc = new PDFDoc(input_file_path);
74 doc.InitSecurityHandler();
75
76 PageLabel label;
77 int page_num = doc.GetPageCount();
78 for (int i=1; i<=page_num; ++i)
79 {
80 WriteLine(string.Format("Page number: {0}", i));
81 label = doc.GetPageLabel(i);
82 if (label.IsValid()) {
83 WriteLine(string.Format(" Label: {0}", label.GetLabelTitle(i)));
84 }
85 else {
86 WriteLine(" No Label.");
87 }
88 }
89 doc.Destroy();
90 }
91
92 //-----------------------------------------------------------
93 // Example 3: Modify page labels from an existing PDF document.
94 //-----------------------------------------------------------
95 {
96 string input_file_path = Path.Combine(OutputPath, "newsletter_with_pagelabels.pdf");
97 WriteLine("Opening file " + input_file_path);
98 PDFDoc doc = new PDFDoc(input_file_path);
99 doc.InitSecurityHandler();
100
101 // Remove the alphabetic labels from example 1.
102 doc.RemovePageLabel(7);
103
104 // Replace the Prefix in the decimal lables (from example 1).
105 PageLabel label = doc.GetPageLabel(4);
106 if (label.IsValid()) {
107 label.SetPrefix("A");
108 label.SetStart(1);
109 }
110
111 // Add a new label
112 PageLabel new_label = PageLabel.Create(doc.GetSDFDoc(), PageLabelStyle.e_decimal, "B", 1);
113 doc.SetPageLabel(10, new_label); // starting from page 10.
114
115 String output_file_path = Path.Combine(OutputPath, "newsletter_with_pagelabels_modified.pdf");
116 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
117 WriteLine("Done. Results saved in " + output_file_path);
118 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
119
120 int page_num = doc.GetPageCount();
121 for (int i=1; i<=page_num; ++i)
122 {
123 WriteLine(string.Format("Page number: {0}", i));
124 label = doc.GetPageLabel(i);
125 if (label.IsValid()) {
126 WriteLine(string.Format(" Label: {0}", label.GetLabelTitle(i)));
127 }
128 else {
129 WriteLine(" No Label.");
130 }
131 }
132 doc.Destroy();
133 }
134
135 //-----------------------------------------------------------
136 // Example 4: Delete all page labels in an existing PDF document.
137 //-----------------------------------------------------------
138 {
139 PDFDoc doc = new PDFDoc(Path.Combine(OutputPath, "newsletter_with_pagelabels.pdf"));
140 //doc.GetRoot().Erase("PageLabels");
141 // ...
142 doc.Destroy();
143 }
144 }
145 catch (Exception e)
146 {
147 WriteLine(GetExceptionMessage(e));
148 }
149
150 WriteLine("\n--------------------------------");
151 WriteLine("Done PageLabels Test.");
152 WriteLine("--------------------------------\n");
153 })).AsAsyncAction();
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