ElementEdit

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales