Stamper

Sample Obj-C code for using Apryse SDK to programmatically stamp PDF pages with text, images, or with other PDF pages. ElementBuilder and ElementWriter should be used for more complex PDF stamping operations. Learn more about our iOS SDK.

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 following sample shows how to add new content (or watermark) PDF pages
11// using 'pdftron.PDF.Stamper' utility class.
12//
13// Stamper can be used to PDF pages with text, images, or with other PDF content
14// in only a few lines of code. Although Stamper is very simple to use compared
15// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
16// need full control over PDF creation use ElementBuilder/ElementWriter to add
17// new content to existing PDF pages as shown in the ElementBuilder sample project.
18//---------------------------------------------------------------------------------------
19int main(int argc, char * argv[])
20{
21 @autoreleasepool {
22
23 int ret = 0;
24
25 [PTPDFNet Initialize: 0];
26
27 //--------------------------------------------------------------------------------
28 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
29 @try
30 {
31 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
32 [doc InitSecurityHandler];
33 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.5 b: 0.5];
34 [s SetAlignment: e_pthorizontal_center vertical_alignment: e_ptvertical_center];
35 PTColorPt *red = [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0]; // set text color to red
36 [s SetFontColor: red];
37 PTPageSet *set = [[PTPageSet alloc] initWithRange_start: 1 range_end: [doc GetPageCount] filter: e_ptall];
38 [s StampText: doc src_txt: @"If you are reading this\nthis is an even page" dest_pages: set];
39 //delete all text stamps in even pages
40 PTPageSet *set2 = [[PTPageSet alloc] initWithRange_start: 1 range_end: [doc GetPageCount] filter: e_ptodd];
41 [PTStamper DeleteStamps: doc page_set: set2];
42
43 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex1.pdf" flags: e_ptlinearized];
44 }
45 @catch (NSException *e)
46 {
47 NSLog(@"%@", e.reason);
48 ret = 1;
49 }
50
51 //--------------------------------------------------------------------------------
52 // Example 2) Add Image stamp to first 2 pages.
53 @try
54 {
55 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
56 [doc InitSecurityHandler];
57
58 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.05 b: 0.05];
59 PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
60 [s SetSize: e_ptrelative_scale a: 0.5 b: 0.5];
61 //set position of the image to the center, left of PDF pages
62 [s SetAlignment: e_pthorizontal_left vertical_alignment: e_ptvertical_center];
63 PTColorPt *pt = [[PTColorPt alloc] initWithX: 0 y: 0 z: 0 w: 0];
64 [s SetFontColor: pt];
65 [s SetRotation: 180];
66 [s SetAsBackground: NO];
67 //only stamp first 2 pages
68 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: 2 filter: e_ptall];
69 [s StampImage: doc src_img: img dest_pages: ps];
70
71 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex2.pdf" flags: e_ptlinearized];
72 }
73 @catch (NSException *e)
74 {
75 NSLog(@"%@", e.reason);
76 ret = 1;
77 }
78
79 //--------------------------------------------------------------------------------
80 // Example 3) Add Page stamp to all pages.
81 @try
82 {
83 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
84 [doc InitSecurityHandler];
85
86 PTPDFDoc *fish_doc = [[PTPDFDoc alloc ] initWithFilepath: @"../../TestFiles/fish.pdf"];
87 [fish_doc InitSecurityHandler];
88
89 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.5 b: 0.5];
90 PTPage *src_page = [fish_doc GetPage: 1];
91 PTPDFRect * page_one_crop = [src_page GetCropBox];
92 // set size of the image to 10% of the original while keep the old aspect ratio
93 [s SetSize: e_ptabsolute_size a: [page_one_crop Width] * 0.1 b: -1];
94 [s SetOpacity: 0.4];
95 [s SetRotation: -67];
96 //put the image at the bottom right hand corner
97 [s SetAlignment: e_pthorizontal_right vertical_alignment: e_ptvertical_bottom];
98 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 2 range_end: [doc GetPageCount] filter: e_ptall];
99 [s StampPage: doc src_page: src_page dest_pages: ps];
100
101 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex3.pdf" flags: e_ptlinearized];
102 }
103 @catch (NSException *e)
104 {
105 NSLog(@"%@", e.reason);
106 ret = 1;
107 }
108
109 //--------------------------------------------------------------------------------
110 // Example 4) Add Image stamp to first 20 odd pages.
111 @try
112 {
113 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
114 [doc InitSecurityHandler];
115
116 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptabsolute_size a: 20 b: 20];
117 [s SetOpacity: 1];
118 [s SetRotation: 45];
119 [s SetAsBackground: YES];
120 [s SetPosition: 30 vertical_distance: 40 use_percentage: NO];
121 PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
122 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: 20 filter: e_ptodd];
123 [s StampImage: doc src_img: img dest_pages: ps];
124
125 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex4.pdf" flags: e_ptlinearized];
126 }
127 @catch (NSException *e)
128 {
129 NSLog(@"%@", e.reason);
130 ret = 1;
131 }
132
133 //--------------------------------------------------------------------------------
134 // Example 5) Add text stamp to first 20 even pages
135 @try
136 {
137 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
138 [doc InitSecurityHandler];
139
140 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.5 b: 0.5];
141 [s SetPosition: 0 vertical_distance: 0 use_percentage: NO];
142 [s SetOpacity: 0.7];
143 [s SetRotation: 90];
144 [s SetSize: e_pts_font_size a: 80 b: -1];
145 [s SetTextAlignment: e_ptalign_center];
146 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: 20 filter: e_pteven];
147 [s StampText: doc src_txt: @"Goodbye\nMoon" dest_pages: ps];
148
149 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex5.pdf" flags: e_ptlinearized];
150 }
151 @catch (NSException* e)
152 {
153 NSLog(@"%@", e.reason);
154 ret = 1;
155 }
156
157 //--------------------------------------------------------------------------------
158 // Example 6) Add first page as stamp to all even pages
159 @try
160 {
161 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
162 [doc InitSecurityHandler];
163
164 PTPDFDoc *fish_doc = [[PTPDFDoc alloc ] initWithFilepath: @"../../TestFiles/fish.pdf"];
165 [fish_doc InitSecurityHandler];
166
167 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.3 b: 0.3];
168 [s SetOpacity: 1];
169 [s SetRotation: 270];
170 [s SetAsBackground: YES];
171 [s SetPosition: 0.5 vertical_distance: 0.5 use_percentage: YES];
172 [s SetAlignment: e_pthorizontal_left vertical_alignment: e_ptvertical_bottom];
173 PTPage *page_one = [fish_doc GetPage: 1];
174 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: [doc GetPageCount] filter: e_pteven];
175 [s StampPage: doc src_page: page_one dest_pages: ps];
176
177 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex6.pdf" flags: e_ptlinearized];
178 }
179 @catch (NSException* e)
180 {
181 NSLog(@"%@", e.reason);
182 ret = 1;
183 }
184
185 //--------------------------------------------------------------------------------
186 // Example 7) Add image stamp at top right corner in every pages
187 @try
188 {
189 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
190 [doc InitSecurityHandler];
191
192 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.1 b: 0.1];
193 [s SetOpacity: 0.8];
194 [s SetRotation: 135];
195 [s SetAsBackground: NO];
196 [s ShowsOnPrint: NO];
197 [s SetAlignment: e_pthorizontal_left vertical_alignment: e_ptvertical_top];
198 [s SetPosition: 10 vertical_distance: 10 use_percentage: YES];
199
200 PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
201 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: [doc GetPageCount] filter: e_ptall];
202 [s StampImage: doc src_img: img dest_pages: ps];
203
204 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex7.pdf" flags: e_ptlinearized];
205 }
206 @catch (NSException* e)
207 {
208 NSLog(@"%@", e.reason);
209 ret = 1;
210 }
211
212 //--------------------------------------------------------------------------------
213 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
214 // Because text stamp is set as background, the image is top of the text
215 // stamp. Text stamp on the first page is not visible.
216 @try
217 {
218 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
219 [doc InitSecurityHandler];
220
221 PTStamper *s = [[PTStamper alloc] initWithSize_type: e_ptrelative_scale a: 0.07 b: -0.1];
222 [s SetAlignment: e_pthorizontal_right vertical_alignment: e_ptvertical_bottom];
223 [s SetAlignment: e_pthorizontal_center vertical_alignment: e_ptvertical_top];
224 [s SetFont: [PTFont Create: [doc GetSDFDoc] type: e_ptcourier embed: YES]];
225 PTColorPt *red = [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0];
226 [s SetFontColor: red]; //set color to red
227 [s SetTextAlignment: e_ptalign_right];
228 [s SetAsBackground: YES]; //set text stamp as background
229 PTPageSet *ps = [[PTPageSet alloc] initWithRange_start: 1 range_end: 2 filter: e_ptall];
230 [s StampText: doc src_txt: @"This is a title!" dest_pages: ps];
231
232 PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"];
233 [s SetAsBackground: NO]; // set image stamp as foreground
234 PTPageSet *first_page_ps = [[PTPageSet alloc] initWithOne_page: 1];
235 [s StampImage: doc src_img: img dest_pages: first_page_ps];
236
237 [doc SaveToFile: @"../../TestFiles/Output/newsletter.ex8.pdf" flags: e_ptlinearized];
238 }
239 @catch (NSException* e)
240 {
241 NSLog(@"%@", e.reason);
242 ret = 1;
243 }
244 [PTPDFNet Terminate: 0];
245 //NSLog(@"Done.");
246 return ret;
247 }
248}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales