PageLabels

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

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//-----------------------------------------------------------------------------------
10// The sample illustrates how to work with PDF page labels.
11//
12// PDF page labels can be used to describe a page. This is used to
13// allow for non-sequential page numbering or the addition of arbitrary
14// labels for a page (such as the inclusion of Roman numerals at the
15// beginning of a book). PDFNet PTPageLabel object can be used to specify
16// the numbering style to use (for example, upper- or lower-case Roman,
17// decimal, and so forth), the starting number for the first page,
18// and an arbitrary prefix to be pre-appended to each number (for
19// example, "A-" to generate "A-1", "A-2", "A-3", and so forth.)
20//-----------------------------------------------------------------------------------
21int main(int argc, char *argv[])
22{
23 @autoreleasepool {
24 int ret = 0;
25 [PTPDFNet Initialize: 0];
26
27 @try
28 {
29 //-----------------------------------------------------------
30 // Example 1: Add page labels to an existing or newly created PDF
31 // document.
32 //-----------------------------------------------------------
33 {
34 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
35 [doc InitSecurityHandler];
36
37 // Create a page labeling scheme that starts with the first page in
38 // the document (page 1) and is using uppercase roman numbering
39 // style.
40 PTPageLabel *L1 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptroman_uppercase prefix: @"My Prefix " start_at: 1];
41 [doc SetPageLabel: 1 label: L1];
42
43 // Create a page labeling scheme that starts with the fourth page in
44 // the document and is using decimal Arabic numbering style.
45 // Also the numeric portion of the first label should start with number
46 // 4 (otherwise the first label would be "My Prefix 1").
47 PTPageLabel *L2 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptdecimal prefix: @"My Prefix " start_at: 4];
48 [doc SetPageLabel: 4 label: L2];
49
50 // Create a page labeling scheme that starts with the seventh page in
51 // the document and is using alphabetic numbering style. The numeric
52 // portion of the first label should start with number 1.
53 PTPageLabel *L3 = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptalphabetic_uppercase prefix: @"My Prefix " start_at: 1];
54 [doc SetPageLabel: 7 label: L3];
55
56 [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf" flags: e_ptlinearized];
57 NSLog(@"Done. Result saved in newsletter_with_pagelabels.pdf...");
58 }
59
60 //-----------------------------------------------------------
61 // Example 2: Read page labels from an existing PDF document.
62 //-----------------------------------------------------------
63 {
64 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
65 [doc InitSecurityHandler];
66
67 PTPageLabel *label;
68 int page_num = [doc GetPageCount];
69 int i=1;
70 for (; i<=page_num; ++i)
71 {
72 NSLog(@"Page number: %d", i);
73 label = [doc GetPageLabel: i];
74 if ([label IsValid]) {
75 NSLog(@" Label: %@", [label GetLabelTitle: i]);
76 }
77 else {
78 NSLog(@" No Label.");
79 }
80 }
81 }
82
83 //-----------------------------------------------------------
84 // Example 3: Modify page labels from an existing PDF document.
85 //-----------------------------------------------------------
86 {
87 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
88 [doc InitSecurityHandler];
89
90 // Remove the alphabetic labels from example 1.
91 [doc RemovePageLabel: 7];
92
93 // Replace the Prefix in the decimal labels (from example 1).
94 PTPageLabel *label = [doc GetPageLabel: 4];
95 if ([label IsValid]) {
96 [label SetPrefix: @"A"];
97 [label SetStart: 1];
98 }
99
100 // Add a new label
101 PTPageLabel *new_label = [PTPageLabel Create: [doc GetSDFDoc] style: e_ptdecimal prefix: @"B" start_at: 1];
102 [doc SetPageLabel: 10 label: new_label]; // starting from page 10.
103
104 [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels_modified.pdf" flags: e_ptlinearized];
105 NSLog(@"Done. Result saved in newsletter_with_pagelabels_modified.pdf...");
106
107 int page_num = [doc GetPageCount];
108 int i=1;
109 for (; i<=page_num; ++i)
110 {
111 NSLog(@"Page number: %d", i);
112 label = [doc GetPageLabel: i];
113 if ([label IsValid]) {
114 NSLog(@" Label: %@", [label GetLabelTitle: i]);
115 }
116 else {
117 NSLog(@" No Label.");
118 }
119 }
120 }
121
122 //-----------------------------------------------------------
123 // Example 4: Delete all page labels in an existing PDF document.
124 //-----------------------------------------------------------
125 {
126 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/newsletter_with_pagelabels.pdf"];
127 [[doc GetRoot] EraseDictElementWithKey: @"PageLabels"];
128
129 [doc SaveToFile: @"../../TestFiles/Output/newsletter_with_pagelabels_removed.pdf" flags: e_ptlinearized];
130 NSLog(@"Done. Result saved in newsletter_with_pagelabels_removed.pdf...");
131 }
132 }
133 @catch(NSException *e)
134 {
135 NSLog(@"%@", e.reason);
136 ret = 1;
137 }
138 [PTPDFNet Terminate: 0];
139 return ret;
140 }
141}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales