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 Xamarin SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2021 by PDFTron Systems 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//-----------------------------------------------------------------------------------
22using NUnit.Framework;
23
24namespace MiscellaneousSamples
25{
26 [TestFixture]
27 public class PageLabelsTest
28 {
29
30 // Relative path to the folder containing test files.
31 const string input_path = "TestFiles/";
32
33 /// <summary>
34 /// The main entry point for the application.
35 /// </summary>
36 [Test]
37 public static void Sample()
38 {
39 try
40 {
41 //-----------------------------------------------------------
42 // Example 1: Add page labels to an existing or newly created PDF
43 // document.
44 //-----------------------------------------------------------
45 {
46 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
47 {
48 doc.InitSecurityHandler();
49
50 // Create a page labeling scheme that starts with the first page in
51 // the document (page 1) and is using uppercase roman numbering
52 // style.
53 doc.SetPageLabel(1, PageLabel.Create(doc, PageLabel.Style.e_roman_uppercase, "My Prefix ", 1));
54
55 // Create a page labeling scheme that starts with the fourth page in
56 // the document and is using decimal arabic numbering style.
57 // Also the numeric portion of the first label should start with number
58 // 4 (otherwise the first label would be "My Prefix 1").
59 PageLabel L2 = PageLabel.Create(doc, PageLabel.Style.e_decimal, "My Prefix ", 4);
60 doc.SetPageLabel(4, L2);
61
62 // Create a page labeling scheme that starts with the seventh page in
63 // the document and is using alphabetic numbering style. The numeric
64 // portion of the first label should start with number 1.
65 PageLabel L3 = PageLabel.Create(doc, PageLabel.Style.e_alphabetic_uppercase, "My Prefix ", 1);
66 doc.SetPageLabel(7, L3);
67
68 doc.Save(Utils.CreateExternalFile("newsletter_with_pagelabels.pdf"), SDFDoc.SaveOptions.e_linearized);
69 Console.WriteLine("Done. Result saved in newsletter_with_pagelabels.pdf...");
70 }
71 }
72
73 //-----------------------------------------------------------
74 // Example 2: Read page labels from an existing PDF document.
75 //-----------------------------------------------------------
76 {
77 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("newsletter_with_pagelabels.pdf")))
78 {
79 doc.InitSecurityHandler();
80
81 PageLabel label;
82 int page_num = doc.GetPageCount();
83 for (int i=1; i<=page_num; ++i)
84 {
85 Console.Write("Page number: {0}", i);
86 label = doc.GetPageLabel(i);
87 if (label.IsValid()) {
88 Console.WriteLine(" Label: {0}", label.GetLabelTitle(i));
89 }
90 else {
91 Console.WriteLine(" No Label.");
92 }
93 }
94 }
95 }
96
97 //-----------------------------------------------------------
98 // Example 3: Modify page labels from an existing PDF document.
99 //-----------------------------------------------------------
100 {
101 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("newsletter_with_pagelabels.pdf")))
102 {
103 doc.InitSecurityHandler();
104
105 // Remove the alphabetic labels from example 1.
106 doc.RemovePageLabel(7);
107
108 // Replace the Prefix in the decimal lables (from example 1).
109 PageLabel label = doc.GetPageLabel(4);
110 if (label.IsValid()) {
111 label.SetPrefix("A");
112 label.SetStart(1);
113 }
114
115 // Add a new label
116 PageLabel new_label = PageLabel.Create(doc, PageLabel.Style.e_decimal, "B", 1);
117 doc.SetPageLabel(10, new_label); // starting from page 10.
118
119 doc.Save(Utils.CreateExternalFile("newsletter_with_pagelabels_modified.pdf"), SDFDoc.SaveOptions.e_linearized);
120 Console.WriteLine("Done. Result saved in newsletter_with_pagelabels_modified.pdf...");
121
122 int page_num = doc.GetPageCount();
123 for (int i=1; i<=page_num; ++i)
124 {
125 Console.Write("Page number: {0}", i);
126 label = doc.GetPageLabel(i);
127 if (label.IsValid()) {
128 Console.WriteLine(" Label: {0}", label.GetLabelTitle(i));
129 }
130 else {
131 Console.WriteLine(" No Label.");
132 }
133 }
134 }
135 }
136
137 //-----------------------------------------------------------
138 // Example 4: Delete all page labels in an existing PDF document.
139 //-----------------------------------------------------------
140 {
141 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("newsletter_with_pagelabels.pdf")))
142 {
143 doc.GetRoot().Erase("PageLabels");
144 // ...
145 }
146 }
147 }
148 catch (pdftron.Common.PDFNetException e)
149 {
150 Console.WriteLine(e.Message);
151 Assert.True(false);
152 }
153 }
154 }
155}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales