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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
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//-----------------------------------------------------------------------------------
21func runPageLabelsTest() -> Int {
22 return autoreleasepool {
23 var ret: Int = 0
24
25
26 do {
27 try PTPDFNet.catchException {
28 //-----------------------------------------------------------
29 // Example 1: Add page labels to an existing or newly created PDF
30 // document.
31 //-----------------------------------------------------------
32 do {
33 let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
34 doc.initSecurityHandler()
35
36 // Create a page labeling scheme that starts with the first page in
37 // the document (page 1) and is using uppercase roman numbering
38 // style.
39 let L1 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptroman_uppercase, prefix: "My Prefix ", start_at: 1)
40 doc.setPageLabel(1, label: L1)
41
42 // Create a page labeling scheme that starts with the fourth page in
43 // the document and is using decimal Arabic numbering style.
44 // Also the numeric portion of the first label should start with number
45 // 4 (otherwise the first label would be "My Prefix 1").
46 let L2 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptdecimal, prefix: "My Prefix ", start_at: 4)
47 doc.setPageLabel(4, label: L2)
48
49 // Create a page labeling scheme that starts with the seventh page in
50 // the document and is using alphabetic numbering style. The numeric
51 // portion of the first label should start with number 1.
52 let L3 = PTPageLabel.create(doc.getSDFDoc(), style: e_ptalphabetic_uppercase, prefix: "My Prefix ", start_at: 1)
53 doc.setPageLabel(7, label: L3)
54
55 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path, flags: e_ptlinearized.rawValue)
56 print("Done. Result saved in newsletter_with_pagelabels.pdf...")
57 }
58
59 //-----------------------------------------------------------
60 // Example 2: Read page labels from an existing PDF document.
61 //-----------------------------------------------------------
62 do {
63 let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
64 doc.initSecurityHandler()
65
66 let page_num = doc.getPageCount()
67
68 for i in 1...page_num {
69 print("Page number: \(i)")
70 let label: PTPageLabel = doc.getPageLabel(i)
71 if label.isValid() {
72 print(" Label: \(String(describing: label.getTitle(i)))")
73 }
74 else {
75 print(" No Label.")
76 }
77 }
78 }
79
80 //-----------------------------------------------------------
81 // Example 3: Modify page labels from an existing PDF document.
82 //-----------------------------------------------------------
83 do {
84 let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
85 doc.initSecurityHandler()
86
87 // Remove the alphabetic labels from example 1.
88 doc.removePageLabel(7)
89
90 // Replace the Prefix in the decimal labels (from example 1).
91 var label: PTPageLabel = doc.getPageLabel(4)
92 if label.isValid() {
93 label.setPrefix("A")
94 label.setStart(1)
95 }
96
97 // Add a new label
98 let new_label = PTPageLabel.create(doc.getSDFDoc(), style: e_ptdecimal, prefix: "B", start_at: 1)
99 doc.setPageLabel(10, label: new_label) // starting from page 10.
100
101 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels_modified.pdf").path, flags: e_ptlinearized.rawValue)
102 print("Done. Result saved in newsletter_with_pagelabels_modified.pdf...")
103
104 let page_num = doc.getPageCount()
105
106 for i in 1...page_num {
107 print("Page number: \(i)")
108 label = doc.getPageLabel(i)
109 if label.isValid() {
110 print(" Label: \(String(describing: label.getTitle(i)))")
111 }
112 else {
113 print(" No Label.")
114 }
115 }
116 }
117
118 //-----------------------------------------------------------
119 // Example 4: Delete all page labels in an existing PDF document.
120 //-----------------------------------------------------------
121 do {
122 let doc: PTPDFDoc = PTPDFDoc(filepath: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels.pdf").path)
123 doc.getRoot().eraseDictElement(withKey: "PageLabels")
124
125 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_with_pagelabels_removed.pdf").path, flags: e_ptlinearized.rawValue)
126 print("Done. Result saved in newsletter_with_pagelabels_removed.pdf...")
127 }
128 }
129 } catch let e as NSError {
130 print("\(e)")
131 ret = 1
132 }
133
134 return ret
135 }
136}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales