Sample Obj-C code for using Apryse SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with interactive PDF editor widgets. Some of Apryse SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. Learn more about our iOS SDK and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2012 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 code shows how to edit the page display list and how to modify graphics state
11// attributes on existing Elements. In particular the sample program strips all images from
12// the page, changes path fill color to red, and changes text color to blue.
13//---------------------------------------------------------------------------------------
14
15static void ProcessElementEditTestElements(PTElementReader *reader, PTElementWriter *writer, NSMutableSet *visited)
16{
17 PTElement *element;
18 while ((element = [reader Next])) // Read page contents
19 {
20 switch ([element GetType])
21 {
22 case e_ptimage:
23 case e_ptinline_image:
24 // remove all images by skipping them
25 break;
26 case e_ptpath:
27 {
28 // Set all paths to red color.
29 PTGState *gs = [element GetGState];
30 [gs SetFillColorSpace: [PTColorSpace CreateDeviceRGB]];
31 PTColorPt *cp = [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0];
32 [gs SetFillColorWithColorPt: cp];
33 [writer WriteElement: element];
34 break;
35 }
36 case e_pttext_obj:
37 {
38 // Set all text to blue color.
39 PTGState *gs = [element GetGState];
40 [gs SetFillColorSpace: [PTColorSpace CreateDeviceRGB]];
41 PTColorPt *cp = [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0];
42 [gs SetFillColorWithColorPt: cp];
43 [writer WriteElement: element];
44 break;
45 }
46 case e_ptform:
47 {
48 [writer WriteElement: element]; // write Form XObject reference to current stream
49
50 PTObj *form_obj = [element GetXObject];
51
52 if (![visited containsObject:@([form_obj GetObjNum])]) // if this XObject has not been processed
53 {
54 // recursively process the Form XObject
55 [visited addObject:@([form_obj GetObjNum])];
56 PTElementWriter *new_writer = [[PTElementWriter alloc] init];
57 [reader FormBegin];
58 [new_writer WriterBeginWithSDFObj:form_obj compress:YES resources: NULL];
59
60 [reader ClearChangeList];
61 [new_writer SetDefaultGState: reader];
62
63 ProcessElementEditTestElements(reader, new_writer, visited);
64 [reader End];
65 [new_writer End];
66 }
67
68 break;
69 }
70 default:
71 [writer WriteElement: element];
72 }
73 }
74}
75
76int main(int argc, char *argv[])
77{
78 @autoreleasepool {
79
80 int ret = 0;
81 [PTPDFNet Initialize: 0];
82
83 @try
84 {
85 NSLog(@"Opening the input file...");
86 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
87 [doc InitSecurityHandler];
88
89 PTElementWriter *writer = [[PTElementWriter alloc] init];
90 PTElementReader *reader = [[PTElementReader alloc] init];
91 NSMutableSet *visited = [[NSMutableSet alloc] init];
92
93 PTPageIterator *itr = [doc GetPageIterator: 1];
94 while ([itr HasNext])
95 {
96 PTPage *page = [itr Current];
97 [visited addObject:@([[page GetSDFObj] GetObjNum])];
98
99 [reader ReaderBeginWithPage: page ocg_context: 0];
100 [writer WriterBeginWithPage: page placement: e_ptreplacement page_coord_sys: NO compress: YES resources: [page GetResourceDict] ];
101
102 ProcessElementEditTestElements(reader, writer, visited);
103
104 [writer End];
105 [reader End];
106
107 [itr Next];
108 }
109
110 // Save modified document
111 [doc SaveToFile: @"../../TestFiles/Output/newsletter_edited.pdf" flags: e_ptremove_unused];
112 NSLog(@"Done. Result saved in newsletter_edited.pdf...");
113 }
114 @catch(NSException *e)
115 {
116 NSLog(@"Caught PDFNet exception: %@", e.reason);
117 ret = 1;
118 }
119 [PTPDFNet Terminate: 0];
120 return ret;
121
122 }
123
124}
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 code shows how to edit the page display list and how to modify graphics state
11// attributes on existing Elements. In particular the sample program strips all images from
12// the page, changes path fill color to red, and changes text color to blue.
13//---------------------------------------------------------------------------------------
14
15private func ProcessElementEditTestElements(reader: PTElementReader, writer: PTElementWriter, visited: NSMutableSet) {
16 while let element = reader.next() {
17 switch element.getType() {
18 case e_ptimage, e_ptinline_image:
19 // remove all images by skipping them
20 break
21 case e_ptpath:
22 // Set all paths to red color.
23 let gs: PTGState = element.getGState()
24 gs.setFill(PTColorSpace.createDeviceRGB())
25 let cp = PTColorPt(x: 1, y: 0, z: 0, w: 0)
26 gs.setFillColor(with: cp)
27 writer.write(element)
28 case e_pttext_obj:
29 // Set all text to blue color.
30 let gs: PTGState = element.getGState()
31 gs.setFill(PTColorSpace.createDeviceRGB())
32 let cp = PTColorPt(x: 0, y: 0, z: 1, w: 0)
33 gs.setFillColor(with: cp)
34 writer.write(element)
35 case e_ptform:
36 writer.write(element) // write Form XObject reference to current stream
37
38 let form_obj: PTObj = element.getXObject()
39
40 if !visited.contains(form_obj.getNum()) {
41 // recursively process the Form XObject
42 visited.add(form_obj.getNum())
43 let new_writer: PTElementWriter = PTElementWriter()
44 reader.formBegin()
45 new_writer.writerBegin(withSDFObj: form_obj, compress: true, resources: nil)
46 ProcessElementEditTestElements(reader: reader, writer: new_writer, visited: visited)
47 reader.end()
48 new_writer.end()
49 }
50 default:
51 writer.write(element)
52 }
53 }
54}
55
56func runElementEditTest() -> Int {
57 return autoreleasepool {
58 var ret: Int = 0
59
60
61 do {
62 try PTPDFNet.catchException {
63 print("Opening the input file...")
64 let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
65 doc.initSecurityHandler()
66
67 let writer: PTElementWriter = PTElementWriter()
68 let reader: PTElementReader = PTElementReader()
69 let visited = NSMutableSet()
70
71 let itr: PTPageIterator = doc.getPageIterator(1)
72 while itr.hasNext() {
73 let page: PTPage = itr.current()
74 visited.add(page.getSDFObj().getNum())
75
76 reader.readerBegin(with: page, ocg_context: nil)
77 writer.begin(page, placement: e_ptreplacement, page_coord_sys: false, compress: true)
78
79 ProcessElementEditTestElements(reader: reader, writer: writer, visited: visited)
80
81 writer.end()
82 reader.end()
83
84 itr.next()
85 }
86
87 // Save modified document
88 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_edited.pdf").path, flags: e_ptremove_unused.rawValue)
89 print("Done. Result saved in newsletter_edited.pdf...")
90 }
91 } catch let e as NSError {
92 print("Caught PDFNet exception: \(e)")
93 ret = 1
94 }
95
96 return ret
97 }
98}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales