Sample Obj-C code to use Apryse SDK for adding or editing PDF annotations. The annotation types included in this sample are: hyperlink, intra-document link, stamp, rubber stamp, file attachment, sound, text, free-text, line, circle, square, polygon, polyline, highlight, squiggly, caret, and ink. Learn more about our iOS SDK and PDF Annotation 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//std::string output_path = "../../TestFiles/Output/";
10//std::string input_path = "../../TestFiles/";
11
12void AnnotationHighLevelAPI(PTPDFDoc *doc)
13{
14 // The following code snippet traverses all annotations in the document
15 NSLog(@"Traversing all annotations in the document...");
16
17 int page_num = 1;
18 PTPageIterator* itr;
19 for (itr = [doc GetPageIterator: 1]; [itr HasNext]; [itr Next])
20 {
21 NSLog(@"Page %d: ", page_num++);
22
23 PTPage* page = [itr Current];
24 int num_annots = [page GetNumAnnots];
25 int i = 0;
26 for (i=0; i<num_annots; ++i)
27 {
28 PTAnnot* annot = [page GetAnnot: i];
29 if (![annot IsValid]) continue;
30 NSLog(@"Annot Type: %@", [[[[annot GetSDFObj] Get: @"Subtype"] Value] GetName]);
31
32 PTPDFRect* bbox = [annot GetRect];
33
34 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
35 formatter.numberStyle = NSNumberFormatterDecimalStyle;
36 formatter.maximumFractionDigits = 4;
37 NSString *x1 = [formatter stringFromNumber:[NSNumber numberWithDouble:[bbox GetX1]]];
38 NSString *y1 = [formatter stringFromNumber:[NSNumber numberWithDouble:[bbox GetY1]]];
39 NSString *x2 = [formatter stringFromNumber:[NSNumber numberWithDouble:[bbox GetX2]]];
40 NSString *y2 = [formatter stringFromNumber:[NSNumber numberWithDouble:[bbox GetY2]]];
41
42 NSLog(@" Position: %s, %s, %s, %s", [x1 UTF8String], [y1 UTF8String], [x2 UTF8String], [y2 UTF8String]);
43
44 switch ([annot GetType])
45 {
46 case e_ptLink:
47 {
48 PTLink *link = [[PTLink alloc] initWithAnn: annot];
49 PTAction* action = [link GetAction];
50 if (![action IsValid])
51 {
52 continue;
53 }
54 if ([action GetType] == e_ptGoTo)
55 {
56 PTDestination* dest = [action GetDest];
57 if (![dest IsValid]) {
58 NSLog(@" Destination is not valid\n");
59 }
60 else {
61 int page_num = [[dest GetPage] GetIndex];
62 NSLog(@" Links to: page number %d in this document\n", page_num);
63 }
64 }
65 else if ([action GetType] == e_ptURI)
66 {
67 NSString *uri = [[[[action GetSDFObj] Get: @"URI"] Value] GetAsPDFText];
68 NSLog(@" Links to: %@\n", uri);
69 }
70 // ...
71 }
72 break;
73 case e_ptWidget:
74 break;
75 case e_ptFileAttachment:
76 break;
77 // ...
78 default:
79 break;
80 }
81 }
82 }
83
84 // Use the high-level API to create new annotations.
85 PTPage *first_page = [doc GetPage:1];
86
87 // Create a hyperlink...
88 PTLink *hyperlink = [PTLink CreateWithAction: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 85 y1: 570 x2: 503 y2: 524] action: [PTAction CreateURI: [doc GetSDFDoc] uri: @"http://www.pdftron.com"]];
89 [first_page AnnotPushBack: hyperlink];
90
91 // Create an intra-document link...
92 PTAction *goto_page_3 = [PTAction CreateGoto: [PTDestination CreateFitH: [doc GetPage:3] top: 0]];
93 PTLink *link = [PTLink CreateWithAction: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 85 y1: 458 x2: 503 y2: 502] action: goto_page_3];
94
95 [link SetColor: [[PTColorPt alloc] initWithX: 0 y:0 z:1 w:0] numcomp: 3];
96
97 // Add the new annotation to the first page
98 [first_page AnnotPushBack: link];
99
100 // Create a stamp annotation ...
101 PTRubberStamp *stamp = [PTRubberStamp Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 30 y1: 30 x2: 300 y2: 200] icon: e_ptDraft];
102 [stamp SetRubberStampIconName: @"Draft"];
103 [first_page AnnotPushBack: stamp];
104
105 // Create a file attachment annotation (embed the 'peppers.jpg').
106 PTFileAttachment *file_attach = [PTFileAttachment CreateFileAttchWithPath: [doc GetSDFDoc] pos:[[PTPDFRect alloc] initWithX1: 80 y1: 280 x2: 108 y2: 320] path: @"../../TestFiles/peppers.jpg" icon_name: e_ptPushPin];
107 [first_page AnnotPushBack: file_attach];
108
109 PTInk* ink = [PTInk Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 110 y1: 10 x2: 300 y2: 200]];
110 PTPDFPoint* pt3 = [[PTPDFPoint alloc] initWithPx: 110 py: 10];
111 //pt3.x = 110; pt3.y = 10;
112 [ink SetPoint: 0 pointindex: 0 pt: pt3];
113 [pt3 setX: 150]; [pt3 setY: 50];
114 [ink SetPoint: 0 pointindex: 1 pt: pt3];
115 [pt3 setX: 190]; [pt3 setY: 60];
116 [ink SetPoint: 0 pointindex: 2 pt: pt3];
117 [pt3 setX: 180]; [pt3 setY: 90];
118 [ink SetPoint: 1 pointindex: 0 pt: pt3];
119 [pt3 setX: 190]; [pt3 setY: 95];
120 [ink SetPoint: 1 pointindex: 1 pt: pt3];
121 [pt3 setX: 200]; [pt3 setY: 100];
122 [ink SetPoint: 1 pointindex: 2 pt: pt3];
123 [pt3 setX: 166]; [pt3 setY: 86];
124 [ink SetPoint: 2 pointindex: 0 pt: pt3];
125 [pt3 setX: 196]; [pt3 setY: 96];
126 [ink SetPoint: 2 pointindex: 1 pt: pt3];
127 [pt3 setX: 221]; [pt3 setY: 121];
128 [ink SetPoint: 2 pointindex: 2 pt: pt3];
129 [pt3 setX: 288]; [pt3 setY: 188];
130 [ink SetPoint: 2 pointindex: 3 pt: pt3];
131 [ink SetColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 1 w: 0] numcomp: 3];
132 [first_page AnnotPushBack: ink];
133
134 //Create a Polygon annotation..
135 //Polygon *poly2 = [Polygon Create: doc PDFRect[100, 60, 300, 480]];
136 //Point *pllp;
137 //pllp.x=105;
138 //pllp.y=68;
139 //poly2.SetVertex(0, pllp);
140 //pllp.x=155;
141 //pllp.y=92;
142 //poly2.SetVertex(1, pllp);
143 //pllp.x=189;
144 //pllp.y=133;
145 //poly2.SetVertex(2, pllp);
146 //pllp.x=200;
147 //pllp.y=140;
148 //poly2.SetVertex(3, pllp);
149 //pllp.x=230;
150 //pllp.y=389;
151 //poly2.SetVertex(4, pllp);
152 //pllp.x=300;
153 //pllp.y=405;
154 //poly2.SetVertex(5, pllp);
155 //poly2.SetColor(ColorPt(0, 0, 1), 3);
156 //poly2.SetInteriorColor(ColorPt(1, 0, 0), 3);
157 //first_page.AnnotPushBack(poly2);
158
159 // ...
160}
161
162void AnnotationLowLevelAPI(PTPDFDoc *doc)
163{
164 PTPage* page = [doc GetPage: 1];
165
166 PTObj* annots = [page GetAnnots];
167
168 if (!annots)
169 {
170 // If there are no annotations, create a new annotation
171 // array for the page.
172 annots = [doc CreateIndirectArray];
173 [[page GetSDFObj] Put: @"Annots" obj:annots];
174 }
175
176 // Create a Text annotation
177 PTObj * annot = [doc CreateIndirectDict];
178 [annot PutName: @"Subtype" name: @"Text"];
179 [annot PutBool: @"Open" value: YES];
180 [annot PutString: @"Contents" value: @"The quick brown fox ate the lazy mouse."];
181 [annot PutRect: @"Rect" x1:266 y1:116 x2:430 y2:204];
182
183 // Insert the annotation in the page annotation array
184 [annots PushBack:annot ];
185
186 // Create a Link annotation
187 PTObj * link1 = [doc CreateIndirectDict];
188 [link1 PutName: @"Subtype" name: @"Link"];
189 PTDestination *dest = [PTDestination CreateFit: [doc GetPage: 2]];
190 [link1 Put: @"Dest" obj:[dest GetSDFObj]];
191 [link1 PutRect: @"Rect" x1:85 y1:705 x2:503 y2:661];
192 [annots PushBack: link1];
193
194 // Create another Link annotation
195 PTObj * link2 = [doc CreateIndirectDict];
196 [link2 PutName: @"Subtype" name: @"Link"];
197 PTDestination *dest2 = [PTDestination CreateFit: [doc GetPage:3]];
198 [link2 Put: @"Dest" obj:[dest2 GetSDFObj]];
199 [link2 PutRect: @"Rect" x1:85 y1:638 x2:503 y2:594];
200 [annots PushBack: link2];
201
202 // Note that PDFNet API can be used to modify existing annotations.
203 // In the following example we will modify the second link annotation
204 // (link2) so that it points to the 10th page. We also use a different
205 // destination page fit type.
206
207 // link2 = annots.GetAt(annots.Size()-1);
208 [link2 Put: @"Dest" obj: [[PTDestination CreateXYZ: [doc GetPage: 10] left:100 top:792-70 zoom:10] GetSDFObj]];
209
210 // Create a third link annotation with a hyperlink action (all other
211 // annotation types can be created in a similar way)
212 PTObj * link3 = [doc CreateIndirectDict];
213 [link3 PutName: @"Subtype" name: @"Link"];
214 [link3 PutRect: @"Rect" x1:85 y1:570 x2:503 y2:524];
215
216 // Create a URI action
217 PTObj * action = [link3 PutDict: @"A"];
218 [action PutName: @"S" name: @"URI"];
219 [action PutString: @"URI" value: @"http://www.pdftron.com"];
220
221 [annots PushBack: link3];
222}
223
224void CreateTestAnnots(PTPDFDoc *doc)
225{
226 PTElementWriter *ew = [[PTElementWriter alloc] init];
227 PTElementBuilder *eb = [[PTElementBuilder alloc] init];
228 PTElement *element = [[PTElement alloc] init];
229
230 PTPage *first_page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
231 [doc PagePushBack: first_page];
232 [ew WriterBeginWithPage: first_page placement: e_ptoverlay page_coord_sys: NO compress: NO resources: NULL]; // begin writing to this page
233 [ew End]; // save changes to the current page
234
235 //
236 // Test of a free text annotation.
237 //
238 {
239 PTFreeText *txtannot = [PTFreeText Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10 y1: 400 x2: 160 y2: 570]];
240 [txtannot SetContents: @"\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
241 "\n\nAha!\n\nAnd there was much rejoicing!"];
242 //std::vector<double> dash( 2, 2.0 );
243 [txtannot SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 1 b_hr: 10 b_vr: 20] oldStyleOnly: YES];
244 [txtannot SetQuaddingFormat: 0];
245 [first_page AnnotPushBack: txtannot];
246 [txtannot RefreshAppearance];
247 }
248 {
249 PTFreeText *txtannot = [PTFreeText Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 100 x2: 350 y2: 500]];
250 [txtannot SetContentRect: [[PTPDFRect alloc] initWithX1: 200 y1: 200 x2: 350 y2: 500]];
251 [txtannot SetContents: @"\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
252 "\n\nAha!\n\nAnd there was much rejoicing!"];
253 [txtannot SetCalloutLinePointsWithKneePoint: [[PTPDFPoint alloc] initWithPx: 200 py: 300] p2: [[PTPDFPoint alloc] initWithPx: 150 py: 290] p3: [[PTPDFPoint alloc] initWithPx: 110 py: 110]];
254 //std::vector<double> dash( 2, 2.0 );
255 [txtannot SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 1 b_hr: 10 b_vr: 20 ] oldStyleOnly: YES];
256 [txtannot SetEndingStyle: e_ptClosedArrow];
257 [txtannot SetColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] numcomp: 3];
258 [txtannot SetQuaddingFormat: 1];
259 [first_page AnnotPushBack: txtannot];
260 [txtannot RefreshAppearance];
261 }
262 {
263 PTFreeText *txtannot = [PTFreeText Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 400 y1: 10 x2: 550 y2: 400]];
264 [txtannot SetContents: @"\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
265 "\n\nAha!\n\nAnd there was much rejoicing!"];
266 [txtannot SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 1 b_hr: 10 b_vr: 20 ] oldStyleOnly: YES];
267 [txtannot SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
268 [txtannot SetOpacity: 0.2 ];
269 [txtannot SetQuaddingFormat: 2];
270 [first_page AnnotPushBack: txtannot];
271 [txtannot RefreshAppearance];
272 }
273
274 PTPage *page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
275 [doc PagePushBack: page];
276 [ew WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to this page
277 [eb Reset: [[PTGState alloc] init]]; // Reset the GState to default
278 [ew End]; // save changes to the current page
279
280 {
281 //Create a Line annotation...
282 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 250 y1: 250 x2: 400 y2: 400]];
283 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 350 py: 270]];
284 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 260 py: 370]];
285 [line SetStartStyle: e_ptl_Square];
286 [line SetEndStyle: e_ptl_Circle];
287 [line SetColor: [[PTColorPt alloc] initWithX: 0.3 y: 0.5 z: 0 w: 0] numcomp: 3];
288 [line SetContents: @"Dashed Captioned"];
289 [line SetShowCaption: YES];
290 [line SetCaptionPosition: e_ptTop ];
291 NSMutableArray *dash = [NSMutableArray arrayWithCapacity: 2];
292 [dash addObject: @2.0];
293 [dash addObject: @2.0];
294 [line SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptdashed b_width: 2 b_hr: 0 b_vr: 0 b_dash: dash] oldStyleOnly: NO];
295 [line RefreshAppearance];
296 [page AnnotPushBack: line];
297 }
298 {
299 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 347 y1: 377 x2: 600 y2: 600]];
300 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 385 py: 410]];
301 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 540 py: 555]];
302 [line SetStartStyle: e_ptl_Circle];
303 [line SetEndStyle: e_ptOpenArrow];
304 [line SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
305 [line SetContents: @"Inline Caption"];
306 [line SetShowCaption: YES];
307 [line SetCaptionPosition: e_ptInline ];
308 [line SetLeaderLineExtensionLength: 4];
309 [line SetLeaderLineLength: -12 ];
310 [line SetLeaderLineOffset: 2];
311 [line RefreshAppearance];
312 [page AnnotPushBack: line];
313 }
314 {
315 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10 y1: 400 x2: 200 y2: 600]];
316 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 25 py: 426]];
317 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 180 py: 555]];
318 [line SetStartStyle: e_ptl_Circle];
319 [line SetEndStyle: e_ptl_Square];
320 [line SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
321 [line SetInteriorColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] CompNum: 3];
322 [line SetContents: @"Offset Caption"];
323 [line SetShowCaption: YES];
324 [line SetCaptionPosition: e_ptTop ];
325 [line SetTextHOffset: -60];
326 [line SetTextVOffset: 10];
327 [line RefreshAppearance];
328 [page AnnotPushBack: line];
329 }
330 {
331 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 200 y1: 10 x2: 400 y2: 70]];
332 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 220 py: 25]];
333 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 370 py: 60]];
334 [line SetStartStyle: e_ptButt];
335 [line SetEndStyle: e_ptOpenArrow];
336 [line SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
337 [line SetContents: @"Regular Caption"];
338 [line SetShowCaption: YES];
339 [line SetCaptionPosition: e_ptTop ];
340 [line RefreshAppearance];
341 [page AnnotPushBack: line];
342 }
343 {
344 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 200 y1: 70 x2: 400 y2: 130]];
345 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 220 py: 111]];
346 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 370 py: 78]];
347 [line SetStartStyle: e_ptl_Circle];
348 [line SetEndStyle: e_ptDiamond];
349 [line SetContents: @"Circle to Diamond"];
350 [line SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
351 [line SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] CompNum: 3];
352 [line SetShowCaption: YES];
353 [line SetCaptionPosition: e_ptTop];
354 [line RefreshAppearance];
355 [page AnnotPushBack: line];
356 }
357 {
358 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10 y1: 100 x2: 160 y2: 200]];
359 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 15 py: 110]];
360 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 150 py: 190]];
361 [line SetStartStyle: e_ptSlash];
362 [line SetEndStyle: e_ptClosedArrow];
363 [line SetContents: @"Slash to CArrow"];
364 [line SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
365 [line SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 1 w: 0] CompNum: 3];
366 [line SetShowCaption: YES];
367 [line SetCaptionPosition: e_ptTop];
368 [line RefreshAppearance];
369 [page AnnotPushBack: line];
370 }
371 {
372 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 270 y1: 270 x2: 570 y2: 433]];
373 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 300 py: 400]];
374 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 550 py: 300]];
375 [line SetStartStyle: e_ptRClosedArrow];
376 [line SetEndStyle: e_ptROpenArrow];
377 [line SetContents: @"ROpen & RClosed arrows"];
378 [line SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
379 [line SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] CompNum: 3];
380 [line SetShowCaption: YES];
381 [line SetCaptionPosition: e_ptTop];
382 [line RefreshAppearance];
383 [page AnnotPushBack: line];
384 }
385 {
386 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 195 y1: 395 x2: 205 y2: 505]];
387 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 200 py: 400]];
388 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 200 py: 500]];
389 [line RefreshAppearance];
390 [page AnnotPushBack: line];
391 }
392 {
393 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 55 y1: 299 x2: 150 y2: 301]];
394 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 55 py: 300]];
395 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 155 py: 300]];
396 [line SetStartStyle: e_ptl_Circle];
397 [line SetEndStyle: e_ptl_Circle];
398 [line SetContents: @"Caption that's longer than its line."];
399 [line SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 1 w: 0] numcomp: 3];
400 [line SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] CompNum: 3];
401 [line SetShowCaption: YES];
402 [line SetCaptionPosition: e_ptTop];
403 [line RefreshAppearance];
404 [page AnnotPushBack: line];
405 }
406 {
407 PTLineAnnot *line=[PTLineAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 300 y1: 200 x2: 290 y2: 234]];
408 [line SetStartPoint: [[PTPDFPoint alloc] initWithPx: 310 py: 210]];
409 [line SetEndPoint: [[PTPDFPoint alloc] initWithPx: 380 py: 220]];
410 [line SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 0 w: 0] numcomp: 3];
411 [line RefreshAppearance];
412 [page AnnotPushBack: line];
413 }
414
415 PTPage *page3 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
416 [ew WriterBeginWithPage: page3 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
417 [ew End]; // save changes to the current page
418 [doc PagePushBack: page3];
419 {
420 PTCircle *circle=[PTCircle Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 300 y1: 300 x2: 390 y2: 350]];
421 [circle SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 0 w: 0] numcomp: 3];
422 [circle RefreshAppearance];
423 [page3 AnnotPushBack: circle];
424 }
425 {
426 PTCircle *circle=[PTCircle Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 100 x2: 200 y2: 200]];
427 [circle SetColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] numcomp: 3];
428 [circle SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] CompNum: 3];
429 NSMutableArray *dash = [NSMutableArray arrayWithCapacity: 2];
430 [dash addObject: @2.0];
431 [dash addObject: @4.0];
432 [circle SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptdashed b_width: 3 b_hr: 0 b_vr: 0 b_dash: dash] oldStyleOnly: NO];
433 [circle SetPadding: 2];
434 [circle RefreshAppearance];
435 [page3 AnnotPushBack: circle];
436 }
437 {
438 PTSquare *sq = [PTSquare Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10 y1: 200 x2: 80 y2: 300]];
439 [sq SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 0 w: 0] numcomp: 3];
440 [sq RefreshAppearance];
441 [page3 AnnotPushBack: sq];
442 }
443 {
444 PTSquare *sq = [PTSquare Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 500 y1: 200 x2: 580 y2: 300]];
445 [sq SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
446 [sq SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 1 w: 0] CompNum: 3];
447 NSMutableArray *dash = [NSMutableArray arrayWithCapacity: 2];
448 [dash addObject: @4.0];
449 [dash addObject: @2.0];
450 [sq SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptdashed b_width: 6 b_hr: 0 b_vr: 0 b_dash: dash] oldStyleOnly: NO];
451 [sq SetPadding: 4];
452 [sq RefreshAppearance];
453 [page3 AnnotPushBack: sq];
454 }
455 {
456 PTPolygon *poly = [PTPolygon Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 5 y1: 500 x2: 125 y2: 590]];
457 [poly SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
458 [poly SetInteriorColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] CompNum: 3];
459 [poly SetVertex: 0 pt: [[PTPDFPoint alloc] initWithPx: 12 py: 510]];
460 [poly SetVertex: 1 pt: [[PTPDFPoint alloc] initWithPx: 100 py: 510]];
461 [poly SetVertex: 2 pt: [[PTPDFPoint alloc] initWithPx: 100 py: 555]];
462 [poly SetVertex: 3 pt: [[PTPDFPoint alloc] initWithPx: 35 py: 544]];
463 [poly SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 4 b_hr: 0 b_vr: 0] oldStyleOnly: NO];
464 [poly SetPadding: 4];
465 [poly RefreshAppearance];
466 [page3 AnnotPushBack: poly];
467 }
468 {
469 PTPolygon *poly = [PTPolygon Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 400 y1: 10 x2: 500 y2: 90]];
470 [poly SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
471 [poly SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] CompNum: 3];
472 [poly SetVertex: 0 pt: [[PTPDFPoint alloc] initWithPx: 405 py: 20]];
473 [poly SetVertex: 1 pt: [[PTPDFPoint alloc] initWithPx: 440 py: 40]];
474 [poly SetVertex: 2 pt: [[PTPDFPoint alloc] initWithPx: 410 py: 60]];
475 [poly SetVertex: 3 pt: [[PTPDFPoint alloc] initWithPx: 470 py: 80]];
476 [poly SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 2 b_hr: 0 b_vr: 0] oldStyleOnly: NO];
477 [poly SetPadding: 4];
478 [poly SetStartStyle: e_ptRClosedArrow];
479 [poly SetEndStyle: e_ptClosedArrow];
480 [poly RefreshAppearance];
481 [page3 AnnotPushBack: poly];
482 }
483 {
484 PTLink *lk = [PTLink Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 5 y1: 5 x2: 55 y2: 24]];
485 //lk.SetColor( ColorPt(0,1,0), 3 );
486 [lk RefreshAppearance];
487 [page3 AnnotPushBack: lk];
488 }
489
490
491 PTPage *page4 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
492 [ew WriterBeginWithPage: page4 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
493 [ew End]; // save changes to the current page
494 [doc PagePushBack: page4];
495
496 {
497 [ew WriterBeginWithPage: page4 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];
498 PTFont *font = [PTFont Create: [doc GetSDFDoc] type: e_pthelvetica embed: NO];
499 element = [eb CreateTextBeginWithFont: font font_sz: 16];
500 [element SetPathFill: YES];
501 [ew WriteElement: element];
502 element = [eb CreateTextRunWithFont: @"Some random text on the page" font: font font_sz: 16];
503 [element SetTextMatrix: 1 b: 0 c: 0 d: 1 h: 100 v: 500];
504 [ew WriteElement: element];
505 [ew WriteElement: [eb CreateTextEnd]];
506 [ew End];
507 }
508 {
509 PTHighlightAnnot *hl = [PTHighlightAnnot Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 490 x2: 150 y2: 515]];
510 [hl SetColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] numcomp: 3];
511 [hl RefreshAppearance];
512 [page4 AnnotPushBack: hl];
513 }
514 {
515 PTSquiggly *sq = [PTSquiggly Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 450 x2: 250 y2: 600]];
516 //sq.SetColor( ColorPt(1,0,0), 3 );
517 PTPDFPoint *p1 = [[PTPDFPoint alloc] initWithPx: 122 py: 455];
518 PTPDFPoint *p2 = [[PTPDFPoint alloc] initWithPx: 240 py: 545];
519 PTPDFPoint *p3 = [[PTPDFPoint alloc] initWithPx: 230 py: 595];
520 PTPDFPoint *p4 = [[PTPDFPoint alloc] initWithPx: 101 py: 500];
521 [sq SetQuadPoint: 0 qp: [[PTQuadPoint alloc] initWithP11: p1 p22: p2 p33: p3 p44: p4]];
522 [sq RefreshAppearance];
523 [page4 AnnotPushBack: sq];
524 }
525 {
526 PTCaret *cr = [PTCaret Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 40 x2: 129 y2: 69]];
527 [cr SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
528 [cr SetSymbol: @"P"];
529 [cr RefreshAppearance];
530 [page4 AnnotPushBack: cr];
531 }
532
533
534 PTPage *page5 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
535 [ew WriterBeginWithPage: page5 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
536 [ew End]; // save changes to the current page
537 [doc PagePushBack: page5];
538 PTFileSpec *fs = [PTFileSpec Create: [doc GetSDFDoc] path: [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"butterfly.png"] embed: NO];
539 PTPage *page6 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
540 [ew WriterBeginWithPage: page6 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
541 [ew End]; // save changes to the current page
542 [doc PagePushBack: page6];
543
544 int ipage;
545 for( ipage =0; ipage < 2; ++ipage ) {
546 int iann;
547 for( iann =0; iann < 100; iann++ ) {
548 if( ! (iann > e_ptTag) ) {
549 PTFileAttachment *fa = [PTFileAttachment CreateFileAttchWithFileSpec: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 50+50*iann y1: 100 x2: 70+50*iann y2: 120] fs: fs icon_name: iann];
550 if(ipage) [fa SetColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] numcomp: 3];
551 [fa RefreshAppearance];
552 if( ipage == 0 )
553 [page5 AnnotPushBack: fa];
554 else
555 [page6 AnnotPushBack: fa];
556 }
557 if( iann > e_ptNote ) break;
558 PTText *txt = [PTText Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10+iann*50 y1: 200 x2: 30+iann*50 y2: 220]];
559 [txt SetTextIconType: iann];
560 [txt SetContents: [txt GetIconName]];
561 if( ipage ) [txt SetColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] numcomp: 3];
562 [txt RefreshAppearance];
563 if( ipage == 0 )
564 [page5 AnnotPushBack: txt];
565 else
566 [page6 AnnotPushBack: txt];
567 }
568 }
569 {
570 PTText *txt = [PTText Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 10 y1: 20 x2: 30 y2: 40]];
571 [txt SetTextIconName: @"UserIcon"];
572 [txt SetContents: @"User defined icon, unrecognized by appearance generator"];
573 [txt SetColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] numcomp: 3];
574 [txt RefreshAppearance];
575 [page6 AnnotPushBack: txt];
576 }
577 {
578 PTInk *ink = [PTInk Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 400 x2: 200 y2: 550]];
579 [ink SetColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 1 w: 0] numcomp: 3];
580 [ink SetPoint: 1 pointindex: 3 pt: [[PTPDFPoint alloc] initWithPx: 220 py: 505]];
581 [ink SetPoint: 1 pointindex: 0 pt: [[PTPDFPoint alloc] initWithPx: 100 py: 490]];
582 [ink SetPoint: 0 pointindex: 1 pt: [[PTPDFPoint alloc] initWithPx: 120 py: 410]];
583 [ink SetPoint: 0 pointindex: 0 pt: [[PTPDFPoint alloc] initWithPx: 100 py: 400]];
584 [ink SetPoint: 1 pointindex: 2 pt: [[PTPDFPoint alloc] initWithPx: 180 py: 490]];
585 [ink SetPoint: 1 pointindex: 1 pt: [[PTPDFPoint alloc] initWithPx: 140 py: 440]];
586 [ink SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptsolid b_width: 3 b_hr: 0 b_vr: 0] oldStyleOnly: NO];
587 [ink RefreshAppearance];
588 [page6 AnnotPushBack: ink];
589 }
590
591
592 PTPage *page7 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
593 [ew WriterBeginWithPage: page7 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
594 [ew End]; // save changes to the current page
595 [doc PagePushBack: page7];
596
597 {
598 PTSound *snd = [PTSound Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 100 y1: 500 x2: 120 y2: 520]];
599 [snd SetColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] numcomp: 3];
600 [snd SetSoundIconType: e_ptSpeaker];
601 [snd RefreshAppearance];
602 [page7 AnnotPushBack: snd];
603 }
604 {
605 PTSound *snd = [PTSound Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 200 y1: 500 x2: 220 y2: 520]];
606 [snd SetColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] numcomp: 3];
607 [snd SetSoundIconType: e_ptMic];
608 [snd RefreshAppearance];
609 [page7 AnnotPushBack: snd];
610 }
611
612
613
614
615 PTPage *page8 = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 600 y2: 600]];
616 [ew WriterBeginWithPage: page8 placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL]; // begin writing to the page
617 [ew End]; // save changes to the current page
618 [doc PagePushBack: page8];
619
620 int ipage1;
621 for( ipage1 =0; ipage1 < 2; ++ipage1 ) {
622 double px = 5, py = 520;
623 PTRubberStampIcon istamp;
624 for( istamp = e_ptApproved; istamp <= e_ptDraft; istamp = istamp + 1 ) {
625 PTRubberStamp *st = [PTRubberStamp Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 1 y1: 2 x2: 200 y2: 200] icon:e_ptDraft];
626 [st SetRubberStampIconType: istamp];
627 [st SetContents: [st GetIconName]];
628 [st SetRect: [[PTPDFRect alloc] initWithX1: px y1: py x2: px+100 y2: py+25]];
629 py = py - 100;
630 if( py < 0 ) {
631 py = 520;
632 px = px + 200;
633 }
634 if( ipage == 0 )
635 //[page7 AnnotPushBack: st];
636 ;
637 else {
638 [page8 AnnotPushBack: st];
639 [st RefreshAppearance];
640 }
641 }
642 }
643 PTRubberStamp *st = [PTRubberStamp Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 400 y1: 5 x2: 550 y2: 45] icon: e_ptDraft];
644 [st SetRubberStampIconName: @"UserStamp"];
645 [st SetContents: @"User defined stamp"];
646 [page8 AnnotPushBack: st];
647 [st RefreshAppearance];
648}
649
650int main(int argc, char *argv[])
651{
652 @autoreleasepool {
653
654 int ret = 0;
655 [PTPDFNet Initialize: 0];
656
657 @try
658 {
659 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/numbered.pdf"];
660 [doc InitSecurityHandler];
661
662 // An example of using SDF/Cos API to add any type of annotations.
663 AnnotationLowLevelAPI(doc);
664 [doc SaveToFile: @"../../TestFiles/Output/annotation_test1.pdf" flags: e_ptlinearized];
665 NSLog(@"%@", @"Done. Results saved in annotation_test1.pdf");
666
667 // An example of using the high-level PDFNet API to read existing annotations,
668 // to edit existing annotations, and to create new annotation from scratch.
669 AnnotationHighLevelAPI(doc);
670 [doc SaveToFile: @"../../TestFiles/Output/annotation_test2.pdf" flags: e_ptlinearized];
671 NSLog(@"%@", @"Done. Results saved in annotation_test2.pdf");
672
673 // an example of creating various annotations in a brand new document
674 PTPDFDoc *doc1 = [[PTPDFDoc alloc] init];
675 CreateTestAnnots( doc1 );
676 [doc1 SaveToFile: @"../../TestFiles/Output/new_annot_test_api.pdf" flags: e_ptlinearized];
677 NSLog(@"%@", @"Saved new_annot_test_api.pdf");
678 }
679 @catch(NSException *e)
680 {
681 NSLog(@"%@", e.reason);
682 ret = 1;
683 }
684 [PTPDFNet Terminate: 0];
685 return ret;
686
687 }
688
689}
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
9func AnnotationHighLevelAPI(doc: PTPDFDoc) -> Void {
10 // The following code snippet traverses all annotations in the document
11 print("Traversing all annotations in the document...")
12
13 var page_num: Int = 1
14 let itr: PTPageIterator = doc.getPageIterator(1)
15 while itr.hasNext() {
16 print("Page \(page_num): ")
17 page_num += 1
18 let page: PTPage = itr.current()
19 let num_annots = page.getNumAnnots()
20 for i in 0..<num_annots {
21 let annot: PTAnnot = page.getAnnot(i)
22 if annot.isValid() == false {
23 continue
24 }
25 print("Annot Type: \(annot.getSDFObj().get("Subtype").value().getName()!)")
26 let bbox: PTPDFRect = annot.getRect()
27 print(" Position: \(bbox.getX1()), \(bbox.getY1()), \(bbox.getX2()), \(bbox.getY2())")
28 switch annot.getType() {
29 case e_ptLink:
30 let link: PTLink = PTLink(ann: annot)
31 let action: PTAction = link.getAction()
32 if action.isValid() == false {
33 continue
34 }
35 if action.getType() == e_ptGoTo {
36 let dest: PTDestination = action.getDest()
37 if dest.isValid() == false {
38 print(" Destination is not valid\n")
39 }
40 else {
41 let page_num = dest.getPage().getIndex()
42 print(" Links to: page number \(page_num) in this document\n")
43 }
44 }
45 else if action.getType() == e_ptURI {
46 let uri: String = action.getSDFObj().get("URI").value().getAsPDFText()
47 print(" Links to: \(uri)\n")
48 }
49
50 // ...
51 case e_ptWidget:
52 break
53 case e_ptFileAttachment:
54 break
55 default:
56 break
57 }
58 }
59 itr.next()
60 }
61
62 // Use the high-level API to create new annotations.
63 let first_page: PTPage = doc.getPage(1)
64
65 // Create a hyperlink...
66 let hyperlink = PTLink.create(withAction: doc.getSDFDoc(), pos: PTPDFRect(x1: 85, y1: 570, x2: 503, y2: 524), action: PTAction.createURI(doc.getSDFDoc(), uri: "http://www.pdftron.com"))
67 first_page.annotPushBack(hyperlink)
68
69 // Create an intra-document link...
70 let goto_page_3 = PTAction.createGoto(PTDestination.createFitH(doc.getPage(3), top: 0))
71 let link: PTLink = PTLink.create(withAction: doc.getSDFDoc(), pos: PTPDFRect(x1: 85, y1: 458, x2: 503, y2: 502), action: goto_page_3)
72
73 // Set the annotation border width to 3 points...
74 let border_style = PTBorderStyle(s: e_ptsolid, b_width: 3, b_hr: 0, b_vr: 0)
75 link.setBorderStyle(border_style, oldStyleOnly: false)
76 link.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
77
78 // Add the new annotation to the first page
79 first_page.annotPushBack(link)
80
81 // Create a stamp annotation ...
82 let stamp: PTRubberStamp = PTRubberStamp.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 30, y1: 30, x2: 300, y2: 200), icon: e_ptDraft)
83 stamp.setRubberStampIconName("Draft")
84 first_page.annotPushBack(stamp)
85
86 // Create a file attachment annotation (embed the 'peppers.jpg').
87 let file_attach = PTFileAttachment.createFileAttch(withPath: doc.getSDFDoc(), pos: PTPDFRect(x1: 80, y1: 280, x2: 200, y2: 320), path: Bundle.main.path(forResource: "peppers", ofType: "jpg"), icon_name: e_ptPushPin)
88 first_page.annotPushBack(file_attach)
89
90 let ink: PTInk = PTInk.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 110, y1: 10, x2: 300, y2: 200))
91 let pt3: PTPDFPoint = PTPDFPoint(px: 110, py: 10)
92 //pt3.x = 110; pt3.y = 10;
93 ink.setPoint(0, pointindex: 0, pt: pt3)
94 pt3.setX(150)
95 pt3.setY(50)
96 ink.setPoint(0, pointindex: 1, pt: pt3)
97 pt3.setX(190)
98 pt3.setY(60)
99 ink.setPoint(0, pointindex: 2, pt: pt3)
100 pt3.setX(180)
101 pt3.setY(90)
102 ink.setPoint(1, pointindex: 0, pt: pt3)
103 pt3.setX(190)
104 pt3.setY(95)
105 ink.setPoint(1, pointindex: 1, pt: pt3)
106 pt3.setX(200)
107 pt3.setY(100)
108 ink.setPoint(1, pointindex: 2, pt: pt3)
109 pt3.setX(166)
110 pt3.setY(86)
111 ink.setPoint(2, pointindex: 0, pt: pt3)
112 pt3.setX(196)
113 pt3.setY(96)
114 ink.setPoint(2, pointindex: 1, pt: pt3)
115 pt3.setX(221)
116 pt3.setY(121)
117 ink.setPoint(2, pointindex: 2, pt: pt3)
118 pt3.setX(288)
119 pt3.setY(188)
120 ink.setPoint(2, pointindex: 3, pt: pt3)
121 ink.setColor(PTColorPt(x: 0, y: 1, z: 1, w: 0), numcomp: 3)
122 first_page.annotPushBack(ink)
123
124 //Create a Polygon annotation..
125 // let poly: PTPolygon = PTPolygon.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 60, x2: 300, y2: 480))
126 // let pllp: PTPDFPoint = PTPDFPoint(px: 105, py: 68)
127 // // pllp.x = 105; pllp.y = 68;
128 // poly.setVertex(0, pt: pllp)
129 // pllp.setX(155)
130 // pllp.setY(92)
131 // poly.setVertex(1, pt: pllp)
132 // pllp.setX(189)
133 // pllp.setY(133)
134 // poly.setVertex(2, pt: pllp)
135 // pllp.setX(200)
136 // pllp.setY(140)
137 // poly.setVertex(3, pt: pllp)
138 // pllp.setX(230)
139 // pllp.setY(389)
140 // poly.setVertex(4, pt: pllp)
141 // pllp.setX(300)
142 // pllp.setY(405)
143 // poly.setVertex(5, pt: pllp)
144 // poly.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
145 // poly.setInteriorColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), compNum: 3)
146 // first_page.annotPushBack(poly)
147
148 // ...
149}
150
151func AnnotationLowLevelAPI(doc: PTPDFDoc) -> Void {
152 let page: PTPage = doc.getPageIterator(1).current()
153
154 let annots: PTObj
155 if let optAnnots = page.getAnnots() {
156 annots = optAnnots
157 } else {
158 // If there are no annotations, create a new annotation
159 // array for the page.
160 annots = doc.createIndirectArray()
161 page.getSDFObj().put("Annots", obj: annots)
162 }
163
164 // Create a Text annotation
165 let annot: PTObj = doc.createIndirectDict()
166 annot.putName("Subtype", name: "Text")
167 annot.putBool("Open", value: true)
168 annot.put("Contents", value: "The quick brown fox ate the lazy mouse.")
169 annot.putRect("Rect", x1: 266, y1: 116, x2: 430, y2: 204)
170
171 // Insert the annotation in the page annotation array
172 annots.pushBack(annot)
173
174 // Create a Link annotation
175 let link1: PTObj = doc.createIndirectDict()
176 link1.putName("Subtype", name: "Link")
177 let dest: PTDestination = PTDestination.createFit(doc.getPage(2))
178 link1.put("Dest", obj: dest.getSDFObj())
179 link1.putRect("Rect", x1: 85, y1: 705, x2: 503, y2: 661)
180 annots.pushBack(link1)
181
182 // Create another Link annotation
183 let link2: PTObj = doc.createIndirectDict()
184 link2.putName("Subtype", name: "Link")
185 let dest2: PTDestination = PTDestination.createFit(doc.getPage(3))
186 link2.put("Dest", obj: dest2.getSDFObj())
187 link2.putRect("Rect", x1: 85, y1: 638, x2: 503, y2: 594)
188 annots.pushBack(link2)
189
190 // Note that PDFNet API can be used to modify existing annotations.
191 // In the following example we will modify the second link annotation
192 // (link2) so that it points to the 10th page. We also use a different
193 // destination page fit type.
194
195 // link2 = annots.GetAt(annots.Size()-1);
196 link2.put("Dest", obj: PTDestination.createXYZ(doc.getPage(10), left: 100, top: 792 - 70, zoom: 10).getSDFObj())
197
198 // Create a third link annotation with a hyperlink action (all other
199 // annotation types can be created in a similar way)
200 let link3: PTObj = doc.createIndirectDict()
201 link3.putName("Subtype", name: "Link")
202 link3.putRect("Rect", x1: 85, y1: 570, x2: 503, y2: 524)
203
204 // Create a URI action
205 let action: PTObj = link3.putDict("A")
206 action.putName("S", name: "URI")
207 action.put("URI", value: "http://www.pdftron.com")
208
209 annots.pushBack(link3)
210}
211
212func CreateTestAnnots(doc: PTPDFDoc) -> Void {
213 let ew: PTElementWriter = PTElementWriter()
214 let eb: PTElementBuilder = PTElementBuilder()
215 var element: PTElement = PTElement()
216
217 let first_page: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
218 doc.pagePushBack(first_page)
219 ew.writerBegin(with: first_page, placement: e_ptoverlay, page_coord_sys: false, compress: false, resources: nil) // begin writing to this page
220 ew.end() // save changes to the current page
221
222 //
223 // Test of a free text annotation.
224 //
225 do {
226 let txtannot: PTFreeText = PTFreeText.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 10, y1: 400, x2: 160, y2: 570))
227 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
228 "\n\nAha!\n\nAnd there was much rejoicing!")
229 //std::vector<double> dash( 2, 2.0 );
230 txtannot.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 1, b_hr: 10, b_vr: 20), oldStyleOnly: true)
231 txtannot.setQuaddingFormat(0)
232 first_page.annotPushBack(txtannot)
233 txtannot.refreshAppearance()
234 }
235 do {
236 let txtannot: PTFreeText = PTFreeText.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 100, x2: 350, y2: 500))
237 txtannot.setContentRect(PTPDFRect(x1: 200, y1: 200, x2: 350, y2: 500))
238 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
239 "\n\nAha!\n\nAnd there was much rejoicing!")
240 txtannot.setCalloutLinePoints(withKneePoint: PTPDFPoint(px: 200, py: 300), p2: PTPDFPoint(px: 150, py: 290), p3: PTPDFPoint(px: 110, py: 110))
241 //std::vector<double> dash( 2, 2.0 );
242 txtannot.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 1, b_hr: 10, b_vr: 20), oldStyleOnly: true)
243 txtannot.setEndingStyle(e_ptClosedArrow)
244 txtannot.setColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), numcomp: 3)
245 txtannot.setQuaddingFormat(1)
246 first_page.annotPushBack(txtannot)
247 txtannot.refreshAppearance()
248 }
249 do {
250 let txtannot: PTFreeText = PTFreeText.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 400, y1: 10, x2: 550, y2: 400))
251 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
252 "\n\nAha!\n\nAnd there was much rejoicing!")
253 txtannot.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 1, b_hr: 10, b_vr: 20), oldStyleOnly: true)
254 txtannot.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
255 txtannot.setOpacity(0.2)
256 txtannot.setQuaddingFormat(2)
257 first_page.annotPushBack(txtannot)
258 txtannot.refreshAppearance()
259 }
260
261 let page: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
262 doc.pagePushBack(page)
263 ew.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to this page
264 eb.reset(PTGState()) // Reset the GState to default
265 ew.end() // save changes to the current page
266
267 do {
268 //Create a Line annotation...
269 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 250, y1: 250, x2: 400, y2: 400))
270 line.setStart(PTPDFPoint(px: 350, py: 270))
271 line.setEnd(PTPDFPoint(px: 260, py: 370))
272 line.setStart(e_ptl_Square)
273 line.setEnd(e_ptl_Circle)
274 line.setColor(PTColorPt(x: 0.3, y: 0.5, z: 0, w: 0), numcomp: 3)
275 line.setContents("Dashed Captioned")
276 line.setShowCaption(true)
277 line.setCaptionPosition(e_ptTop)
278 let dash = NSMutableArray(capacity: 2)
279 dash.add(2.0)
280 dash.add(2.0)
281 line.setBorderStyle(PTBorderStyle(s: e_ptdashed, b_width: 2, b_hr: 0, b_vr: 0, b_dash: dash), oldStyleOnly: false)
282 line.refreshAppearance()
283 page.annotPushBack(line)
284 }
285 do {
286 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 347, y1: 377, x2: 600, y2: 600))
287 line.setStart(PTPDFPoint(px: 385, py: 410))
288 line.setEnd(PTPDFPoint(px: 540, py: 555))
289 line.setStart(e_ptl_Circle)
290 line.setEnd(e_ptOpenArrow)
291 line.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
292 line.setContents("Inline Caption")
293 line.setShowCaption(true)
294 line.setCaptionPosition(e_ptInline)
295 line.setLeaderLineExtensionLength(-4)
296 line.setLeaderLineLength(-12)
297 line.setLeaderLineOffset(2)
298 line.refreshAppearance()
299 page.annotPushBack(line)
300 }
301
302 do {
303 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 10, y1: 400, x2: 200, y2: 600))
304 line.setStart(PTPDFPoint(px: 25, py: 426))
305 line.setEnd(PTPDFPoint(px: 180, py: 555))
306 line.setStart(e_ptl_Circle)
307 line.setEnd(e_ptl_Square)
308 line.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
309 line.setInteriorColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), compNum: 3)
310 line.setContents("Offset Caption")
311 line.setShowCaption(true)
312 line.setCaptionPosition(e_ptTop)
313 line.setTextHOffset(-60)
314 line.setTextVOffset(10)
315 line.refreshAppearance()
316 page.annotPushBack(line)
317 }
318 do {
319 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 200, y1: 10, x2: 400, y2: 70))
320 line.setStart(PTPDFPoint(px: 220, py: 25))
321 line.setEnd(PTPDFPoint(px: 370, py: 60))
322 line.setStart(e_ptButt)
323 line.setEnd(e_ptOpenArrow)
324 line.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
325 line.setContents("Regular Caption")
326 line.setShowCaption(true)
327 line.setCaptionPosition(e_ptTop)
328 line.refreshAppearance()
329 page.annotPushBack(line)
330 }
331 do {
332 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 200, y1: 70, x2: 400, y2: 130))
333 line.setStart(PTPDFPoint(px: 220, py: 111))
334 line.setEnd(PTPDFPoint(px: 370, py: 78))
335 line.setStart(e_ptl_Circle)
336 line.setEnd(e_ptDiamond)
337 line.setContents("Circle to Diamond")
338 line.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
339 line.setInteriorColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), compNum: 3)
340 line.setShowCaption(true)
341 line.setCaptionPosition(e_ptTop)
342 line.refreshAppearance()
343 page.annotPushBack(line)
344 }
345 do {
346 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 10, y1: 100, x2: 160, y2: 200))
347 line.setStart(PTPDFPoint(px: 15, py: 110))
348 line.setEnd(PTPDFPoint(px: 150, py: 190))
349 line.setStart(e_ptSlash)
350 line.setEnd(e_ptClosedArrow)
351 line.setContents("Slash to CArrow")
352 line.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
353 line.setInteriorColor(PTColorPt(x: 0, y: 1, z: 1, w: 0), compNum: 3)
354 line.setShowCaption(true)
355 line.setCaptionPosition(e_ptTop)
356 line.refreshAppearance()
357 page.annotPushBack(line)
358 }
359 do {
360 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 270, y1: 270, x2: 570, y2: 433))
361 line.setStart(PTPDFPoint(px: 300, py: 400))
362 line.setEnd(PTPDFPoint(px: 550, py: 300))
363 line.setStart(e_ptRClosedArrow)
364 line.setEnd(e_ptROpenArrow)
365 line.setContents("ROpen & RClosed arrows")
366 line.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
367 line.setInteriorColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), compNum: 3)
368 line.setShowCaption(true)
369 line.setCaptionPosition(e_ptTop)
370 line.refreshAppearance()
371 page.annotPushBack(line)
372 }
373 do {
374 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 195, y1: 395, x2: 205, y2: 505))
375 line.setStart(PTPDFPoint(px: 200, py: 400))
376 line.setEnd(PTPDFPoint(px: 200, py: 500))
377 line.refreshAppearance()
378 page.annotPushBack(line)
379 }
380 do {
381 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 55, y1: 299, x2: 150, y2: 301))
382 line.setStart(PTPDFPoint(px: 55, py: 300))
383 line.setEnd(PTPDFPoint(px: 155, py: 300))
384 line.setStart(e_ptl_Circle)
385 line.setEnd(e_ptl_Circle)
386 line.setContents("Caption that's longer than its line")
387 line.setColor(PTColorPt(x: 1, y: 0, z: 1, w: 0), numcomp: 3)
388 line.setInteriorColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), compNum: 3)
389 line.setShowCaption(true)
390 line.setCaptionPosition(e_ptTop)
391 line.refreshAppearance()
392 page.annotPushBack(line)
393 }
394 do {
395 let line: PTLineAnnot = PTLineAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 300, y1: 200, x2: 290, y2: 234))
396 line.setStart(PTPDFPoint(px: 310, py: 210))
397 line.setEnd(PTPDFPoint(px: 380, py: 220))
398 line.setColor(PTColorPt(x: 0, y: 0, z: 0, w: 0), numcomp: 3)
399 line.refreshAppearance()
400 page.annotPushBack(line)
401 }
402
403 let page3: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
404 ew.writerBegin(with: page3, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
405 ew.end() // save changes to the current page
406 doc.pagePushBack(page3)
407 do {
408 let circle: PTCircle = PTCircle.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 300, y1: 300, x2: 390, y2: 350))
409 circle.setColor(PTColorPt(x: 0, y: 0, z: 0, w: 0), numcomp: 3)
410 circle.refreshAppearance()
411 page3.annotPushBack(circle)
412 }
413 do {
414 let circle: PTCircle = PTCircle.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 100, x2: 200, y2: 200))
415 circle.setColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), numcomp: 3)
416 circle.setInteriorColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), compNum: 3)
417 let dash = NSMutableArray(capacity: 2)
418 dash.add(2.0)
419 dash.add(4.0)
420 circle.setBorderStyle(PTBorderStyle(s: e_ptdashed, b_width: 3, b_hr: 0, b_vr: 0, b_dash: dash), oldStyleOnly: false)
421 circle.setPadding(2)
422 circle.refreshAppearance()
423 page3.annotPushBack(circle)
424 }
425 do {
426 let sq: PTSquare = PTSquare.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 10, y1: 200, x2: 80, y2: 300))
427 sq.setColor(PTColorPt(x: 0, y: 0, z: 0, w: 0), numcomp: 3)
428 sq.refreshAppearance()
429 page3.annotPushBack(sq)
430 }
431 do {
432 let sq: PTSquare = PTSquare.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 500, y1: 200, x2: 580, y2: 300))
433 sq.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
434 sq.setInteriorColor(PTColorPt(x: 0, y: 1, z: 1, w: 0), compNum: 3)
435 let dash = NSMutableArray(capacity: 2)
436 dash.add(4.0)
437 dash.add(2.0)
438 sq.setBorderStyle(PTBorderStyle(s: e_ptdashed, b_width: 6, b_hr: 0, b_vr: 0, b_dash: dash), oldStyleOnly: false)
439 sq.setPadding(4)
440 sq.refreshAppearance()
441 page3.annotPushBack(sq)
442 }
443 do {
444 let poly: PTPolygon = PTPolygon.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 5, y1: 500, x2: 125, y2: 590))
445 poly.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
446 poly.setInteriorColor(PTColorPt(x: 1, y: 1, z: 0, w: 0), compNum: 3)
447 poly.setVertex(0, pt: PTPDFPoint(px: 12, py: 510))
448 poly.setVertex(1, pt: PTPDFPoint(px: 100, py: 510))
449 poly.setVertex(2, pt: PTPDFPoint(px: 100, py: 555))
450 poly.setVertex(3, pt: PTPDFPoint(px: 35, py: 544))
451 poly.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 4, b_hr: 0, b_vr: 0), oldStyleOnly: false)
452 poly.setPadding(4)
453 poly.refreshAppearance()
454 page3.annotPushBack(poly)
455 }
456 do {
457 let poly: PTPolygon = PTPolygon.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 400, y1: 10, x2: 500, y2: 90))
458 poly.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
459 poly.setInteriorColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), compNum: 3)
460 poly.setVertex(0, pt: PTPDFPoint(px: 405, py: 20))
461 poly.setVertex(1, pt: PTPDFPoint(px: 440, py: 40))
462 poly.setVertex(2, pt: PTPDFPoint(px: 410, py: 60))
463 poly.setVertex(3, pt: PTPDFPoint(px: 470, py: 80))
464 poly.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 2, b_hr: 0, b_vr: 0), oldStyleOnly: false)
465 poly.setPadding(4)
466 poly.setStart(e_ptRClosedArrow)
467 poly.setEnd(e_ptClosedArrow)
468 poly.refreshAppearance()
469 page3.annotPushBack(poly)
470 }
471 do {
472 let lk: PTLink = PTLink.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 5, y1: 5, x2: 55, y2: 24))
473 //lk.setColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), numcomp: 3)
474 lk.refreshAppearance()
475 page3.annotPushBack(lk)
476 }
477
478 let page4: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
479 ew.writerBegin(with: page4, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
480 ew.end() // save changes to the current page
481 doc.pagePushBack(page4)
482
483 do {
484 ew.writerBegin(with: page4, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)
485 let font = PTFont.create(doc.getSDFDoc(), type: e_pthelvetica, embed: false)
486 element = eb.createTextBegin(with: font, font_sz: 16)
487 element.setPathFill(true)
488 ew.write(element)
489 element = eb.createTextRun(withFont: "Some random text on the page", font: font, font_sz: 16)
490 element.setTextMatrix(1, b: 0, c: 0, d: 1, h: 100, v: 500)
491 ew.write(element)
492 ew.write(eb.createTextEnd())
493 ew.end()
494 }
495 do {
496 let hl: PTHighlightAnnot = PTHighlightAnnot.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 490, x2: 150, y2: 515))
497 hl.setColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), numcomp: 3)
498 hl.refreshAppearance()
499 page4.annotPushBack(hl)
500 }
501 do {
502 let sq: PTSquiggly = PTSquiggly.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 450, x2: 250, y2: 600))
503 //sq.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
504 let p1 = PTPDFPoint(px: 122, py: 455)
505 let p2 = PTPDFPoint(px: 240, py: 545)
506 let p3 = PTPDFPoint(px: 230, py: 595)
507 let p4 = PTPDFPoint(px: 101, py: 500)
508 sq.setQuadPoint(0, qp: PTQuadPoint(p11: p1, p22: p2, p33: p3, p44: p4))
509 sq.refreshAppearance()
510 page4.annotPushBack(sq)
511 }
512 do {
513 let cr: PTCaret = PTCaret.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 40, x2: 129, y2: 69))
514 cr.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
515 cr.setSymbol("P")
516 cr.refreshAppearance()
517 page4.annotPushBack(cr)
518 }
519
520 let page5: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
521 ew.writerBegin(with: page5, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
522 ew.end() // save changes to the current page
523 doc.pagePushBack(page5)
524 let fs = PTFileSpec.create(doc.getSDFDoc(), path: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("butterfly.png").path, embed: false)
525 let page6: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
526 ew.writerBegin(with: page6, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
527 ew.end() // save changes to the current page
528 doc.pagePushBack(page6)
529
530 for ipage in 0..<2 {
531 for iann in 0..<100 {
532 if !(iann > e_ptTag.rawValue) {
533 let fa: PTFileAttachment = PTFileAttachment.createFileAttch(withFileSpec: doc.getSDFDoc(), pos: PTPDFRect(x1: Double(50 + 50 * iann), y1: 100, x2: Double(70 + 50 * iann), y2: 120), fs: fs, icon_name: PTFileIcon.init(UInt32(iann)))
534 if ipage != 0 {
535 fa.setColor(PTColorPt(x: 1, y: 1, z: 0, w: 0), numcomp: 3)
536 }
537 fa.refreshAppearance()
538 if ipage == 0 {
539 page5.annotPushBack(fa)
540 }
541 else {
542 page6.annotPushBack(fa)
543 }
544 }
545 if iann > e_ptNote.rawValue {
546 break
547 }
548 let txt: PTText = PTText.create(doc.getSDFDoc(), pos: PTPDFRect(x1: Double(10 + iann * 50), y1: 200, x2: Double(30 + iann * 50), y2: 220))
549 txt.setTextIconType(PTTextIcon.init(UInt32(iann)))
550 txt.setContents(txt.getIconName())
551 if ipage != 0 {
552 txt.setColor(PTColorPt(x: 1, y: 1, z: 0, w: 0), numcomp: 3)
553 }
554 txt.refreshAppearance()
555 if ipage == 0 {
556 page5.annotPushBack(txt)
557 }
558 else {
559 page6.annotPushBack(txt)
560 }
561 }
562 }
563 do {
564 let txt: PTText = PTText.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 10, y1: 20, x2: 30, y2: 40))
565 txt.setTextIconName("UserIcon")
566 txt.setContents("User defined icon, unrecognized by appearance generator")
567 txt.setColor(PTColorPt(x: 0, y: 1, z: 0, w: 0), numcomp: 3)
568 txt.refreshAppearance()
569 page6.annotPushBack(txt)
570 }
571 do {
572 let ink: PTInk = PTInk.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 400, x2: 200, y2: 550))
573 ink.setColor(PTColorPt(x: 0, y: 0, z: 1, w: 0), numcomp: 3)
574 ink.setPoint(1, pointindex: 3, pt: PTPDFPoint(px: 220, py: 505))
575 ink.setPoint(1, pointindex: 0, pt: PTPDFPoint(px: 100, py: 490))
576 ink.setPoint(0, pointindex: 1, pt: PTPDFPoint(px: 120, py: 410))
577 ink.setPoint(0, pointindex: 0, pt: PTPDFPoint(px: 100, py: 400))
578 ink.setPoint(1, pointindex: 2, pt: PTPDFPoint(px: 180, py: 490))
579 ink.setPoint(1, pointindex: 1, pt: PTPDFPoint(px: 140, py: 440))
580 ink.setBorderStyle(PTBorderStyle(s: e_ptsolid, b_width: 3, b_hr: 0, b_vr: 0), oldStyleOnly: false)
581 ink.refreshAppearance()
582 page6.annotPushBack(ink)
583 }
584
585 let page7: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
586 ew.writerBegin(with: page7, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
587 ew.end() // save changes to the current page
588 doc.pagePushBack(page7)
589
590 do {
591 let snd: PTSound = PTSound.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 100, y1: 500, x2: 120, y2: 520))
592 snd.setColor(PTColorPt(x: 1, y: 1, z: 0, w: 0), numcomp: 3)
593 snd.setSoundIconType(e_ptSpeaker)
594 snd.refreshAppearance()
595 page7.annotPushBack(snd)
596 }
597 do {
598 let snd: PTSound = PTSound.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 200, y1: 500, x2: 220, y2: 520))
599 snd.setColor(PTColorPt(x: 1, y: 1, z: 0, w: 0), numcomp: 3)
600 snd.setSoundIconType(e_ptMic)
601 snd.refreshAppearance()
602 page7.annotPushBack(snd)
603 }
604
605 let page8: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 600, y2: 600))
606 ew.writerBegin(with: page8, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil) // begin writing to the page
607 ew.end() // save changes to the current page
608 doc.pagePushBack(page8)
609
610 for ipage1 in 0..<2 {
611 var px: Double = 5
612 var py: Double = 520
613 var istamp = e_ptApproved.rawValue
614 while istamp <= e_ptDraft.rawValue {
615 let st: PTRubberStamp = PTRubberStamp.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 1, y1: 2, x2: 200, y2: 200), icon: e_ptDraft)
616 st.setRubberStampIconType(PTRubberStampIcon.init(istamp))
617 st.setContents(st.getIconName())
618 st.setRect(PTPDFRect(x1: px, y1: py, x2: px + 100, y2: py + 25))
619 py = py - 100
620 if py < 0 {
621 py = 520
622 px = px + 200
623 }
624 if ipage1 == 0 {
625 //[page7 AnnotPushBack: st];
626 }
627 else {
628 page8.annotPushBack(st)
629 st.refreshAppearance()
630 }
631 istamp = istamp + 1
632 }
633 }
634 let st: PTRubberStamp = PTRubberStamp.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 400, y1: 5, x2: 550, y2: 45), icon: e_ptDraft)
635 st.setRubberStampIconName("UserStamp")
636 st.setContents("User defined stamp")
637 page8.annotPushBack(st)
638 st.refreshAppearance()
639}
640
641func runAnnotationTest() -> Int {
642 return autoreleasepool {
643 var ret: Int = 0
644
645
646 do {
647 try PTPDFNet.catchException {
648 let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "numbered", ofType: "pdf"))
649 doc.initSecurityHandler()
650
651 // An example of using SDF/Cos API to add any type of annotations.
652 AnnotationLowLevelAPI(doc: doc)
653 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("annotation_test1.pdf").path, flags: e_ptlinearized.rawValue)
654
655 // An example of using the high-level PDFNet API to read existing annotations,
656 // to edit existing annotations, and to create new annotation from scratch.
657 AnnotationHighLevelAPI(doc: doc)
658 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("annotation_test2.pdf").path, flags: e_ptlinearized.rawValue)
659
660 // an example of creating various annotations in a brand new document
661 let doc1: PTPDFDoc = PTPDFDoc()
662 CreateTestAnnots(doc: doc1)
663 doc1.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("new_annot_test_api.pdf").path, flags: e_ptlinearized.rawValue)
664 if let outputFile = doc1.getFileName(){
665 print("Saved: \(outputFile)")
666 }
667 }
668 } catch let e as NSError {
669 print("Uncaught exception: \(e)")
670 ret = 1
671 }
672
673 return ret
674 }
675}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales