Sample code to use Apryse Server 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 Server SDK and PDF Annotation Library.
1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using pdftron;
6using pdftron.Common;
7using pdftron.Filters;
8using pdftron.SDF;
9using pdftron.PDF;
10using pdftron.PDF.Annots;
11
12namespace AnnotationTestCS
13{
14 /// <summary>
15 /// Summary description for Class1.
16 /// </summary>
17 class Class1
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static Class1() {}
21
22 private static void AnnotationHighLevelAPI(PDFDoc doc)
23 {
24 // The following code snippet traverses all annotations in the document
25 System.Console.WriteLine("Traversing all annotations in the document...");
26
27 string uri;
28 int page_num = 1;
29 for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
30 {
31 System.Console.WriteLine("Page " + page_num++ + ": ");
32
33 Page page = itr.Current();
34 int num_annots = page.GetNumAnnots();
35 for (int i = 0; i < num_annots; ++i)
36 {
37 Annot annot = page.GetAnnot(i);
38 if (!annot.IsValid()) continue;
39 System.Console.WriteLine("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName());
40
41 Rect bbox = annot.GetRect();
42 System.Console.WriteLine(" Position: " + bbox.x1
43 + ", " + bbox.y1
44 + ", " + bbox.x2
45 + ", " + bbox.y2);
46
47 switch (annot.GetType())
48 {
49 case Annot.Type.e_Link:
50 {
51 Link lnk = new Link(annot);
52 Action action = lnk.GetAction();
53 if (!action.IsValid()) continue;
54 if (action.GetType() == Action.Type.e_GoTo)
55 {
56 Destination dest = action.GetDest();
57 if (!dest.IsValid())
58 {
59 System.Console.WriteLine(" Destination is not valid");
60 }
61 else
62 {
63 int pg_num = dest.GetPage().GetIndex();
64 System.Console.WriteLine(" Links to: page number " + pg_num + " in this document");
65 }
66 }
67 else if (action.GetType() == Action.Type.e_URI)
68 {
69 uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText();
70 System.Console.WriteLine(" Links to: " + uri);
71 }
72 // ...
73 }
74 break;
75 case Annot.Type.e_Widget:
76 break;
77 case Annot.Type.e_FileAttachment:
78 break;
79 // ...
80 default:
81 break;
82 }
83 }
84 }
85
86 // Use the high-level API to create new annotations.
87 Page first_page = doc.GetPage(1);
88
89 // Create a hyperlink...
90 Link hyperlink = Link.Create(doc, new Rect(85, 570, 503, 524), Action.CreateURI(doc, "http://www.pdftron.com"));
91 first_page.AnnotPushBack(hyperlink);
92
93 // Create an intra-document link...
94 Action goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0));
95 Link link = Link.Create(doc, new Rect(85, 458, 503, 502), goto_page_3);
96 link.SetColor(new ColorPt(0, 0, 1));
97
98 // Add the new annotation to the first page
99 first_page.AnnotPushBack(link);
100
101 // Create a stamp annotation ...
102 RubberStamp stamp = RubberStamp.Create(doc, new Rect(30, 30, 300, 200));
103 stamp.SetIcon("Draft");
104 first_page.AnnotPushBack(stamp);
105
106 // Create a file attachment annotation (embed the 'peppers.jpg').
107 FileAttachment file_attach = FileAttachment.Create(doc, new Rect(80, 280, 108, 320), (input_path + "peppers.jpg"));
108 first_page.AnnotPushBack(file_attach);
109
110
111 Ink ink = Ink.Create(doc, new Rect(110, 10, 300, 200));
112 Point pt3 = new Point(110, 10);
113 //pt3.x = 110; pt3.y = 10;
114 ink.SetPoint(0, 0, pt3);
115 pt3.x = 150; pt3.y = 50;
116 ink.SetPoint(0, 1, pt3);
117 pt3.x = 190; pt3.y = 60;
118 ink.SetPoint(0, 2, pt3);
119 pt3.x = 180; pt3.y = 90;
120 ink.SetPoint(1, 0, pt3);
121 pt3.x = 190; pt3.y = 95;
122 ink.SetPoint(1, 1, pt3);
123 pt3.x = 200; pt3.y = 100;
124 ink.SetPoint(1, 2, pt3);
125 pt3.x = 166; pt3.y = 86;
126 ink.SetPoint(2, 0, pt3);
127 pt3.x = 196; pt3.y = 96;
128 ink.SetPoint(2, 1, pt3);
129 pt3.x = 221; pt3.y = 121;
130 ink.SetPoint(2, 2, pt3);
131 pt3.x = 288; pt3.y = 188;
132 ink.SetPoint(2, 3, pt3);
133 ink.SetColor(new ColorPt(0, 1, 1), 3);
134 first_page.AnnotPushBack(ink);
135 }
136
137 static void AnnotationLowLevelAPI(PDFDoc doc)
138 {
139 Page page = doc.GetPage(1);
140
141 Obj annots = page.GetAnnots();
142 if (annots == null)
143 {
144 // If there are no annotations, create a new annotation
145 // array for the page.
146 annots = doc.CreateIndirectArray();
147 page.GetSDFObj().Put("Annots", annots);
148 }
149
150 // Create the Text annotation
151 Obj text_annot = doc.CreateIndirectDict();
152 text_annot.PutName("Subtype", "Text");
153 text_annot.PutBool("Open", true);
154 text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
155 text_annot.PutRect("Rect", 266, 116, 430, 204);
156
157 // Insert the annotation in the page annotation array
158 annots.PushBack(text_annot);
159
160 // Create a Link annotation
161 Obj link1 = doc.CreateIndirectDict();
162 link1.PutName("Subtype", "Link");
163 Destination dest = Destination.CreateFit(doc.GetPage(2));
164 link1.Put("Dest", dest.GetSDFObj());
165 link1.PutRect("Rect", 85, 705, 503, 661);
166 annots.PushBack(link1);
167
168 // Create another Link annotation
169 Obj link2 = doc.CreateIndirectDict();
170 link2.PutName("Subtype", "Link");
171 Destination dest2 = Destination.CreateFit(doc.GetPage(3));
172 link2.Put("Dest", dest2.GetSDFObj());
173 link2.PutRect("Rect", 85, 638, 503, 594);
174 annots.PushBack(link2);
175
176 // Note that PDFNet APi can be used to modify existing annotations.
177 // In the following example we will modify the second link annotation
178 // (link2) so that it points to the 10th page. We also use a different
179 // destination page fit type.
180
181 link2.Put("Dest",
182 Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj());
183
184 // Create a third link annotation with a hyperlink action (all other
185 // annotation types can be created in a similar way)
186 Obj link3 = doc.CreateIndirectDict();
187 link3.PutName("Subtype", "Link");
188 link3.PutRect("Rect", 85, 570, 503, 524);
189
190 // Create a URI action
191 Obj action = link3.PutDict("A");
192 action.PutName("S", "URI");
193 action.PutString("URI", "http://www.pdftron.com");
194 annots.PushBack(link3);
195 }
196
197 private static void CreateTestAnnots(PDFDoc doc) {
198
199 ElementWriter ew = new ElementWriter();
200 ElementBuilder eb = new ElementBuilder();
201 Element element;
202
203 Page first_page= doc.PageCreate(new Rect(0, 0, 600, 600));
204 doc.PagePushBack(first_page);
205 ew.Begin(first_page, ElementWriter.WriteMode.e_overlay, false ); // begin writing to this page
206 ew.End(); // save changes to the current page
207
208 //
209 // Test of a free text annotation.
210 //
211 {
212 FreeText txtannot = FreeText.Create( doc, new Rect(10, 400, 160, 570) );
213 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
214 "\n\nAha!\n\nAnd there was much rejoicing!" );
215 txtannot.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 1, 10, 20 ) );
216 txtannot.SetQuaddingFormat(0);
217 first_page.AnnotPushBack(txtannot);
218 txtannot.RefreshAppearance();
219 }
220 {
221 FreeText txtannot = FreeText.Create( doc, new Rect(100, 100, 350, 500) );
222 txtannot.SetContentRect( new Rect( 200, 200, 350, 500 ) );
223 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
224 "\n\nAha!\n\nAnd there was much rejoicing!" );
225 txtannot.SetCalloutLinePoints( new Point(200,300), new Point(150,290), new Point(110,110) );
226 txtannot.SetBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 1, 10, 20 ) );
227 txtannot.SetEndingStyle(Line.EndingStyle.e_ClosedArrow );
228 txtannot.SetColor( new ColorPt( 0, 1, 0 ) );
229 txtannot.SetQuaddingFormat(1);
230 first_page.AnnotPushBack(txtannot);
231 txtannot.RefreshAppearance();
232 }
233 {
234 FreeText txtannot = FreeText.Create( doc, new Rect(400, 10, 550, 400) );
235 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
236 "\n\nAha!\n\nAnd there was much rejoicing!" );
237 txtannot.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 1, 10, 20 ) );
238 txtannot.SetColor( new ColorPt( 0, 0, 1 ) );
239 txtannot.SetOpacity( 0.2 );
240 txtannot.SetQuaddingFormat(2);
241 first_page.AnnotPushBack(txtannot);
242 txtannot.RefreshAppearance();
243 }
244
245 Page page= doc.PageCreate(new Rect(0, 0, 600, 600));
246 doc.PagePushBack(page);
247 ew.Begin(page, ElementWriter.WriteMode.e_overlay, false ); // begin writing to this page
248 eb.Reset(); // Reset the GState to default
249 ew.End(); // save changes to the current page
250
251 {
252 //Create a Line annotation...
253 Line line = Line.Create(doc, new Rect(250, 250, 400, 400));
254 line.SetStartPoint( new Point(350, 270 ) );
255 line.SetEndPoint( new Point(260,370) );
256 line.SetStartStyle(Line.EndingStyle.e_Square);
257 line.SetEndStyle(Line.EndingStyle.e_Circle);
258 line.SetColor(new ColorPt(.3, .5, 0), 3);
259 line.SetContents( "Dashed Captioned" );
260 line.SetShowCaption(true);
261 line.SetCaptionPosition(Line.CapPos.e_Top );
262 double[] dash = new double[2];
263 dash[0] = 2;
264 dash[1] = 2.0;
265 line.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 2, 0, 0, dash ) );
266 line.RefreshAppearance();
267 page.AnnotPushBack(line);
268 }
269 {
270 Line line=Line.Create(doc, new Rect(347, 377, 600, 600));
271 line.SetStartPoint( new Point(385, 410 ) );
272 line.SetEndPoint(new Point(540,555) );
273 line.SetStartStyle(Line.EndingStyle.e_Circle);
274 line.SetEndStyle(Line.EndingStyle.e_OpenArrow);
275 line.SetColor(new ColorPt(1, 0, 0), 3);
276 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
277 line.SetContents( "Inline Caption" );
278 line.SetShowCaption(true);
279 line.SetCaptionPosition( Line.CapPos.e_Inline);
280 line.SetLeaderLineExtensionLength( 4 );
281 line.SetLeaderLineLength( -12 );
282 line.SetLeaderLineOffset( 2 );
283 line.RefreshAppearance();
284 page.AnnotPushBack(line);
285 }
286 {
287 Line line=Line.Create(doc, new Rect(10, 400, 200, 600));
288 line.SetStartPoint(new Point(25, 426 ) );
289 line.SetEndPoint(new Point(180,555) );
290 line.SetStartStyle(Line.EndingStyle.e_Circle);
291 line.SetEndStyle(Line.EndingStyle.e_Square);
292 line.SetColor(new ColorPt(0, 0, 1), 3);
293 line.SetInteriorColor(new ColorPt(1, 0, 0), 3);
294 line.SetContents("Offset Caption");
295 line.SetShowCaption(true);
296 line.SetCaptionPosition( Line.CapPos.e_Top );
297 line.SetTextHOffset( -60 );
298 line.SetTextVOffset( 10 );
299 line.RefreshAppearance();
300 page.AnnotPushBack(line);
301 }
302 {
303 Line line=Line.Create(doc, new Rect(200, 10, 400, 70));
304 line.SetStartPoint(new Point(220, 25 ) );
305 line.SetEndPoint(new Point(370,60) );
306 line.SetStartStyle(Line.EndingStyle.e_Butt);
307 line.SetEndStyle(Line.EndingStyle.e_OpenArrow);
308 line.SetColor(new ColorPt(0, 0, 1), 3);
309 line.SetContents("Regular Caption");
310 line.SetShowCaption(true);
311 line.SetCaptionPosition( Line.CapPos.e_Top );
312 line.RefreshAppearance();
313 page.AnnotPushBack(line);
314 }
315 {
316 Line line=Line.Create(doc, new Rect(200, 70, 400, 130));
317 line.SetStartPoint(new Point(220, 111 ) );
318 line.SetEndPoint(new Point(370,78) );
319 line.SetStartStyle(Line.EndingStyle.e_Circle);
320 line.SetEndStyle(Line.EndingStyle.e_Diamond);
321 line.SetContents("Circle to Diamond");
322 line.SetColor(new ColorPt(0, 0, 1), 3);
323 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
324 line.SetShowCaption(true);
325 line.SetCaptionPosition( Line.CapPos.e_Top );
326 line.RefreshAppearance();
327 page.AnnotPushBack(line);
328 }
329 {
330 Line line=Line.Create(doc, new Rect(10, 100, 160, 200));
331 line.SetStartPoint(new Point(15, 110 ) );
332 line.SetEndPoint(new Point(150, 190) );
333 line.SetStartStyle(Line.EndingStyle.e_Slash);
334 line.SetEndStyle(Line.EndingStyle.e_ClosedArrow);
335 line.SetContents("Slash to CArrow");
336 line.SetColor(new ColorPt(1, 0, 0), 3);
337 line.SetInteriorColor(new ColorPt(0, 1, 1), 3);
338 line.SetShowCaption(true);
339 line.SetCaptionPosition( Line.CapPos.e_Top );
340 line.RefreshAppearance();
341 page.AnnotPushBack(line);
342 }
343 {
344 Line line=Line.Create(doc, new Rect( 270, 270, 570, 433 ));
345 line.SetStartPoint(new Point(300, 400 ) );
346 line.SetEndPoint(new Point(550, 300) );
347 line.SetStartStyle(Line.EndingStyle.e_RClosedArrow);
348 line.SetEndStyle(Line.EndingStyle.e_ROpenArrow);
349 line.SetContents("ROpen & RClosed arrows");
350 line.SetColor(new ColorPt(0, 0, 1), 3);
351 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
352 line.SetShowCaption(true);
353 line.SetCaptionPosition( Line.CapPos.e_Top );
354 line.RefreshAppearance();
355 page.AnnotPushBack(line);
356 }
357 {
358 Line line=Line.Create(doc, new Rect( 195, 395, 205, 505 ));
359 line.SetStartPoint(new Point(200, 400 ) );
360 line.SetEndPoint(new Point(200, 500) );
361 line.RefreshAppearance();
362 page.AnnotPushBack(line);
363 }
364 {
365 Line line=Line.Create(doc, new Rect( 55, 299, 150, 301 ));
366 line.SetStartPoint(new Point(55, 300 ) );
367 line.SetEndPoint(new Point(155, 300) );
368 line.SetStartStyle(Line.EndingStyle.e_Circle);
369 line.SetEndStyle(Line.EndingStyle.e_Circle);
370 line.SetContents("Caption that's longer than its line.");
371 line.SetColor(new ColorPt(1, 0, 1), 3);
372 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
373 line.SetShowCaption(true);
374 line.SetCaptionPosition( Line.CapPos.e_Top );
375 line.RefreshAppearance();
376 page.AnnotPushBack(line);
377 }
378 {
379 Line line=Line.Create(doc, new Rect( 300, 200, 390, 234 ));
380 line.SetStartPoint(new Point(310, 210 ) );
381 line.SetEndPoint(new Point(380, 220) );
382 line.SetColor(new ColorPt(0, 0, 0), 3);
383 line.RefreshAppearance();
384 page.AnnotPushBack(line);
385 }
386
387 Page page3 = doc.PageCreate(new Rect(0, 0, 600, 600));
388 ew.Begin(page3); // begin writing to the page
389 ew.End(); // save changes to the current page
390 doc.PagePushBack(page3);
391 {
392 Circle circle = Circle.Create(doc, new Rect( 300, 300, 390, 350 ));
393 circle.SetColor(new ColorPt(0, 0, 0), 3);
394 circle.RefreshAppearance();
395 page3.AnnotPushBack(circle);
396 }
397 {
398 Circle circle= Circle.Create(doc, new Rect( 100, 100, 200, 200 ));
399 circle.SetColor(new ColorPt(0, 1, 0), 3);
400 circle.SetInteriorColor(new ColorPt(0, 0, 1), 3);
401 double[] dash = new double[2];
402 dash[0]=2;dash[1]=4;
403 circle.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 3, 0, 0, dash ) );
404 circle.SetPadding( new Rect(2,2,2,2) );
405 circle.RefreshAppearance();
406 page3.AnnotPushBack(circle);
407 }
408 {
409 Square sq = Square.Create( doc, new Rect(10,200, 80, 300 ) );
410 sq.SetColor(new ColorPt(0, 0, 0), 3);
411 sq.RefreshAppearance();
412 page3.AnnotPushBack( sq );
413 }
414 {
415 Square sq = Square.Create( doc, new Rect(500,200, 580, 300 ) );
416 sq.SetColor(new ColorPt(1, 0, 0), 3);
417 sq.SetInteriorColor(new ColorPt(0, 1, 1), 3);
418 double[] dash = new double[2];
419 dash[0]=4;dash[1]=2;
420 sq.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 6, 0, 0, dash ) );
421 sq.SetPadding( new Rect(4,4,4,4) );
422 sq.RefreshAppearance();
423 page3.AnnotPushBack( sq );
424 }
425 {
426 Polygon poly = Polygon.Create(doc, new Rect(5, 500, 125, 590));
427 poly.SetColor(new ColorPt(1, 0, 0), 3);
428 poly.SetInteriorColor(new ColorPt(1, 1, 0), 3);
429 poly.SetVertex(0, new Point(12,510) );
430 poly.SetVertex(1, new Point(100,510) );
431 poly.SetVertex(2, new Point(100,555) );
432 poly.SetVertex(3, new Point(35,544) );
433 poly.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 4, 0, 0 ) );
434 poly.SetPadding( new Rect(4,4,4,4) );
435 poly.RefreshAppearance();
436 page3.AnnotPushBack( poly );
437 }
438 {
439 PolyLine poly = PolyLine.Create(doc, new Rect(400, 10, 500, 90));
440 poly.SetColor(new ColorPt(1, 0, 0), 3);
441 poly.SetInteriorColor(new ColorPt(0, 1, 0), 3);
442 poly.SetVertex(0, new Point(405,20) );
443 poly.SetVertex(1, new Point(440,40) );
444 poly.SetVertex(2, new Point(410,60) );
445 poly.SetVertex(3, new Point(470,80) );
446 poly.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 2, 0, 0 ) );
447 poly.SetPadding( new Rect(4,4,4,4) );
448 poly.SetStartStyle( Line.EndingStyle.e_RClosedArrow );
449 poly.SetEndStyle( Line.EndingStyle.e_ClosedArrow );
450 poly.RefreshAppearance();
451 page3.AnnotPushBack( poly );
452 }
453 {
454 Link lk = Link.Create( doc, new Rect(5,5,55,24) );
455 //lk.SetColor( ColorPt(0,1,0), 3 );
456 lk.RefreshAppearance();
457 page3.AnnotPushBack( lk );
458 }
459
460
461 Page page4 = doc.PageCreate(new Rect(0, 0, 600, 600));
462 ew.Begin(page4); // begin writing to the page
463 ew.End(); // save changes to the current page
464 doc.PagePushBack(page4);
465
466 {
467 ew.Begin( page4 );
468 Font font = Font.Create(doc, Font.StandardType1Font.e_helvetica);
469 element = eb.CreateTextBegin( font, 16 );
470 element.SetPathFill(true);
471 ew.WriteElement(element);
472 element = eb.CreateTextRun( "Some random text on the page", font, 16 );
473 element.SetTextMatrix(1, 0, 0, 1, 100, 500 );
474 ew.WriteElement(element);
475 ew.WriteElement( eb.CreateTextEnd() );
476 ew.End();
477 }
478 {
479 Highlight hl = Highlight.Create( doc, new Rect(100,490,150,515) );
480 hl.SetColor(new ColorPt(0,1,0), 3 );
481 hl.RefreshAppearance();
482 page4.AnnotPushBack( hl );
483 }
484 {
485 Squiggly sq = Squiggly.Create( doc, new Rect(100,450,250,600) );
486 //sq.SetColor( ColorPt(1,0,0), 3 );
487 sq.SetQuadPoint( 0, new QuadPoint(new Point( 122,455), new Point(240, 545), new Point(230, 595), new Point(101,500 ) ) );
488 sq.RefreshAppearance();
489 page4.AnnotPushBack( sq );
490 }
491 {
492 Caret cr = Caret.Create( doc, new Rect(100,40,129,69) );
493 cr.SetColor( new ColorPt(0,0,1), 3 );
494 cr.SetSymbol( "P" );
495 cr.RefreshAppearance();
496 page4.AnnotPushBack( cr );
497 }
498
499
500 Page page5 = doc.PageCreate(new Rect(0, 0, 600, 600));
501 ew.Begin(page5); // begin writing to the page
502 ew.End(); // save changes to the current page
503 doc.PagePushBack(page5);
504 Page page6 = doc.PageCreate(new Rect(0, 0, 600, 600));
505 ew.Begin(page6); // begin writing to the page
506 ew.End(); // save changes to the current page
507 doc.PagePushBack(page6);
508
509 {
510 Text txt = Text.Create( doc, new Rect( 10, 20, 30, 40 ) );
511 txt.SetIcon( "UserIcon" );
512 txt.SetContents( "User defined icon, unrecognized by appearance generator" );
513 txt.SetColor(new ColorPt(0,1,0) );
514 txt.RefreshAppearance();
515 page6.AnnotPushBack( txt );
516 }
517 {
518 Ink ink = Ink.Create( doc, new Rect( 100, 400, 200, 550 ) );
519 ink.SetColor(new ColorPt(0,0,1) );
520 ink.SetPoint( 1, 3, new Point( 220, 505) );
521 ink.SetPoint( 1, 0, new Point( 100, 490) );
522 ink.SetPoint( 0, 1, new Point( 120, 410) );
523 ink.SetPoint( 0, 0, new Point( 100, 400) );
524 ink.SetPoint( 1, 2, new Point( 180, 490) );
525 ink.SetPoint( 1, 1, new Point( 140, 440) );
526 ink.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 3, 0, 0 ) );
527 ink.RefreshAppearance();
528 page6.AnnotPushBack( ink );
529 }
530
531
532 Page page7 = doc.PageCreate(new Rect(0, 0, 600, 600));
533 ew.Begin(page7); // begin writing to the page
534 ew.End(); // save changes to the current page
535 doc.PagePushBack(page7);
536
537 {
538 Sound snd = Sound.Create( doc, new Rect( 100, 500, 120, 520 ) );
539 snd.SetColor( new ColorPt(1,1,0) );
540 snd.SetIcon(Sound.Icon.e_Speaker );
541 snd.RefreshAppearance();
542 page7.AnnotPushBack( snd );
543 }
544 {
545 Sound snd = Sound.Create( doc, new Rect( 200, 500, 220, 520 ) );
546 snd.SetColor(new ColorPt(1,1,0) );
547 snd.SetIcon(Sound.Icon.e_Mic );
548 snd.RefreshAppearance();
549 page7.AnnotPushBack( snd );
550 }
551
552 Page page8 = doc.PageCreate(new Rect(0, 0, 600, 600));
553 ew.Begin(page8); // begin writing to the page
554 ew.End(); // save changes to the current page
555 doc.PagePushBack(page8);
556
557 for( int ipage =0; ipage < 2; ++ipage ) {
558 double px = 5, py = 520;
559 for (RubberStamp.Icon istamp = RubberStamp.Icon.e_Approved;
560 istamp <= RubberStamp.Icon.e_Draft;
561 istamp = (RubberStamp.Icon) ( (int)(istamp) + 1 ) ) {
562 RubberStamp stmp = RubberStamp.Create( doc, new Rect(1,1,100,100) );
563 stmp.SetIcon( istamp );
564 stmp.SetContents(stmp.GetIconName());
565 stmp.SetRect(new Rect(px, py, px+100, py+25 ) );
566 py -= 100;
567 if( py < 0 ) {
568 py = 520;
569 px += 200;
570 }
571 if( ipage == 0 ){
572 //page7.AnnotPushBack( st );
573 ;
574 }
575 else {
576 page8.AnnotPushBack(stmp);
577 stmp.RefreshAppearance();
578 }
579 }
580 }
581 RubberStamp st = RubberStamp.Create( doc, new Rect(400,5,550,45) );
582 st.SetIcon( "UserStamp" );
583 st.SetContents( "User defined stamp" );
584 page8.AnnotPushBack( st );
585 st.RefreshAppearance();
586
587
588
589 }
590
591
592 // Relative path to the folder containing test files.
593 const string input_path = "../../../../TestFiles/";
594 const string output_path = "../../../../TestFiles/Output/";
595
596 /// <summary>
597 /// The main entry point for the application.
598 /// </summary>
599 [System.STAThread]
600 static void Main(string[] args)
601 {
602 PDFNet.Initialize(PDFTronLicense.Key);
603
604 try
605 {
606 using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf"))
607 {
608 doc.InitSecurityHandler();
609
610 // An example of using SDF/Cos API to add any type of annotations.
611 AnnotationLowLevelAPI(doc);
612 doc.Save(output_path + "annotation_test1.pdf", SDFDoc.SaveOptions.e_linearized);
613 System.Console.WriteLine("Done. Results saved in annotation_test1.pdf");
614
615 // An example of using the high-level PDFNet API to read existing annotations,
616 // to edit existing annotations, and to create new annotation from scratch.
617 AnnotationHighLevelAPI(doc);
618 doc.Save(output_path + "annotation_test2.pdf", SDFDoc.SaveOptions.e_linearized);
619 System.Console.WriteLine("Done. Results saved in annotation_test2.pdf");
620 }
621
622 // an example of creating various annotations in a brand new document
623 using (PDFDoc doc1 = new PDFDoc())
624 {
625 CreateTestAnnots(doc1);
626 doc1.Save(output_path + "new_annot_test_api.pdf", SDFDoc.SaveOptions.e_linearized);
627 System.Console.WriteLine("Saved new_annot_test_api.pdf");
628 }
629 }
630 catch (PDFNetException e)
631 {
632 System.Console.WriteLine(e.Message);
633 }
634 PDFNet.Terminate();
635 }
636 }
637}
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#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementBuilder.h>
9#include <PDF/ElementReader.h>
10#include <PDF/ElementWriter.h>
11#include <PDF/Annot.h>
12#include <SDF/Obj.h>
13#include <iostream>
14#include "../../LicenseKey/CPP/LicenseKey.h"
15
16using namespace pdftron;
17using namespace SDF;
18using namespace PDF;
19using namespace std;
20
21
22std::string output_path = "../../TestFiles/Output/";
23std::string input_path = "../../TestFiles/";
24
25void AnnotationHighLevelAPI(PDFDoc& doc)
26{
27 // The following code snippet traverses all annotations in the document
28 cout << "Traversing all annotations in the document..." << endl;
29
30 UString uri;
31 int page_num=1;
32 for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
33 {
34 cout << "Page " << page_num++ << ": " << endl;
35
36 Page page = itr.Current();
37 int num_annots = page.GetNumAnnots();
38 for (int i=0; i<num_annots; ++i)
39 {
40 Annot annot = page.GetAnnot(i);
41 if (!annot.IsValid()) continue;
42 cout << "Annot Type: " << annot.GetSDFObj().Get("Subtype").Value().GetName() << endl;
43
44 Rect bbox = annot.GetRect();
45 cout << " Position: " << bbox.x1
46 << ", " << bbox.y1
47 << ", " << bbox.x2
48 << ", " << bbox.y2 << endl;
49
50 switch (annot.GetType())
51 {
52 case Annot::e_Link:
53 {
54 Annots::Link link(annot);
55 Action action = link.GetAction();
56 if (!action.IsValid()) continue;
57 if (action.GetType() == Action::e_GoTo)
58 {
59 Destination dest = action.GetDest();
60 if (!dest.IsValid()) {
61 cout << " Destination is not valid" << endl;
62 }
63 else {
64 int page_num = dest.GetPage().GetIndex();
65 cout << " Links to: page number " << page_num << " in this document" << endl;
66 }
67 }
68 else if (action.GetType() == Action::e_URI)
69 {
70 action.GetSDFObj().Get("URI").Value().GetAsPDFText(uri);
71 cout << " Links to: " << uri << endl;
72 }
73 // ...
74 }
75 break;
76 case Annot::e_Widget:
77 break;
78 case Annot::e_FileAttachment:
79 break;
80 // ...
81 default:
82 break;
83 }
84 }
85 }
86
87 // Use the high-level API to create new annotations.
88 Page first_page = doc.GetPage(1);
89
90 // Create a hyperlink...
91 Annots::Link hyperlink = Annots::Link::Create(doc, Rect(85, 570, 503, 524), Action::CreateURI(doc, "http://www.pdftron.com"));
92 first_page.AnnotPushBack(hyperlink);
93
94 // Create an intra-document link...
95 Action goto_page_3 = Action::CreateGoto(Destination::CreateFitH(doc.GetPage(3), 0));
96 Annots::Link link = Annots::Link::Create(doc, Rect(85, 458, 503, 502), goto_page_3);
97 link.SetColor(ColorPt(0, 0, 1));
98
99 // Add the new annotation to the first page
100 first_page.AnnotPushBack(link);
101
102 // Create a stamp annotation ...
103 Annots::RubberStamp stamp = Annots::RubberStamp::Create(doc, Rect(30, 30, 300, 200));
104 stamp.SetIcon("Draft");
105 first_page.AnnotPushBack(stamp);
106
107 // Create a file attachment annotation (embed the 'peppers.jpg').
108 Annots::FileAttachment file_attach = Annots::FileAttachment::Create(doc, Rect(80, 280, 108, 320), (input_path + "peppers.jpg").c_str());
109 first_page.AnnotPushBack(file_attach);
110
111
112 Annots::Ink ink = Annots::Ink::Create(doc, Rect(110, 10, 300, 200));
113 Point pt3(110, 10);
114 //pt3.x = 110; pt3.y = 10;
115 ink.SetPoint(0, 0, pt3);
116 pt3.x = 150; pt3.y = 50;
117 ink.SetPoint(0, 1, pt3);
118 pt3.x = 190; pt3.y = 60;
119 ink.SetPoint(0, 2, pt3);
120 pt3.x = 180; pt3.y = 90;
121 ink.SetPoint(1, 0, pt3);
122 pt3.x = 190; pt3.y = 95;
123 ink.SetPoint(1, 1, pt3);
124 pt3.x = 200; pt3.y = 100;
125 ink.SetPoint(1, 2, pt3);
126 pt3.x = 166; pt3.y = 86;
127 ink.SetPoint(2, 0, pt3);
128 pt3.x = 196; pt3.y = 96;
129 ink.SetPoint(2, 1, pt3);
130 pt3.x = 221; pt3.y = 121;
131 ink.SetPoint(2, 2, pt3);
132 pt3.x = 288; pt3.y = 188;
133 ink.SetPoint(2, 3, pt3);
134 ink.SetColor(ColorPt(0, 1, 1), 3);
135 first_page.AnnotPushBack(ink);
136
137}
138
139void AnnotationLowLevelAPI(PDFDoc& doc)
140{
141 Page page = (doc.GetPageIterator()).Current();
142
143 Obj annots = page.GetAnnots();
144
145 if (!annots)
146 {
147 // If there are no annotations, create a new annotation
148 // array for the page.
149 annots = doc.CreateIndirectArray();
150 page.GetSDFObj().Put("Annots", annots);
151 }
152
153 // Create a Text annotation
154 Obj annot = doc.CreateIndirectDict();
155 annot.PutName("Subtype", "Text");
156 annot.PutBool("Open", true);
157 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
158 annot.PutRect("Rect", 266, 116, 430, 204);
159
160 // Insert the annotation in the page annotation array
161 annots.PushBack(annot);
162
163 // Create a Link annotation
164 Obj link1 = doc.CreateIndirectDict();
165 link1.PutName("Subtype", "Link");
166 Destination dest = Destination::CreateFit(doc.GetPage(2));
167 link1.Put("Dest", dest.GetSDFObj());
168 link1.PutRect("Rect", 85, 705, 503, 661);
169 annots.PushBack(link1);
170
171 // Create another Link annotation
172 Obj link2 = doc.CreateIndirectDict();
173 link2.PutName("Subtype", "Link");
174 Destination dest2 = Destination::CreateFit(doc.GetPage(3));
175 link2.Put("Dest", dest2.GetSDFObj());
176 link2.PutRect("Rect", 85, 638, 503, 594);
177 annots.PushBack(link2);
178
179 // Note that PDFNet API can be used to modify existing annotations.
180 // In the following example we will modify the second link annotation
181 // (link2) so that it points to the 10th page. We also use a different
182 // destination page fit type.
183
184 // link2 = annots.GetAt(annots.Size()-1);
185 link2.Put("Dest",
186 Destination::CreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj());
187
188 // Create a third link annotation with a hyperlink action (all other
189 // annotation types can be created in a similar way)
190 Obj link3 = doc.CreateIndirectDict();
191 link3.PutName("Subtype", "Link");
192 link3.PutRect("Rect", 85, 570, 503, 524);
193
194 // Create a URI action
195 Obj action = link3.PutDict("A");
196 action.PutName("S", "URI");
197 action.PutString("URI", "http://www.pdftron.com");
198
199 annots.PushBack(link3);
200}
201
202
203void CreateTestAnnots(PDFDoc& doc) {
204 using namespace pdftron;
205 using namespace SDF;
206 using namespace PDF;
207 using namespace Annots;
208
209
210 ElementWriter ew;
211 ElementBuilder eb;
212 Element element ;
213
214 Page first_page= doc.PageCreate(Rect(0, 0, 600, 600));
215 doc.PagePushBack(first_page);
216 ew.Begin(first_page, ElementWriter::e_overlay, false ); // begin writing to this page
217 ew.End(); // save changes to the current page
218
219 //
220 // Test of a free text annotation.
221 //
222 {
223 Annots::FreeText txtannot = Annots::FreeText::Create( doc, Rect(10, 400, 160, 570) );
224 txtannot.SetContents( UString("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
225 "\n\nAha!\n\nAnd there was much rejoicing!" ) );
226 //std::vector<double> dash( 2, 2.0 );
227 txtannot.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 1, 10, 20 ), false);
228 txtannot.SetQuaddingFormat(0);
229 first_page.AnnotPushBack(txtannot);
230 txtannot.RefreshAppearance();
231 }
232 {
233 Annots::FreeText txtannot = Annots::FreeText::Create( doc, Rect(100, 100, 350, 500) );
234 txtannot.SetContentRect( Rect( 200, 200, 350, 500 ) );
235 txtannot.SetContents( UString("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
236 "\n\nAha!\n\nAnd there was much rejoicing!" ) );
237 txtannot.SetCalloutLinePoints( Point(200,300), Point(150,290), Point(110,110) );
238 //std::vector<double> dash( 2, 2.0 );
239 txtannot.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 1, 10, 20 ), false);
240 txtannot.SetEndingStyle( Line::e_ClosedArrow );
241 txtannot.SetColor( ColorPt( 0, 1, 0 ) );
242 txtannot.SetQuaddingFormat(1);
243 first_page.AnnotPushBack(txtannot);
244 txtannot.RefreshAppearance();
245 }
246 {
247 Annots::FreeText txtannot = Annots::FreeText::Create( doc, Rect(400, 10, 550, 400) );
248 txtannot.SetContents( UString("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
249 "\n\nAha!\n\nAnd there was much rejoicing!" ) );
250 txtannot.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 1, 10, 20 ), false);
251 txtannot.SetColor( ColorPt( 0, 0, 1 ) );
252 txtannot.SetOpacity( 0.2 );
253 txtannot.SetQuaddingFormat(2);
254 first_page.AnnotPushBack(txtannot);
255 txtannot.RefreshAppearance();
256 }
257
258 Page page= doc.PageCreate(Rect(0, 0, 600, 600));
259 doc.PagePushBack(page);
260 ew.Begin(page, ElementWriter::e_overlay, false ); // begin writing to this page
261 eb.Reset(); // Reset the GState to default
262 ew.End(); // save changes to the current page
263
264 {
265 //Create a Line annotation...
266 Line line=Line::Create(doc, Rect(250, 250, 400, 400));
267 line.SetStartPoint( Point(350, 270 ) );
268 line.SetEndPoint( Point(260,370) );
269 line.SetStartStyle(Line::e_Square);
270 line.SetEndStyle(Line::e_Circle);
271 line.SetColor(ColorPt(.3, .5, 0), 3);
272 line.SetContents( UString("Dashed Captioned") );
273 line.SetShowCaption(true);
274 line.SetCaptionPosition( Line::e_Top );
275 std::vector<double> dash( 2, 2.0 );
276 line.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_dashed, 2, 0, 0, dash ) );
277 line.RefreshAppearance();
278 page.AnnotPushBack(line);
279 }
280 {
281 Line line=Line::Create(doc, Rect(347, 377, 600, 600));
282 line.SetStartPoint( Point(385, 410 ) );
283 line.SetEndPoint( Point(540,555) );
284 line.SetStartStyle(Line::e_Circle);
285 line.SetEndStyle(Line::e_OpenArrow);
286 line.SetColor(ColorPt(1, 0, 0), 3);
287 line.SetInteriorColor(ColorPt(0, 1, 0), 3);
288 line.SetContents( UString("Inline Caption") );
289 line.SetShowCaption(true);
290 line.SetCaptionPosition( Line::e_Inline );
291 line.SetLeaderLineExtensionLength( 4. );
292 line.SetLeaderLineLength( -12. );
293 line.SetLeaderLineOffset( 2. );
294 line.RefreshAppearance();
295 page.AnnotPushBack(line);
296 }
297 {
298 Line line=Line::Create(doc, Rect(10, 400, 200, 600));
299 line.SetStartPoint( Point(25, 426 ) );
300 line.SetEndPoint( Point(180,555) );
301 line.SetStartStyle(Line::e_Circle);
302 line.SetEndStyle(Line::e_Square);
303 line.SetColor(ColorPt(0, 0, 1), 3);
304 line.SetInteriorColor(ColorPt(1, 0, 0), 3);
305 line.SetContents( UString("Offset Caption") );
306 line.SetShowCaption(true);
307 line.SetCaptionPosition( Line::e_Top );
308 line.SetTextHOffset( -60 );
309 line.SetTextVOffset( 10 );
310 line.RefreshAppearance();
311 page.AnnotPushBack(line);
312 }
313 {
314 Line line=Line::Create(doc, Rect(200, 10, 400, 70));
315 line.SetStartPoint( Point(220, 25 ) );
316 line.SetEndPoint( Point(370,60) );
317 line.SetStartStyle(Line::e_Butt);
318 line.SetEndStyle(Line::e_OpenArrow);
319 line.SetColor(ColorPt(0, 0, 1), 3);
320 line.SetContents( UString("Regular Caption") );
321 line.SetShowCaption(true);
322 line.SetCaptionPosition( Line::e_Top );
323 line.RefreshAppearance();
324 page.AnnotPushBack(line);
325 }
326 {
327 Line line=Line::Create(doc, Rect(200, 70, 400, 130));
328 line.SetStartPoint( Point(220, 111 ) );
329 line.SetEndPoint( Point(370,78) );
330 line.SetStartStyle(Line::e_Circle);
331 line.SetEndStyle(Line::e_Diamond);
332 line.SetContents( UString("Circle to Diamond") );
333 line.SetColor(ColorPt(0, 0, 1), 3);
334 line.SetInteriorColor(ColorPt(0, 1, 0), 3);
335 line.SetShowCaption(true);
336 line.SetCaptionPosition( Line::e_Top );
337 line.RefreshAppearance();
338 page.AnnotPushBack(line);
339 }
340 {
341 Line line=Line::Create(doc, Rect(10, 100, 160, 200));
342 line.SetStartPoint( Point(15, 110 ) );
343 line.SetEndPoint( Point(150, 190) );
344 line.SetStartStyle(Line::e_Slash);
345 line.SetEndStyle(Line::e_ClosedArrow);
346 line.SetContents( UString("Slash to CArrow") );
347 line.SetColor(ColorPt(1, 0, 0), 3);
348 line.SetInteriorColor(ColorPt(0, 1, 1), 3);
349 line.SetShowCaption(true);
350 line.SetCaptionPosition( Line::e_Top );
351 line.RefreshAppearance();
352 page.AnnotPushBack(line);
353 }
354 {
355 Line line=Line::Create(doc, Rect( 270, 270, 570, 433 ));
356 line.SetStartPoint( Point(300, 400 ) );
357 line.SetEndPoint( Point(550, 300) );
358 line.SetStartStyle(Line::e_RClosedArrow);
359 line.SetEndStyle(Line::e_ROpenArrow);
360 line.SetContents( UString("ROpen & RClosed arrows") );
361 line.SetColor(ColorPt(0, 0, 1), 3);
362 line.SetInteriorColor(ColorPt(0, 1, 0), 3);
363 line.SetShowCaption(true);
364 line.SetCaptionPosition( Line::e_Top );
365 line.RefreshAppearance();
366 page.AnnotPushBack(line);
367 }
368 {
369 Line line=Line::Create(doc, Rect( 195, 395, 205, 505 ));
370 line.SetStartPoint( Point(200, 400 ) );
371 line.SetEndPoint( Point(200, 500) );
372 line.RefreshAppearance();
373 page.AnnotPushBack(line);
374 }
375 {
376 Line line=Line::Create(doc, Rect( 55, 299, 150, 301 ));
377 line.SetStartPoint( Point(55, 300 ) );
378 line.SetEndPoint( Point(155, 300) );
379 line.SetStartStyle(Line::e_Circle);
380 line.SetEndStyle(Line::e_Circle);
381 line.SetContents( UString("Caption that's longer than its line.") );
382 line.SetColor(ColorPt(1, 0, 1), 3);
383 line.SetInteriorColor(ColorPt(0, 1, 0), 3);
384 line.SetShowCaption(true);
385 line.SetCaptionPosition( Line::e_Top );
386 line.RefreshAppearance();
387 page.AnnotPushBack(line);
388 }
389 {
390 Line line=Line::Create(doc, Rect( 300, 200, 390, 234 ));
391 line.SetStartPoint( Point(310, 210 ) );
392 line.SetEndPoint( Point(380, 220) );
393 line.SetColor(ColorPt(0, 0, 0), 3);
394 line.RefreshAppearance();
395 page.AnnotPushBack(line);
396 }
397
398 Page page3 = doc.PageCreate(Rect(0, 0, 600, 600));
399 ew.Begin(page3); // begin writing to the page
400 ew.End(); // save changes to the current page
401 doc.PagePushBack(page3);
402 {
403 Circle circle=Circle::Create(doc, Rect( 300, 300, 390, 350 ));
404 circle.SetColor(ColorPt(0, 0, 0), 3);
405 circle.RefreshAppearance();
406 page3.AnnotPushBack(circle);
407 }
408 {
409 Circle circle=Circle::Create(doc, Rect( 100, 100, 200, 200 ));
410 circle.SetColor(ColorPt(0, 1, 0), 3);
411 circle.SetInteriorColor(ColorPt(0, 0, 1), 3);
412 std::vector<double> dash( 2 ); dash[0]=2;dash[1]=4;
413 circle.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_dashed, 3, 0, 0, dash ) );
414 circle.SetPadding( 2 );
415 circle.RefreshAppearance();
416 page3.AnnotPushBack(circle);
417 }
418 {
419 Square sq = Square::Create( doc, Rect(10,200, 80, 300 ) );
420 sq.SetColor(ColorPt(0, 0, 0), 3);
421 sq.RefreshAppearance();
422 page3.AnnotPushBack( sq );
423 }
424 {
425 Square sq = Square::Create( doc, Rect(500,200, 580, 300 ) );
426 sq.SetColor(ColorPt(1, 0, 0), 3);
427 sq.SetInteriorColor(ColorPt(0, 1, 1), 3);
428 std::vector<double> dash( 2 ); dash[0]=4;dash[1]=2;
429 sq.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_dashed, 6, 0, 0, dash ) );
430 sq.SetPadding( 4 );
431 sq.RefreshAppearance();
432 page3.AnnotPushBack( sq );
433 }
434 {
435 Polygon poly = Polygon::Create(doc, Rect(5, 500, 125, 590));
436 poly.SetColor(ColorPt(1, 0, 0), 3);
437 poly.SetInteriorColor(ColorPt(1, 1, 0), 3);
438 poly.SetVertex(0, Point(12,510) );
439 poly.SetVertex(1, Point(100,510) );
440 poly.SetVertex(2, Point(100,555) );
441 poly.SetVertex(3, Point(35,544) );
442 poly.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 4, 0, 0 ) );
443 poly.SetPadding( 4 );
444 poly.RefreshAppearance();
445 page3.AnnotPushBack( poly );
446 }
447 {
448 PolyLine poly = PolyLine::Create(doc, Rect(400, 10, 500, 90));
449 poly.SetColor(ColorPt(1, 0, 0), 3);
450 poly.SetInteriorColor(ColorPt(0, 1, 0), 3);
451 poly.SetVertex(0, Point(405,20) );
452 poly.SetVertex(1, Point(440,40) );
453 poly.SetVertex(2, Point(410,60) );
454 poly.SetVertex(3, Point(470,80) );
455 poly.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 2, 0, 0 ) );
456 poly.SetPadding( 4 );
457 poly.SetStartStyle( Line::e_RClosedArrow );
458 poly.SetEndStyle( Line::e_ClosedArrow );
459 poly.RefreshAppearance();
460 page3.AnnotPushBack( poly );
461 }
462 {
463 Link lk = Link::Create( doc, Rect(5,5,55,24) );
464 //lk.SetColor( ColorPt(0,1,0), 3 );
465 lk.RefreshAppearance();
466 page3.AnnotPushBack( lk );
467 }
468
469
470 Page page4 = doc.PageCreate(Rect(0, 0, 600, 600));
471 ew.Begin(page4); // begin writing to the page
472 ew.End(); // save changes to the current page
473 doc.PagePushBack(page4);
474
475 {
476 ew.Begin( page4 );
477 Font font = Font::Create(doc, Font::e_helvetica);
478 element = eb.CreateTextBegin( font, 16 );
479 element.SetPathFill(true);
480 ew.WriteElement(element);
481 element = eb.CreateTextRun( "Some random text on the page", font, 16 );
482 element.SetTextMatrix(1, 0, 0, 1, 100, 500 );
483 ew.WriteElement(element);
484 ew.WriteElement( eb.CreateTextEnd() );
485 ew.End();
486 }
487 {
488 Highlight hl = Highlight::Create( doc, Rect(100,490,150,515) );
489 hl.SetColor( ColorPt(0,1,0), 3 );
490 hl.RefreshAppearance();
491 page4.AnnotPushBack( hl );
492 }
493 {
494 Squiggly sq = Squiggly::Create( doc, Rect(100,450,250,600) );
495 //sq.SetColor( ColorPt(1,0,0), 3 );
496 sq.SetQuadPoint( 0, QuadPoint( Point( 122,455), Point(240, 545), Point(230, 595), Point(101,500 ) ) );
497 sq.RefreshAppearance();
498 page4.AnnotPushBack( sq );
499 }
500 {
501 Caret cr = Caret::Create( doc, Rect(100,40,129,69) );
502 cr.SetColor( ColorPt(0,0,1), 3 );
503 cr.SetSymbol( "P" );
504 cr.RefreshAppearance();
505 page4.AnnotPushBack( cr );
506 }
507
508
509 Page page5 = doc.PageCreate(Rect(0, 0, 600, 600));
510 ew.Begin(page5); // begin writing to the page
511 ew.End(); // save changes to the current page
512 doc.PagePushBack(page5);
513 FileSpec fs = FileSpec::Create( doc, (input_path + "butterfly.png").c_str(), false );
514 Page page6 = doc.PageCreate(Rect(0, 0, 600, 600));
515 ew.Begin(page6); // begin writing to the page
516 ew.End(); // save changes to the current page
517 doc.PagePushBack(page6);
518
519 {
520 Text txt = Text::Create( doc, Rect( 10, 20, 30, 40 ) );
521 txt.SetIcon( "UserIcon" );
522 txt.SetContents( "User defined icon, unrecognized by appearance generator" );
523 txt.SetColor( ColorPt(0,1,0) );
524 txt.RefreshAppearance();
525 page6.AnnotPushBack( txt );
526 }
527 {
528 Ink ink = Ink::Create( doc, Rect( 100, 400, 200, 550 ) );
529 ink.SetColor( ColorPt(0,0,1) );
530 ink.SetPoint( 1, 3, Point( 220, 505) );
531 ink.SetPoint( 1, 0, Point( 100, 490) );
532 ink.SetPoint( 0, 1, Point( 120, 410) );
533 ink.SetPoint( 0, 0, Point( 100, 400) );
534 ink.SetPoint( 1, 2, Point( 180, 490) );
535 ink.SetPoint( 1, 1, Point( 140, 440) );
536 ink.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_solid, 3, 0, 0 ) );
537 ink.RefreshAppearance();
538 page6.AnnotPushBack( ink );
539 }
540
541
542 Page page7 = doc.PageCreate(Rect(0, 0, 600, 600));
543 ew.Begin(page7); // begin writing to the page
544 ew.End(); // save changes to the current page
545 doc.PagePushBack(page7);
546
547 {
548 Sound snd = Sound::Create( doc, Rect( 100, 500, 120, 520 ) );
549 snd.SetColor( ColorPt(1,1,0) );
550 snd.SetIcon( Sound::e_Speaker );
551 snd.RefreshAppearance();
552 page7.AnnotPushBack( snd );
553 }
554 {
555 Sound snd = Sound::Create( doc, Rect( 200, 500, 220, 520 ) );
556 snd.SetColor( ColorPt(1,1,0) );
557 snd.SetIcon( Sound::e_Mic );
558 snd.RefreshAppearance();
559 page7.AnnotPushBack( snd );
560 }
561
562
563
564
565 Page page8 = doc.PageCreate(Rect(0, 0, 600, 600));
566 ew.Begin(page8); // begin writing to the page
567 ew.End(); // save changes to the current page
568 doc.PagePushBack(page8);
569
570 for( int ipage =0; ipage < 2; ++ipage ) {
571 double px = 5, py = 520;
572 for( RubberStamp::Icon istamp = RubberStamp::e_Approved;
573 istamp <= RubberStamp::e_Draft;
574 istamp = static_cast<RubberStamp::Icon>( static_cast<int>(istamp) + 1 ) ) {
575 RubberStamp st = RubberStamp::Create( doc, Rect(1,1,100,100) );
576 st.SetIcon( istamp );
577 st.SetContents( UString( st.GetIconName() ) );
578 st.SetRect( Rect(px, py, px+100, py+25 ) );
579 py -= 100;
580 if( py < 0 ) {
581 py = 520;
582 px += 200;
583 }
584 if( ipage == 0 )
585 //page7.AnnotPushBack( st );
586 ;
587 else {
588 page8.AnnotPushBack( st );
589 st.RefreshAppearance();
590 }
591 }
592 }
593 RubberStamp st = RubberStamp::Create( doc, Rect(400,5,550,45) );
594 st.SetIcon( "UserStamp" );
595 st.SetContents( "User defined stamp" );
596 page8.AnnotPushBack( st );
597 st.RefreshAppearance();
598
599
600
601}
602
603
604
605
606int main(int argc, char *argv[])
607{
608 int ret = 0;
609 PDFNet::Initialize(LicenseKey);
610
611 std::string output_path = "../../TestFiles/Output/";
612
613 try
614 {
615 PDFDoc doc((input_path + "numbered.pdf").c_str());
616 doc.InitSecurityHandler();
617
618 // An example of using SDF/Cos API to add any type of annotations.
619 AnnotationLowLevelAPI(doc);
620 doc.Save((output_path + "annotation_test1.pdf").c_str(), SDFDoc::e_linearized, 0);
621 cout << "Done. Results saved in annotation_test1.pdf" << endl;
622
623 // An example of using the high-level PDFNet API to read existing annotations,
624 // to edit existing annotations, and to create new annotation from scratch.
625 AnnotationHighLevelAPI(doc);
626 doc.Save((output_path + "annotation_test2.pdf").c_str(), SDFDoc::e_linearized, 0);
627 cout << "Done. Results saved in annotation_test2.pdf" << endl;
628
629 // an example of creating various annotations in a brand new document
630 PDFDoc doc1;
631 CreateTestAnnots( doc1 );
632 doc1.Save(output_path + "new_annot_test_api.pdf", SDFDoc::e_linearized, 0);
633 cout << "Saved new_annot_test_api.pdf" << std::endl;
634 }
635 catch(Common::Exception& e)
636 {
637 cout << e << std::endl;
638 ret = 1;
639 }
640 catch(...)
641 {
642 cout << "Unknown Exception" << std::endl;
643 ret = 1;
644 }
645
646 PDFNet::Terminate();
647 return ret;
648}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15// Relative path to the folder containing the test files.
16var inputPath = "../../TestFiles/"
17var outputPath = "../../TestFiles/Output/"
18
19func AnnotationLowLevelAPI(doc PDFDoc){
20 itr := doc.GetPageIterator()
21 page := itr.Current()
22 annots := page.GetAnnots()
23 if (page.GetNumAnnots() == 0){
24 // If there are no annotations, create a new annotation
25 // array for the page.
26 annots = doc.CreateIndirectArray()
27 page.GetSDFObj().Put("Annots", annots)
28 }
29
30 // Create a Text annotation
31 annot := doc.CreateIndirectDict()
32 annot.PutName("Subtype", "Text")
33 annot.PutBool("Open", true)
34 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
35 annot.PutRect("Rect", 266.0, 116.0, 430.0, 204.0)
36
37 // Insert the annotation in the page annotation array
38 annots.PushBack(annot)
39
40 // Create a Link annotation
41 link1 := doc.CreateIndirectDict()
42 link1.PutName("Subtype", "Link")
43 dest := DestinationCreateFit(doc.GetPage(2))
44 link1.Put("Dest", dest.GetSDFObj())
45 link1.PutRect("Rect", 85.0, 705.0, 503.0, 661.0)
46 annots.PushBack(link1)
47
48 // Create another Link annotation
49 link2 := doc.CreateIndirectDict()
50 link2.PutName("Subtype", "Link")
51 dest2 := DestinationCreateFit((doc.GetPage(3)))
52 link2.Put("Dest", dest2.GetSDFObj())
53 link2.PutRect("Rect", 85.0, 638.0, 503.0, 594.0)
54 annots.PushBack(link2)
55
56 // Note that PDFNet APi can be used to modify existing annotations.
57 // In the following example we will modify the second link annotation
58 // (link2) so that it points to the 10th page. We also use a different
59 // destination page fit type.
60
61 // link2 = annots.GetAt(annots.Size()-1)
62 link2.Put("Dest", DestinationCreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj())
63
64 // Create a third link annotation with a hyperlink action (all other
65 // annotation types can be created in a similar way)
66 link3 := doc.CreateIndirectDict()
67 link3.PutName("Subtype", "Link")
68 link3.PutRect("Rect", 85.0, 570.0, 503.0, 524.0)
69
70 // Create a URI action
71 action := link3.PutDict("A")
72 action.PutName("S", "URI")
73 action.PutString("URI", "http://www.pdftron.com")
74
75 annots.PushBack(link3)
76}
77
78func AnnotationHighLevelAPI(doc PDFDoc){
79 // The following code snippet traverses all annotations in the document
80 fmt.Println("Traversing all annotations in the document...")
81 pageNum := 1
82 itr := doc.GetPageIterator()
83
84 for itr.HasNext(){
85 fmt.Println("Page " + strconv.Itoa(pageNum) + ": ")
86 pageNum = pageNum + 1
87 page := itr.Current()
88 numAnnots := page.GetNumAnnots()
89 i := uint(0)
90 for i < numAnnots{
91 annot := page.GetAnnot(i)
92 if (!annot.IsValid()){
93 continue
94 }
95 fmt.Println("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName())
96
97 bbox := annot.GetRect()
98 fmt.Println(" Position: " + fmt.Sprintf("%.0f", bbox.GetX1()) +
99 ", " + fmt.Sprintf("%.0f", bbox.GetY1()) +
100 ", " + fmt.Sprintf("%.0f", bbox.GetX2()) +
101 ", " + fmt.Sprintf("%.0f", bbox.GetY2()))
102
103 atype := annot.GetType()
104
105 if (atype == AnnotE_Link){
106 link := NewLink(annot)
107 action := link.GetAction()
108 if (!action.IsValid()){
109 continue
110 }
111 if (action.GetType() == ActionE_GoTo){
112 dest := action.GetDest()
113 if (!dest.IsValid()){
114 fmt.Println(" Destination is not valid.")
115 }else{
116 pageN := dest.GetPage().GetIndex()
117 fmt.Println(" Links to: page number " + strconv.Itoa(pageN) + " in this document")
118 }
119 }else if (action.GetType() == ActionE_URI){
120 uri := action.GetSDFObj().Get("URI").Value().GetAsPDFText()
121 fmt.Println(" Links to: " + uri)
122 }
123 }else if (atype == AnnotE_Widget){
124 //handle Widget here
125 }else if (atype == AnnotE_FileAttachment){
126 //handle FileAttachment here
127 }
128 i = i + 1
129 }
130 itr.Next()
131 }
132 // Use the high-level API to create new annotations.
133 firstPage := doc.GetPage(1)
134
135 // Create a hyperlink...
136 hyperlink := LinkCreate(doc.GetSDFDoc(), NewRect(85.0, 570.0, 503.0, 524.0), ActionCreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"))
137 firstPage.AnnotPushBack(hyperlink)
138
139 // Create an intra-document link...
140 gotoPage3 := ActionCreateGoto(DestinationCreateFitH(doc.GetPage(3), 0))
141 link := LinkCreate(doc.GetSDFDoc(), NewRect(85.0, 458.0, 503.0, 502.0), gotoPage3)
142 link.SetColor(NewColorPt(0.0, 0.0, 1.0))
143
144 // Add the new annotation to the first page
145 firstPage.AnnotPushBack(link)
146
147 // Create a stamp annotation ...
148 stamp := RubberStampCreate(doc.GetSDFDoc(), NewRect(30.0, 30.0, 300.0, 200.0))
149 stamp.SetIcon("Draft")
150 firstPage.AnnotPushBack(stamp)
151
152 // Create a file attachment annotation (embed the 'peppers.jpg').
153 file_attach := FileAttachmentCreate(doc.GetSDFDoc(), NewRect(80.0, 280.0, 108.0, 320.0), (inputPath + "peppers.jpg"))
154 firstPage.AnnotPushBack(file_attach)
155
156
157 ink := InkCreate(doc.GetSDFDoc(), NewRect(110.0, 10.0, 300.0, 200.0))
158 pt3 := NewPoint(110.0, 10.0)
159 pt3.SetX(110)
160 pt3.SetY(10)
161 ink.SetPoint(0, 0, pt3)
162 pt3.SetX(150)
163 pt3.SetY(50)
164 ink.SetPoint(0, 1, pt3)
165 pt3.SetX(190)
166 pt3.SetY(60)
167 ink.SetPoint(0, 2, pt3)
168 pt3.SetX(180)
169 pt3.SetY(90)
170 ink.SetPoint(1, 0, pt3)
171 pt3.SetX(190)
172 pt3.SetY(95)
173 ink.SetPoint(1, 1, pt3)
174 pt3.SetX(200)
175 pt3.SetY(100)
176 ink.SetPoint(1, 2, pt3)
177 pt3.SetX(166)
178 pt3.SetY(86)
179 ink.SetPoint(2, 0, pt3)
180 pt3.SetX(196)
181 pt3.SetY(96)
182 ink.SetPoint(2, 1, pt3)
183 pt3.SetX(221)
184 pt3.SetY(121)
185 ink.SetPoint(2, 2, pt3)
186 pt3.SetX(288)
187 pt3.SetY(188)
188 ink.SetPoint(2, 3, pt3)
189 ink.SetColor(NewColorPt(0.0, 1.0, 1.0), 3)
190 firstPage.AnnotPushBack(ink)
191}
192
193func CreateTestAnnots(doc PDFDoc){
194 ew := NewElementWriter()
195 eb := NewElementBuilder()
196
197 firstPage := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
198 doc.PagePushBack(firstPage)
199 ew.Begin(firstPage, ElementWriterE_overlay, false ) // begin writing to this page
200 ew.End() // save changes to the current page
201
202 // Test of a free text annotation.
203 txtannot := FreeTextCreate( doc.GetSDFDoc(), NewRect(10.0, 400.0, 160.0, 570.0) )
204 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out " +
205 "of the air by freezing it with an angry glare." +
206 "\n\nAha!\n\nAnd there was much rejoicing!" )
207 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
208 txtannot.SetQuaddingFormat(0)
209 firstPage.AnnotPushBack(txtannot)
210 txtannot.RefreshAppearance()
211
212 txtannot = FreeTextCreate( doc.GetSDFDoc(), NewRect(100.0, 100.0, 350.0, 500.0) )
213 txtannot.SetContentRect( NewRect(200.0, 200.0, 350.0, 500.0 ) )
214 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air " +
215 "by freezing it with an angry glare." +
216 "\n\nAha!\n\nAnd there was much rejoicing!" )
217 txtannot.SetCalloutLinePoints( NewPoint(200.0,300.0), NewPoint(150.0,290.0), NewPoint(110.0,110.0) )
218 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
219 txtannot.SetEndingStyle( LineAnnotE_ClosedArrow )
220 txtannot.SetColor( NewColorPt( 0.0, 1.0, 0.0 ) )
221 txtannot.SetQuaddingFormat(1)
222 firstPage.AnnotPushBack(txtannot)
223 txtannot.RefreshAppearance()
224
225 txtannot = FreeTextCreate( doc.GetSDFDoc(), NewRect(400.0, 10.0, 550.0, 400.0) )
226 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air " +
227 "by freezing it with an angry glare." +
228 "\n\nAha!\n\nAnd there was much rejoicing!" )
229 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
230 txtannot.SetColor( NewColorPt( 0.0, 0.0, 1.0 ) )
231 txtannot.SetOpacity( 0.2 )
232 txtannot.SetQuaddingFormat(2)
233 firstPage.AnnotPushBack(txtannot)
234 txtannot.RefreshAppearance()
235
236 page := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
237 doc.PagePushBack(page)
238 ew.Begin(page, ElementWriterE_overlay, false ) // begin writing to this page
239 eb.Reset() // Reset the GState to default
240 ew.End() // save changes to the current page
241
242 // Create a Line annotation...
243 line := LineAnnotCreate(doc.GetSDFDoc(), NewRect(250.0, 250.0, 400.0, 400.0))
244 line.SetStartPoint( NewPoint(350.0, 270.0 ) )
245 line.SetEndPoint( NewPoint(260.0,370.0) )
246 line.SetStartStyle(LineAnnotE_Square)
247 line.SetEndStyle(LineAnnotE_Circle)
248 line.SetColor(NewColorPt(.3, .5, 0.0), 3)
249 line.SetContents( "Dashed Captioned" )
250 line.SetShowCaption(true)
251 line.SetCaptionPosition( &LineAnnotE_Top )
252 var dash = NewVectorDouble()
253 dash.Add(2.0)
254 dash.Add(2.0)
255 line.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 2.0, 0.0, 0.0, dash ) )
256 dash.Clear()
257 line.RefreshAppearance()
258 page.AnnotPushBack(line)
259
260 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(347.0, 377.0, 600.0, 600.0))
261 line.SetStartPoint( NewPoint(385.0, 410.0 ) )
262 line.SetEndPoint( NewPoint(540.0,555.0) )
263 line.SetStartStyle(LineAnnotE_Circle)
264 line.SetEndStyle(LineAnnotE_OpenArrow)
265 line.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
266 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
267 line.SetContents( "Inline Caption" )
268 line.SetShowCaption(true)
269 line.SetCaptionPosition( &LineAnnotE_Inline )
270 line.SetLeaderLineExtensionLength( -4. )
271 line.SetLeaderLineLength( -12. )
272 line.SetLeaderLineOffset( 2. )
273 line.RefreshAppearance()
274 page.AnnotPushBack(line)
275
276 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(10.0, 400.0, 200.0, 600.0))
277 line.SetStartPoint( NewPoint(25.0, 426.0 ) )
278 line.SetEndPoint( NewPoint(180.0,555.0) )
279 line.SetStartStyle(LineAnnotE_Circle)
280 line.SetEndStyle(LineAnnotE_Square)
281 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
282 line.SetInteriorColor(NewColorPt(1.0, 0.0, 0.0), 3)
283 line.SetContents("Offset Caption")
284 line.SetShowCaption(true)
285 line.SetCaptionPosition( &LineAnnotE_Top )
286 line.SetTextHOffset( -60 )
287 line.SetTextVOffset( 10 )
288 line.RefreshAppearance()
289 page.AnnotPushBack(line)
290
291 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(200.0, 10.0, 400.0, 70.0))
292 line.SetStartPoint( NewPoint(222.0, 25.0 ) )
293 line.SetEndPoint( NewPoint(370.0,60.0) )
294 line.SetStartStyle(LineAnnotE_Butt)
295 line.SetEndStyle(LineAnnotE_OpenArrow)
296 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
297 line.SetContents( "Regular Caption" )
298 line.SetShowCaption(true)
299 line.SetCaptionPosition( &LineAnnotE_Top )
300 line.RefreshAppearance()
301 page.AnnotPushBack(line)
302
303 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(200.0, 70.0, 400.0, 130.0))
304 line.SetStartPoint( NewPoint(220.0, 111.0 ) )
305 line.SetEndPoint( NewPoint(370.0,78.0) )
306 line.SetStartStyle(LineAnnotE_Circle)
307 line.SetEndStyle(LineAnnotE_Diamond)
308 line.SetContents( "Circle to Diamond" )
309 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
310 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
311 line.SetShowCaption(true)
312 line.SetCaptionPosition( &LineAnnotE_Top )
313 line.RefreshAppearance()
314 page.AnnotPushBack(line)
315
316 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(10.0, 100.0, 160.0, 200.0))
317 line.SetStartPoint( NewPoint(15.0, 110.0 ) )
318 line.SetEndPoint( NewPoint(150.0, 190.0) )
319 line.SetStartStyle(LineAnnotE_Slash)
320 line.SetEndStyle(LineAnnotE_ClosedArrow)
321 line.SetContents( "Slash to CArrow" )
322 line.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
323 line.SetInteriorColor(NewColorPt(0.0, 1.0, 1.0), 3)
324 line.SetShowCaption(true)
325 line.SetCaptionPosition( &LineAnnotE_Top )
326 line.RefreshAppearance()
327 page.AnnotPushBack(line)
328
329 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(270.0, 270.0, 570.0, 433.0 ))
330 line.SetStartPoint( NewPoint(300.0, 400.0 ) )
331 line.SetEndPoint( NewPoint(550.0, 300.0) )
332 line.SetStartStyle(LineAnnotE_RClosedArrow)
333 line.SetEndStyle(LineAnnotE_ROpenArrow)
334 line.SetContents( "ROpen & RClosed arrows" )
335 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
336 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
337 line.SetShowCaption(true)
338 line.SetCaptionPosition( &LineAnnotE_Top )
339 line.RefreshAppearance()
340 page.AnnotPushBack(line)
341
342 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(195.0, 395.0, 205.0, 505.0 ))
343 line.SetStartPoint( NewPoint(200.0, 400.0 ) )
344 line.SetEndPoint( NewPoint(200.0, 500.0) )
345 line.RefreshAppearance()
346 page.AnnotPushBack(line)
347
348 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(55.0, 299.0, 150.0, 301.0 ))
349 line.SetStartPoint( NewPoint(55.0, 300.0 ) )
350 line.SetEndPoint( NewPoint(155.0, 300.0) )
351 line.SetStartStyle(LineAnnotE_Circle)
352 line.SetEndStyle(LineAnnotE_Circle)
353 line.SetContents( "Caption that's longer than its line." )
354 line.SetColor(NewColorPt(1.0, 0.0, 1.0), 3)
355 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
356 line.SetShowCaption(true)
357 line.SetCaptionPosition( &LineAnnotE_Top )
358 line.RefreshAppearance()
359 page.AnnotPushBack(line)
360
361 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(300.0, 200.0, 390.0, 234.0 ))
362 line.SetStartPoint( NewPoint(310.0, 210.0 ) )
363 line.SetEndPoint( NewPoint(380.0, 220.0) )
364 line.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
365 line.RefreshAppearance()
366 page.AnnotPushBack(line)
367
368 page3 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
369 ew.Begin(page3) // begin writing to the page
370 ew.End() // save changes to the current page
371 doc.PagePushBack(page3)
372
373 circle := CircleCreate(doc.GetSDFDoc(), NewRect(300.0, 300.0, 390.0, 350.0 ))
374 circle.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
375 circle.RefreshAppearance()
376 page3.AnnotPushBack(circle)
377
378 circle = CircleCreate(doc.GetSDFDoc(), NewRect(100.0, 100.0, 200.0, 200.0 ))
379 circle.SetColor(NewColorPt(0.0, 1.0, 0.0), 3)
380 circle.SetInteriorColor(NewColorPt(0.0, 0.0, 1.0), 3)
381 dash.Add(2.0)
382 dash.Add(4.0)
383 circle.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 3.0, 0.0, 0.0, dash ) )
384 dash.Clear()
385 circle.SetPadding( 2.0 )
386 circle.RefreshAppearance()
387 page3.AnnotPushBack(circle)
388
389 sq := SquareCreate( doc.GetSDFDoc(), NewRect(10.0, 200.0, 80.0, 300.0 ) )
390 sq.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
391 sq.RefreshAppearance()
392 page3.AnnotPushBack( sq )
393
394 sq = SquareCreate( doc.GetSDFDoc(), NewRect(500.0, 200.0, 580.0, 300.0 ) )
395 sq.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
396 sq.SetInteriorColor(NewColorPt(0.0, 1.0, 1.0), 3)
397 dash.Add(4.0)
398 dash.Add(2.0)
399 sq.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 6.0, 0.0, 0.0, dash ) )
400 dash.Clear()
401 sq.SetPadding( 4.0 )
402 sq.RefreshAppearance()
403 page3.AnnotPushBack( sq )
404
405 polygon := PolygonCreate(doc.GetSDFDoc(), NewRect(5.0, 500.0, 125.0, 590.0))
406 polygon.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
407 polygon.SetInteriorColor(NewColorPt(1.0, 1.0, 0.0), 3)
408 polygon.SetVertex(0, NewPoint(12.0,510.0) )
409 polygon.SetVertex(1, NewPoint(100.0,510.0) )
410 polygon.SetVertex(2, NewPoint(100.0,555.0) )
411 polygon.SetVertex(3, NewPoint(35.0,544.0) )
412 polygon.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 4.0, 0.0, 0.0 ) )
413 polygon.SetPadding( 4.0 )
414 polygon.RefreshAppearance()
415 page3.AnnotPushBack( polygon )
416 polyline := PolyLineCreate(doc.GetSDFDoc(), NewRect(400.0, 10.0, 500.0, 90.0))
417 polyline.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
418 polyline.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
419 polyline.SetVertex(0, NewPoint(405.0,20.0) )
420 polyline.SetVertex(1, NewPoint(440.0,40.0) )
421 polyline.SetVertex(2, NewPoint(410.0,60.0) )
422 polyline.SetVertex(3, NewPoint(470.0,80.0) )
423 polyline.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 2.0, 0.0, 0.0 ) )
424 polyline.SetPadding( 4.0 )
425 polyline.SetStartStyle( LineAnnotE_RClosedArrow )
426 polyline.SetEndStyle( LineAnnotE_ClosedArrow )
427 polyline.RefreshAppearance()
428 page3.AnnotPushBack( polyline )
429 lk := LinkCreate( doc.GetSDFDoc(), NewRect(5.0, 5.0, 55.0, 24.0) )
430 lk.RefreshAppearance()
431 page3.AnnotPushBack( lk )
432
433 page4 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
434 ew.Begin(page4) // begin writing to the page
435 ew.End() // save changes to the current page
436 doc.PagePushBack(page4)
437
438 ew.Begin( page4 )
439 font := FontCreate(doc.GetSDFDoc(), FontE_helvetica)
440 element := eb.CreateTextBegin( font, 16.0 )
441 element.SetPathFill(true)
442 ew.WriteElement(element)
443 element = eb.CreateTextRun( "Some random text on the page", font, 16.0 )
444 element.SetTextMatrix(1.0, 0.0, 0.0, 1.0, 100.0, 500.0 )
445 ew.WriteElement(element)
446 ew.WriteElement( eb.CreateTextEnd() )
447 ew.End()
448
449 hl := HighlightAnnotCreate( doc.GetSDFDoc(), NewRect(100.0, 490.0, 150.0, 515.0) )
450 hl.SetColor( NewColorPt(0.0,1.0,0.0), 3 )
451 hl.RefreshAppearance()
452 page4.AnnotPushBack( hl )
453
454 sqly := SquigglyCreate( doc.GetSDFDoc(), NewRect(100.0, 450.0, 250.0, 600.0) )
455 sqly.SetQuadPoint( 0, NewQuadPoint(NewPoint(122.0,455.0), NewPoint(240.0, 545.0), NewPoint(230.0, 595.0), NewPoint(101.0,500.0) ) )
456 sqly.RefreshAppearance()
457 page4.AnnotPushBack( sqly )
458
459 cr := CaretCreate( doc.GetSDFDoc(), NewRect(100.0, 40.0, 129.0, 69.0) )
460 cr.SetColor( NewColorPt(0.0,0.0,1.0), 3 )
461 cr.SetSymbol( "P" )
462 cr.RefreshAppearance()
463 page4.AnnotPushBack( cr )
464
465 page5 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
466 ew.Begin(page5) // begin writing to the page
467 ew.End() // save changes to the current page
468 doc.PagePushBack(page5)
469 //fs := FileSpecCreate( doc.GetSDFDoc(), (inputPath + "butterfly.png"), false )
470 page6 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
471 ew.Begin(page6) // begin writing to the page
472 ew.End() // save changes to the current page
473 doc.PagePushBack(page6)
474
475
476 txt := TextCreate( doc.GetSDFDoc(), NewPoint(10.0, 20.0) )
477 txt.SetIcon( "UserIcon" )
478 txt.SetContents( "User defined icon, unrecognized by appearance generator" )
479 txt.SetColor( NewColorPt(0.0,1.0,0.0) )
480 txt.RefreshAppearance()
481 page6.AnnotPushBack( txt )
482
483 ink := InkCreate( doc.GetSDFDoc(), NewRect(100.0, 400.0, 200.0, 550.0 ) )
484 ink.SetColor( NewColorPt(0.0,0.0,1.0) )
485 ink.SetPoint( 1, 3, NewPoint( 220.0, 505.0) )
486 ink.SetPoint( 1, 0, NewPoint( 100.0, 490.0) )
487 ink.SetPoint( 0, 1, NewPoint( 120.0, 410.0) )
488 ink.SetPoint( 0, 0, NewPoint( 100.0, 400.0) )
489 ink.SetPoint( 1, 2, NewPoint( 180.0, 490.0) )
490 ink.SetPoint( 1, 1, NewPoint( 140.0, 440.0) )
491 ink.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 3.0, 0.0, 0.0 ))
492 ink.RefreshAppearance()
493 page6.AnnotPushBack( ink )
494
495 page7 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
496 ew.Begin(page7) // begin writing to the page
497 ew.End() // save changes to the current page
498 doc.PagePushBack(page7)
499
500 snd := SoundCreate( doc.GetSDFDoc(), NewRect(100.0, 500.0, 120.0, 520.0 ) )
501 snd.SetColor( NewColorPt(1.0,1.0,0.0) )
502 snd.SetIcon( SoundE_Speaker )
503 snd.RefreshAppearance()
504 page7.AnnotPushBack( snd )
505
506 snd = SoundCreate( doc.GetSDFDoc(), NewRect(200.0, 500.0, 220.0, 520.0 ) )
507 snd.SetColor( NewColorPt(1.0,1.0,0.0) )
508 snd.SetIcon( SoundE_Mic )
509 snd.RefreshAppearance()
510 page7.AnnotPushBack( snd )
511
512 page8 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
513 ew.Begin(page8) // begin writing to the page
514 ew.End() // save changes to the current page
515 doc.PagePushBack(page8)
516
517 ipage := 0
518 for ipage<2{
519 px := 5
520 py := 520
521 istamp := RubberStampE_Approved
522 for istamp <= RubberStampE_Draft{
523 st := RubberStampCreate(doc.GetSDFDoc(), NewRect(1.0, 1.0, 100.0, 100.0))
524 st.SetIcon( istamp )
525 st.SetContents( st.GetIconName() )
526 st.SetRect( NewRect(float64(px), float64(py), float64(px+100), float64(py+25) ) )
527 py -= 100
528 if py < 0{
529 py = 520
530 px+=200
531 }
532 if ipage == 0{
533 //page7.AnnotPushBack(st)
534 }else{
535 page8.AnnotPushBack( st )
536 st.RefreshAppearance()
537 }
538 istamp = istamp + 1
539 }
540 ipage = ipage + 1
541 }
542
543 st := RubberStampCreate( doc.GetSDFDoc(), NewRect(400.0, 5.0, 550.0, 45.0) )
544 st.SetIcon( "UserStamp" )
545 st.SetContents( "User defined stamp" )
546 page8.AnnotPushBack( st )
547 st.RefreshAppearance()
548}
549
550func main(){
551 PDFNetInitialize(PDFTronLicense.Key)
552
553 doc := NewPDFDoc(inputPath + "numbered.pdf")
554 doc.InitSecurityHandler()
555
556 // An example of using SDF/Cos API to add any type of annotations.
557 AnnotationLowLevelAPI(doc)
558 doc.Save(outputPath + "annotation_test1.pdf", uint(SDFDocE_remove_unused))
559 fmt.Println("Done. Results saved in annotation_test1.pdf")
560 // An example of using the high-level PDFNet API to read existing annotations,
561 // to edit existing annotations, and to create new annotation from scratch.
562 AnnotationHighLevelAPI(doc)
563 doc.Save((outputPath + "annotation_test2.pdf"), uint(SDFDocE_linearized))
564 doc.Close()
565 fmt.Println("Done. Results saved in annotation_test2.pdf")
566
567 doc1 := NewPDFDoc()
568 CreateTestAnnots(doc1)
569 outfname := outputPath + "new_annot_test_api.pdf"
570 doc1.Save(outfname, uint(SDFDocE_linearized))
571 fmt.Println("Saved new_annot_test_api.pdf")
572 PDFNetTerminate()
573}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.pdf.Annot.BorderStyle;
9import com.pdftron.pdf.annots.*;
10import com.pdftron.sdf.SDFDoc;
11import com.pdftron.sdf.Obj;
12
13import java.text.DecimalFormat;
14
15
16public class AnnotationTest {
17 // Relative path to the folder containing test files.
18 public static final String input_path = "../../TestFiles/";
19
20 public static final DecimalFormat format = new DecimalFormat("0.#");
21
22 static void AnnotationHighLevelAPI(PDFDoc doc) throws PDFNetException {
23 // The following code snippet traverses all annotations in the document
24 System.out.println("Traversing all annotations in the document...");
25
26 int page_num = 1;
27 for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) {
28 System.out.println("Page " + (page_num++) + ": ");
29
30 Page page = itr.next();
31 int num_annots = page.getNumAnnots();
32 for (int i = 0; i < num_annots; ++i) {
33 Annot annot = page.getAnnot(i);
34 if (annot.isValid() == false) continue;
35 System.out.println("Annot Type: " + annot.getSDFObj().get("Subtype").value().getName());
36
37 double[] bbox = annot.getRect().get();
38 System.out.println(" Position: " + format.format(bbox[0])
39 + ", " + format.format(bbox[1])
40 + ", " + format.format(bbox[2])
41 + ", " + format.format(bbox[3]));
42
43 switch (annot.getType()) {
44 case Annot.e_Link: {
45 com.pdftron.pdf.annots.Link link = new com.pdftron.pdf.annots.Link(annot);
46 Action action = link.getAction();
47 if (action.isValid() == false) continue;
48 if (action.getType() == Action.e_GoTo) {
49 Destination dest = action.getDest();
50 if (dest.isValid() == false) {
51 System.out.println(" Destination is not valid.");
52 } else {
53 int page_link = dest.getPage().getIndex();
54 System.out.println(" Links to: page number " + page_link + " in this document");
55 }
56 } else if (action.getType() == Action.e_URI) {
57 String uri = action.getSDFObj().get("URI").value().getAsPDFText();
58 System.out.println(" Links to: " + uri);
59 }
60 // ...
61 }
62 break;
63 case Annot.e_Widget:
64 break;
65 case Annot.e_FileAttachment:
66 break;
67 // ...
68 default:
69 break;
70 }
71 }
72 }
73
74 // Use the high-level API to create new annotations.
75 Page first_page = doc.getPage(1);
76
77 // Create a hyperlink...
78 com.pdftron.pdf.annots.Link hyperlink = com.pdftron.pdf.annots.Link.create(doc, new Rect(85, 570, 503, 524), Action.createURI(doc, "http://www.pdftron.com"));
79 first_page.annotPushBack(hyperlink);
80
81 // Create an intra-document link...
82 Action goto_page_3 = Action.createGoto(Destination.createFitH(doc.getPage(3), 0));
83 com.pdftron.pdf.annots.Link link = com.pdftron.pdf.annots.Link.create(doc.getSDFDoc(),
84 new Rect(85, 458, 503, 502),
85 goto_page_3);
86 link.setColor(new ColorPt(0, 0, 1), 3);
87
88 // Add the new annotation to the first page
89 first_page.annotPushBack(link);
90
91 // Create a stamp annotation ...
92 com.pdftron.pdf.annots.RubberStamp stamp = com.pdftron.pdf.annots.RubberStamp.create(doc, new Rect(30, 30, 300, 200));
93 stamp.setIcon("Draft");
94 first_page.annotPushBack(stamp);
95
96 // Create a file attachment annotation (embed the 'peppers.jpg').
97 com.pdftron.pdf.annots.FileAttachment file_attach = com.pdftron.pdf.annots.FileAttachment.create(doc, new Rect(80, 280, 108, 320), (input_path + "peppers.jpg"));
98 first_page.annotPushBack(file_attach);
99
100
101 com.pdftron.pdf.annots.Ink ink = com.pdftron.pdf.annots.Ink.create(doc, new Rect(110, 10, 300, 200));
102 Point pt3 = new Point(110, 10);
103 //pt3.x = 110; pt3.y = 10;
104 ink.setPoint(0, 0, pt3);
105 pt3.x = 150;
106 pt3.y = 50;
107 ink.setPoint(0, 1, pt3);
108 pt3.x = 190;
109 pt3.y = 60;
110 ink.setPoint(0, 2, pt3);
111 pt3.x = 180;
112 pt3.y = 90;
113 ink.setPoint(1, 0, pt3);
114 pt3.x = 190;
115 pt3.y = 95;
116 ink.setPoint(1, 1, pt3);
117 pt3.x = 200;
118 pt3.y = 100;
119 ink.setPoint(1, 2, pt3);
120 pt3.x = 166;
121 pt3.y = 86;
122 ink.setPoint(2, 0, pt3);
123 pt3.x = 196;
124 pt3.y = 96;
125 ink.setPoint(2, 1, pt3);
126 pt3.x = 221;
127 pt3.y = 121;
128 ink.setPoint(2, 2, pt3);
129 pt3.x = 288;
130 pt3.y = 188;
131 ink.setPoint(2, 3, pt3);
132 ink.setColor(new ColorPt(0, 1, 1), 3);
133 first_page.annotPushBack(ink);
134
135 }
136
137 static void AnnotationLowLevelAPI(PDFDoc doc) throws PDFNetException {
138 Page page = doc.getPageIterator().next();
139
140 Obj annots = page.getAnnots();
141
142 if (annots == null) {
143 // If there are no annotations, create a new annotation
144 // array for the page.
145 annots = doc.createIndirectArray();
146 page.getSDFObj().put("Annots", annots);
147 }
148
149 // Create a Text annotation
150 Obj annot = doc.createIndirectDict();
151 annot.putName("Subtype", "Text");
152 annot.putBool("Open", true);
153 annot.putString("Contents", "The quick brown fox ate the lazy mouse.");
154 annot.putRect("Rect", 266, 116, 430, 204);
155
156 // Insert the annotation in the page annotation array
157 annots.pushBack(annot);
158
159 // Create a Link annotation
160 Obj link1 = doc.createIndirectDict();
161 link1.putName("Subtype", "Link");
162 Destination dest = Destination.createFit(doc.getPage(2));
163 link1.put("Dest", dest.getSDFObj());
164 link1.putRect("Rect", 85, 705, 503, 661);
165 annots.pushBack(link1);
166
167 // Create another Link annotation
168 Obj link2 = doc.createIndirectDict();
169 link2.putName("Subtype", "Link");
170 Destination dest2 = Destination.createFit(doc.getPage(3));
171 link2.put("Dest", dest2.getSDFObj());
172 link2.putRect("Rect", 85, 638, 503, 594);
173 annots.pushBack(link2);
174
175 // Note that PDFNet APi can be used to modify existing annotations.
176 // In the following example we will modify the second link annotation
177 // (link2) so that it points to the 10th page. We also use a different
178 // destination page fit type.
179
180 // link2 = annots.GetAt(annots.Size()-1);
181 link2.put("Dest",
182 Destination.createXYZ(doc.getPage(10), 100, 792 - 70, 10).getSDFObj());
183
184 // Create a third link annotation with a hyperlink action (all other
185 // annotation types can be created in a similar way)
186 Obj link3 = doc.createIndirectDict();
187 link3.putName("Subtype", "Link");
188 link3.putRect("Rect", 85, 570, 503, 524);
189
190 // Create a URI action
191 Obj action = link3.putDict("A");
192 action.putName("S", "URI");
193 action.putString("URI", "http://www.pdftron.com");
194
195 annots.pushBack(link3);
196 }
197
198
199 static void CreateTestAnnots(PDFDoc doc) throws PDFNetException {
200 ElementWriter ew = new ElementWriter();
201 ElementBuilder eb = new ElementBuilder();
202 Element element;
203
204 Page first_page = doc.pageCreate(new Rect(0, 0, 600, 600));
205 doc.pagePushBack(first_page);
206 ew.begin(first_page, ElementWriter.e_overlay, false); // begin writing to this page
207 ew.end(); // save changes to the current page
208
209 //
210 // Test of a free text annotation.
211 //
212 {
213 FreeText txtannot = FreeText.create(doc, new Rect(10, 400, 160, 570));
214 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
215 "\n\nAha!\n\nAnd there was much rejoicing!");
216 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
217 txtannot.setQuaddingFormat(0);
218 first_page.annotPushBack(txtannot);
219 txtannot.refreshAppearance();
220 }
221 {
222 FreeText txtannot = FreeText.create(doc, new Rect(100, 100, 350, 500));
223 txtannot.setContentRect(new Rect(200, 200, 350, 500));
224 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
225 "\n\nAha!\n\nAnd there was much rejoicing!");
226 txtannot.setCalloutLinePoints(new Point(200, 300), new Point(150, 290), new Point(110, 110));
227 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
228 txtannot.setEndingStyle(Line.e_ClosedArrow);
229 txtannot.setColor(new ColorPt(0, 1, 0));
230 txtannot.setQuaddingFormat(1);
231 first_page.annotPushBack(txtannot);
232 txtannot.refreshAppearance();
233 }
234 {
235 FreeText txtannot = FreeText.create(doc, new Rect(400, 10, 550, 400));
236 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
237 "\n\nAha!\n\nAnd there was much rejoicing!");
238 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
239 txtannot.setColor(new ColorPt(0, 0, 1));
240 txtannot.setOpacity(0.2);
241 txtannot.setQuaddingFormat(2);
242 first_page.annotPushBack(txtannot);
243 txtannot.refreshAppearance();
244 }
245
246 Page page = doc.pageCreate(new Rect(0, 0, 600, 600));
247 doc.pagePushBack(page);
248 ew.begin(page, ElementWriter.e_overlay, false); // begin writing to this page
249 eb.reset(); // Reset the GState to default
250 ew.end(); // save changes to the current page
251
252 {
253 //Create a Line annotation...
254 Line line = Line.create(doc, new Rect(250, 250, 400, 400));
255 line.setStartPoint(new Point(350, 270));
256 line.setEndPoint(new Point(260, 370));
257 line.setStartStyle(Line.e_Square);
258 line.setEndStyle(Line.e_Circle);
259 line.setColor(new ColorPt(.3, .5, 0), 3);
260 line.setContents("Dashed Captioned");
261 line.setShowCaption(true);
262 line.setCaptionPosition(Line.e_Top);
263 double[] dash = {2, 2.0};
264 line.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 2, 0, 0, dash));
265 line.refreshAppearance();
266 page.annotPushBack(line);
267 }
268 {
269 Line line = Line.create(doc, new Rect(347, 377, 600, 600));
270 line.setStartPoint(new Point(385, 410));
271 line.setEndPoint(new Point(540, 555));
272 line.setStartStyle(Line.e_Circle);
273 line.setEndStyle(Line.e_OpenArrow);
274 line.setColor(new ColorPt(1, 0, 0), 3);
275 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
276 line.setContents("Inline Caption");
277 line.setShowCaption(true);
278 line.setCaptionPosition(Line.e_Inline);
279 line.setLeaderLineExtensionLength(4.);
280 line.setLeaderLineLength(-12.);
281 line.setLeaderLineOffset(2.);
282 line.refreshAppearance();
283 page.annotPushBack(line);
284 }
285 {
286 Line line = Line.create(doc, new Rect(10, 400, 200, 600));
287 line.setStartPoint(new Point(25, 426));
288 line.setEndPoint(new Point(180, 555));
289 line.setStartStyle(Line.e_Circle);
290 line.setEndStyle(Line.e_Square);
291 line.setColor(new ColorPt(0, 0, 1), 3);
292 line.setInteriorColor(new ColorPt(1, 0, 0), 3);
293 line.setContents("Offset Caption");
294 line.setShowCaption(true);
295 line.setCaptionPosition(Line.e_Top);
296 line.setTextHOffset(-60);
297 line.setTextVOffset(10);
298 line.refreshAppearance();
299 page.annotPushBack(line);
300 }
301 {
302 Line line = Line.create(doc, new Rect(200, 10, 400, 70));
303 line.setStartPoint(new Point(220, 25));
304 line.setEndPoint(new Point(370, 60));
305 line.setStartStyle(Line.e_Butt);
306 line.setEndStyle(Line.e_OpenArrow);
307 line.setColor(new ColorPt(0, 0, 1), 3);
308 line.setContents("Regular Caption");
309 line.setShowCaption(true);
310 line.setCaptionPosition(Line.e_Top);
311 line.refreshAppearance();
312 page.annotPushBack(line);
313 }
314 {
315 Line line = Line.create(doc, new Rect(200, 70, 400, 130));
316 line.setStartPoint(new Point(220, 111));
317 line.setEndPoint(new Point(370, 78));
318 line.setStartStyle(Line.e_Circle);
319 line.setEndStyle(Line.e_Diamond);
320 line.setContents("Circle to Diamond");
321 line.setColor(new ColorPt(0, 0, 1), 3);
322 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
323 line.setShowCaption(true);
324 line.setCaptionPosition(Line.e_Top);
325 line.refreshAppearance();
326 page.annotPushBack(line);
327 }
328 {
329 Line line = Line.create(doc, new Rect(10, 100, 160, 200));
330 line.setStartPoint(new Point(15, 110));
331 line.setEndPoint(new Point(150, 190));
332 line.setStartStyle(Line.e_Slash);
333 line.setEndStyle(Line.e_ClosedArrow);
334 line.setContents("Slash to CArrow");
335 line.setColor(new ColorPt(1, 0, 0), 3);
336 line.setInteriorColor(new ColorPt(0, 1, 1), 3);
337 line.setShowCaption(true);
338 line.setCaptionPosition(Line.e_Top);
339 line.refreshAppearance();
340 page.annotPushBack(line);
341 }
342 {
343 Line line = Line.create(doc, new Rect(270, 270, 570, 433));
344 line.setStartPoint(new Point(300, 400));
345 line.setEndPoint(new Point(550, 300));
346 line.setStartStyle(Line.e_RClosedArrow);
347 line.setEndStyle(Line.e_ROpenArrow);
348 line.setContents("ROpen & RClosed arrows");
349 line.setColor(new ColorPt(0, 0, 1), 3);
350 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
351 line.setShowCaption(true);
352 line.setCaptionPosition(Line.e_Top);
353 line.refreshAppearance();
354 page.annotPushBack(line);
355 }
356 {
357 Line line = Line.create(doc, new Rect(195, 395, 205, 505));
358 line.setStartPoint(new Point(200, 400));
359 line.setEndPoint(new Point(200, 500));
360 line.refreshAppearance();
361 page.annotPushBack(line);
362 }
363 {
364 Line line = Line.create(doc, new Rect(55, 299, 150, 301));
365 line.setStartPoint(new Point(55, 300));
366 line.setEndPoint(new Point(155, 300));
367 line.setStartStyle(Line.e_Circle);
368 line.setEndStyle(Line.e_Circle);
369 line.setContents("Caption that's longer than its line.");
370 line.setColor(new ColorPt(1, 0, 1), 3);
371 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
372 line.setShowCaption(true);
373 line.setCaptionPosition(Line.e_Top);
374 line.refreshAppearance();
375 page.annotPushBack(line);
376 }
377 {
378 Line line = Line.create(doc, new Rect(300, 200, 390, 234));
379 line.setStartPoint(new Point(310, 210));
380 line.setEndPoint(new Point(380, 220));
381 line.setColor(new ColorPt(0, 0, 0), 3);
382 line.refreshAppearance();
383 page.annotPushBack(line);
384 }
385
386 Page page3 = doc.pageCreate(new Rect(0, 0, 600, 600));
387 ew.begin(page3); // begin writing to the page
388 ew.end(); // save changes to the current page
389 doc.pagePushBack(page3);
390 {
391 Circle circle = Circle.create(doc, new Rect(300, 300, 390, 350));
392 circle.setColor(new ColorPt(0, 0, 0), 3);
393 circle.refreshAppearance();
394 page3.annotPushBack(circle);
395 }
396 {
397 Circle circle = Circle.create(doc, new Rect(100, 100, 200, 200));
398 circle.setColor(new ColorPt(0, 1, 0), 3);
399 circle.setInteriorColor(new ColorPt(0, 0, 1), 3);
400 double[] dash = {2, 4};
401 circle.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 3, 0, 0, dash));
402 circle.setPadding(2);
403 circle.refreshAppearance();
404 page3.annotPushBack(circle);
405 }
406 {
407 Square sq = Square.create(doc, new Rect(10, 200, 80, 300));
408 sq.setColor(new ColorPt(0, 0, 0), 3);
409 sq.refreshAppearance();
410 page3.annotPushBack(sq);
411 }
412 {
413 Square sq = Square.create(doc, new Rect(500, 200, 580, 300));
414 sq.setColor(new ColorPt(1, 0, 0), 3);
415 sq.setInteriorColor(new ColorPt(0, 1, 1), 3);
416 double[] dash = {4, 2};
417 sq.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 6, 0, 0, dash));
418 sq.setPadding(4);
419 sq.refreshAppearance();
420 page3.annotPushBack(sq);
421 }
422 {
423 Polygon poly = Polygon.create(doc, new Rect(5, 500, 125, 590));
424 poly.setColor(new ColorPt(1, 0, 0), 3);
425 poly.setInteriorColor(new ColorPt(1, 1, 0), 3);
426 poly.setVertex(0, new Point(12, 510));
427 poly.setVertex(1, new Point(100, 510));
428 poly.setVertex(2, new Point(100, 555));
429 poly.setVertex(3, new Point(35, 544));
430 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 4, 0, 0));
431 poly.setPadding(4);
432 poly.refreshAppearance();
433 page3.annotPushBack(poly);
434 }
435 {
436 PolyLine poly = PolyLine.create(doc, new Rect(400, 10, 500, 90));
437 poly.setColor(new ColorPt(1, 0, 0), 3);
438 poly.setInteriorColor(new ColorPt(0, 1, 0), 3);
439 poly.setVertex(0, new Point(405, 20));
440 poly.setVertex(1, new Point(440, 40));
441 poly.setVertex(2, new Point(410, 60));
442 poly.setVertex(3, new Point(470, 80));
443 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 2, 0, 0));
444 poly.setPadding(4);
445 poly.setStartStyle(Line.e_RClosedArrow);
446 poly.setEndStyle(Line.e_ClosedArrow);
447 poly.refreshAppearance();
448 page3.annotPushBack(poly);
449 }
450 {
451 Link lk = Link.create(doc, new Rect(5, 5, 55, 24));
452 lk.refreshAppearance();
453 page3.annotPushBack(lk);
454 }
455
456
457 Page page4 = doc.pageCreate(new Rect(0, 0, 600, 600));
458 ew.begin(page4); // begin writing to the page
459 ew.end(); // save changes to the current page
460 doc.pagePushBack(page4);
461
462 {
463 ew.begin(page4);
464 Font font = Font.create(doc, Font.e_helvetica);
465 element = eb.createTextBegin(font, 16);
466 element.setPathFill(true);
467 ew.writeElement(element);
468 element = eb.createTextRun("Some random text on the page", font, 16);
469 element.setTextMatrix(1, 0, 0, 1, 100, 500);
470 ew.writeElement(element);
471 ew.writeElement(eb.createTextEnd());
472 ew.end();
473 }
474 {
475 Highlight hl = Highlight.create(doc, new Rect(100, 490, 150, 515));
476 hl.setColor(new ColorPt(0, 1, 0), 3);
477 hl.refreshAppearance();
478 page4.annotPushBack(hl);
479 }
480 {
481 Squiggly sq = Squiggly.create(doc, new Rect(100, 450, 250, 600));
482 sq.setQuadPoint(0, new QuadPoint(new Point(122, 455), new Point(240, 545), new Point(230, 595), new Point(101, 500)));
483 sq.refreshAppearance();
484 page4.annotPushBack(sq);
485 }
486 {
487 Caret cr = Caret.create(doc, new Rect(100, 40, 129, 69));
488 cr.setColor(new ColorPt(0, 0, 1), 3);
489 cr.setSymbol("P");
490 cr.refreshAppearance();
491 page4.annotPushBack(cr);
492 }
493
494
495 Page page5 = doc.pageCreate(new Rect(0, 0, 600, 600));
496 ew.begin(page5); // begin writing to the page
497 ew.end(); // save changes to the current page
498 doc.pagePushBack(page5);
499 FileSpec fs = FileSpec.create(doc, input_path + "butterfly.png", false);
500 Page page6 = doc.pageCreate(new Rect(0, 0, 600, 600));
501 ew.begin(page6); // begin writing to the page
502 ew.end(); // save changes to the current page
503 doc.pagePushBack(page6);
504
505 {
506 Text txt = Text.create(doc, new Point(10, 20));
507 txt.setIcon("UserIcon");
508 txt.setContents("User defined icon, unrecognized by appearance generator");
509 txt.setColor(new ColorPt(0, 1, 0));
510 txt.refreshAppearance();
511 page6.annotPushBack(txt);
512 }
513 {
514 Ink ink = Ink.create(doc, new Rect(100, 400, 200, 550));
515 ink.setColor(new ColorPt(0, 0, 1));
516 ink.setPoint(1, 3, new Point(220, 505));
517 ink.setPoint(1, 0, new Point(100, 490));
518 ink.setPoint(0, 1, new Point(120, 410));
519 ink.setPoint(0, 0, new Point(100, 400));
520 ink.setPoint(1, 2, new Point(180, 490));
521 ink.setPoint(1, 1, new Point(140, 440));
522 ink.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 3, 0, 0));
523 ink.refreshAppearance();
524 page6.annotPushBack(ink);
525 }
526
527
528 Page page7 = doc.pageCreate(new Rect(0, 0, 600, 600));
529 ew.begin(page7); // begin writing to the page
530 ew.end(); // save changes to the current page
531 doc.pagePushBack(page7);
532
533 {
534 Sound snd = Sound.create(doc, new Rect(100, 500, 120, 520));
535 snd.setColor(new ColorPt(1, 1, 0));
536 snd.setIcon(Sound.e_Speaker);
537 snd.refreshAppearance();
538 page7.annotPushBack(snd);
539 }
540 {
541 Sound snd = Sound.create(doc, new Rect(200, 500, 220, 520));
542 snd.setColor(new ColorPt(1, 1, 0));
543 snd.setIcon(Sound.e_Mic);
544 snd.refreshAppearance();
545 page7.annotPushBack(snd);
546 }
547
548 Page page8 = doc.pageCreate(new Rect(0, 0, 600, 600));
549 ew.begin(page8); // begin writing to the page
550 ew.end(); // save changes to the current page
551 doc.pagePushBack(page8);
552
553 for (int ipage = 0; ipage < 2; ++ipage) {
554 double px = 5, py = 520;
555 for (int istamp = 0; istamp <= RubberStamp.e_Draft; ++istamp) {
556 RubberStamp st = RubberStamp.create(doc, new Rect(1, 1, 100, 100));
557 st.SetIcon(istamp);
558 st.setContents(st.getIconName());
559 st.setRect(new Rect(px, py, px + 100, py + 25));
560 py -= 100;
561 if (py < 0) {
562 py = 520;
563 px += 200;
564 }
565 if (ipage == 0)
566 //page7.AnnotPushBack( st );
567 ;
568 else {
569 page8.annotPushBack(st);
570 st.refreshAppearance();
571 }
572 }
573 }
574 RubberStamp st = RubberStamp.create(doc, new Rect(400, 5, 550, 45));
575 st.setIcon("UserStamp");
576 st.setContents("User defined stamp");
577 page8.annotPushBack(st);
578 st.refreshAppearance();
579 }
580
581 public static void main(String[] args) {
582 PDFNet.initialize(PDFTronLicense.Key());
583
584 String output_path = "../../TestFiles/Output/";
585
586 try (PDFDoc doc = new PDFDoc((input_path + "numbered.pdf"))) {
587 doc.initSecurityHandler();
588
589 // An example of using SDF/Cos API to add any type of annotations.
590 AnnotationLowLevelAPI(doc);
591 doc.save(output_path + "annotation_test1.pdf", SDFDoc.SaveMode.LINEARIZED, null);
592 // output PDF doc
593 System.out.println("Done. Results saved in annotation_test1.pdf");
594
595 // An example of using the high-level PDFNet API to read existing annotations,
596 // to edit existing annotations, and to create new annotation from scratch.
597 AnnotationHighLevelAPI(doc);
598 doc.save(output_path + "annotation_test2.pdf", SDFDoc.SaveMode.LINEARIZED, null);
599 System.out.println("Done. Results saved in annotation_test2.pdf");
600 } catch (Exception e) {
601 System.out.println(e);
602 }
603
604 // an example of creating various annotations in a brand new document
605 try (PDFDoc doc1 = new PDFDoc()) {
606 CreateTestAnnots(doc1);
607 doc1.save(output_path + "new_annot_test_api.pdf", SDFDoc.SaveMode.LINEARIZED, null);
608 System.out.println("Saved new_annot_test_api.pdf");
609 } catch (Exception e) {
610 System.out.println(e);
611 }
612
613 PDFNet.terminate();
614 }
615
616}
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
7const { PDFNet } = require('@pdftron/pdfnet-node');
8const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
10((exports) => {
11 exports.runAnnotationTest = async() => {
12
13 const inputPath = '../TestFiles/';
14
15 const AnnotationHighLevelAPI = async(doc) => {
16 await PDFNet.startDeallocateStack(); // start stack-based deallocation. All objects will be deallocated by end of function
17
18 // The following code snippet traverses all annotations in the document
19 console.log('Traversing all annotations in the document...');
20
21 let pageNum = 0;
22 const itr = await doc.getPageIterator();
23 for (itr; (await itr.hasNext()); (await itr.next())) {
24 pageNum += 1;
25 console.log('Page ' + pageNum + ': ');
26 const page = await itr.current();
27 const numAnnots = await page.getNumAnnots();
28 for (let i = 0; i < numAnnots; ++i) {
29 const annot = await page.getAnnot(i);
30 if (!(await annot.isValid())) {
31 continue;
32 }
33
34 const annotSDF = await annot.getSDFObj();
35 const subType = await annotSDF.get('Subtype');
36 const subTypeVal = await subType.value();
37
38 let outputString = 'Annot Type: ' + (await subTypeVal.getName());
39 console.log(outputString);
40 const bbox = await annot.getRect();
41 outputString = ' Position: ' + bbox.x1 + ', ' + bbox.y1 + ', ' + bbox.x2 + ', ' + bbox.y2;
42 console.log(outputString);
43 const annotType = await annot.getType();
44 switch (annotType) {
45 case PDFNet.Annot.Type.e_Link:
46 {
47 const link = await PDFNet.LinkAnnot.createFromAnnot(annot);
48 const action = await link.getAction();
49 if (!(await action.isValid())) {
50 continue;
51 }
52
53 if ((await action.getType()) === PDFNet.Action.Type.e_GoTo) {
54 const dest = await action.getDest();
55 if (!(await dest.isValid())) {
56 console.log(' Destination is not valid');
57 } else {
58 const pageNumOut = await (await dest.getPage()).getIndex();
59 console.log(' Links to: page number ' + pageNumOut + ' in this document');
60 }
61 } else if ((await action.getType()) === PDFNet.Action.Type.e_URI) {
62 const SDFObj = await action.getSDFObj();
63 const URI = await SDFObj.get('URI');
64 const URIval = await URI.value();
65 const URIText = await URIval.getAsPDFText(); // An Exception is thrown if this is not a Obj::Type::e_string.
66 console.log(' Links to: ' + URIText); // Other get methods such as getNumber do not work either, although some do, so confusing.
67 // deallocate dictionary object on C side
68 URI.destroy();
69 }
70 }
71 break;
72 case PDFNet.Annot.Type.e_Widget:
73 break;
74 case PDFNet.Annot.Type.e_FileAttachment:
75 break;
76 default:
77 break;
78 }
79
80 await subType.destroy();
81 }
82 }
83 // create a hyperlink
84 const firstPage = await doc.getPage(1);
85 const createURIAction = await PDFNet.Action.createURI(doc, 'http://www.pdftron.com');
86 const linkRect = new PDFNet.Rect(85, 570, 503, 524);
87 const hyperlink = await PDFNet.LinkAnnot.create(doc, linkRect);
88 await hyperlink.setAction(createURIAction);
89 await firstPage.annotPushBack(hyperlink);
90
91 // Create an intra-document link...
92 const page3 = await doc.getPage(3);
93 const gotoPage3 = await PDFNet.Action.createGoto(await PDFNet.Destination.createFitH(page3, 0));
94 const link = await PDFNet.LinkAnnot.create(doc, (new PDFNet.Rect(85, 458, 503, 502)));
95 await link.setAction(gotoPage3);
96 const greenColorPt = await PDFNet.ColorPt.init(0, 0, 1);
97 await link.setColor(greenColorPt);
98
99 // Add the new annotation to the first page
100 await firstPage.annotPushBack(link);
101
102 // Create a stamp annotation ...
103 const stamp = await PDFNet.RubberStampAnnot.create(doc, (new PDFNet.Rect(30, 30, 300, 200)));
104 await stamp.setIconName('Draft');
105 await firstPage.annotPushBack(stamp);
106
107 // Create a file attachment annotation (embed the 'peppers.jpg').
108 const file_attach = await PDFNet.FileAttachmentAnnot.createDefault(doc, (new PDFNet.Rect(80, 280, 108, 320)), inputPath + 'peppers.jpg');
109 await firstPage.annotPushBack(file_attach);
110
111 const ink = await PDFNet.InkAnnot.create(doc, (new PDFNet.Rect(110, 10, 300, 200)));
112 const pt3 = new PDFNet.Point(110, 10);
113 await ink.setPoint(0, 0, pt3);
114 pt3.x = 150;
115 pt3.y = 50;
116 await ink.setPoint(0, 1, pt3);
117 pt3.x = 190;
118 pt3.y = 60;
119 await ink.setPoint(0, 2, pt3);
120 pt3.x = 180;
121 pt3.y = 90;
122 await ink.setPoint(1, 0, pt3);
123 pt3.x = 190;
124 pt3.y = 95;
125 await ink.setPoint(1, 1, pt3);
126 pt3.x = 200;
127 pt3.y = 100;
128 await ink.setPoint(1, 2, pt3);
129 pt3.x = 166;
130 pt3.y = 86;
131 await ink.setPoint(2, 0, pt3);
132 pt3.x = 196;
133 pt3.y = 96;
134 await ink.setPoint(2, 1, pt3);
135 pt3.x = 221;
136 pt3.y = 121;
137 await ink.setPoint(2, 2, pt3);
138 pt3.x = 288;
139 pt3.y = 188;
140 await ink.setPoint(2, 3, pt3);
141 const cyanColorPt = await PDFNet.ColorPt.init(0, 1, 1);
142 await ink.setColor(cyanColorPt, 3);
143 firstPage.annotPushBack(ink);
144
145 await PDFNet.endDeallocateStack();
146 };
147
148 const AnnotationLowLevelAPI = async(doc) => {
149 try {
150 await PDFNet.startDeallocateStack(); // start stack-based deallocation. All objects will be deallocated by end of function
151 const itr = await doc.getPageIterator();
152 const page = await itr.current();
153
154 let annots = await page.getAnnots();
155
156 if (annots == null) {
157 // If there are no annotations, create a new annotation
158 // array for the page.
159 annots = await doc.createIndirectArray();
160 const sdfDoc = await page.getSDFObj();
161 await sdfDoc.put('Annots', annots);
162 }
163
164 // Create a Text annotation
165 const annot = await doc.createIndirectDict();
166 await annot.putName('Subtype', 'Text');
167 await annot.putBool('Open', true);
168 await annot.putString('Contents', 'The quick brown fox ate the lazy mouse.');
169 await annot.putRect('Rect', 266, 116, 430, 204);
170
171 // Insert the annotation in the page annotation array
172 await annots.pushBack(annot);
173
174 // Create a Link annotation
175 const link1 = await doc.createIndirectDict();
176 await link1.putName('Subtype', 'Link');
177 const dest = await PDFNet.Destination.createFit((await doc.getPage(2)));
178 await link1.put('Dest', (await dest.getSDFObj()));
179 await link1.putRect('Rect', 85, 705, 503, 661);
180 await annots.pushBack(link1);
181
182 // Create another Link annotation
183 const link2 = await doc.createIndirectDict();
184 await link2.putName('Subtype', 'Link');
185 const dest2 = await PDFNet.Destination.createFit((await doc.getPage(3)));
186 await link2.put('Dest', (await dest2.getSDFObj()));
187 await link2.putRect('Rect', 85, 638, 503, 594);
188 await annots.pushBack(link2);
189
190 // link2 = annots.GetAt(annots.Size()-1);
191 const tenthPage = await doc.getPage(10);
192 // XYZ destination stands for 'left', 'top' and 'zoom' coordinates
193 const XYZDestination = await PDFNet.Destination.createXYZ(tenthPage, 100, 722, 10);
194 await link2.put('Dest', (await XYZDestination.getSDFObj()));
195
196 // Create a third link annotation with a hyperlink action (all other
197 // annotation types can be created in a similar way)
198 const link3 = await doc.createIndirectDict();
199 await link3.putName('Subtype', 'Link');
200 await link3.putRect('Rect', 85, 570, 503, 524);
201
202 // Create a URI action
203 const action = await link3.putDict('A');
204 await action.putName('S', 'URI');
205 await action.putString('URI', 'http://www.pdftron.com');
206
207 await annots.pushBack(link3);
208 await PDFNet.endDeallocateStack();
209 } catch (err) {
210 console.log(err);
211 }
212 };
213
214 const CreateTestAnnots = async(doc) => {
215 await PDFNet.startDeallocateStack();
216 const ew = await PDFNet.ElementWriter.create(); // elementWriter
217 const eb = await PDFNet.ElementBuilder.create(); // elementBuilder
218 let element;
219
220 const firstPage = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
221 doc.pagePushBack(firstPage);
222 ew.beginOnPage(firstPage, PDFNet.ElementWriter.WriteMode.e_overlay, false); // begin writing to this page
223 ew.end(); // save changes to the current page
224
225 // NOTE: The following code represents three different ways to create a text annotation.
226 {
227 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(10, 400, 160, 570));
228 await txtannot.setContents('\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare.\n\nAha!\n\nAnd there was much rejoicing!');
229 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
230 await txtannot.setBorderStyle(solidLine, false);
231 await txtannot.setQuaddingFormat(0);
232 await firstPage.annotPushBack(txtannot);
233 await txtannot.refreshAppearance();
234 }
235
236 {
237 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(100, 100, 350, 500));
238 await txtannot.setContentRect(new PDFNet.Rect(200, 200, 350, 500));
239 await txtannot.setContents('\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare.\n\nAha!\n\nAnd there was much rejoicing!');
240 await txtannot.setCalloutLinePoints(new PDFNet.Point(200, 300), new PDFNet.Point(150, 290), new PDFNet.Point(110, 110));
241 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
242 await txtannot.setBorderStyle(solidLine, false);
243 await txtannot.setEndingStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
244 const greenColorPt = await PDFNet.ColorPt.init(0, 1, 0);
245 await txtannot.setColorDefault(greenColorPt); // default value of last param is 0
246 await txtannot.setQuaddingFormat(1);
247 await firstPage.annotPushBack(txtannot);
248 await txtannot.refreshAppearance();
249 }
250 {
251 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(400, 10, 550, 400));
252 await txtannot.setContents('\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare.\n\nAha!\n\nAnd there was much rejoicing!');
253 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
254 await txtannot.setBorderStyle(solidLine, false);
255 const redColorPt = await PDFNet.ColorPt.init(0, 0, 1);
256 await txtannot.setColorDefault(redColorPt);
257 await txtannot.setOpacity(0.2);
258 await txtannot.setQuaddingFormat(2);
259 await firstPage.annotPushBack(txtannot);
260 await txtannot.refreshAppearance();
261 }
262 const page = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
263 doc.pagePushBack(page);
264 await ew.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_overlay, false);
265 await eb.reset();
266 await ew.end(); // save changes to the current page
267 {
268 // Create a Line annotation...
269 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(250, 250, 400, 400));
270 await line.setStartPoint(new PDFNet.Point(350, 270));
271 await line.setEndPoint(new PDFNet.Point(260, 370));
272 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Square);
273 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
274 const darkGreenColorPt = await PDFNet.ColorPt.init(0.3, 0.5, 0);
275 await line.setColor(darkGreenColorPt, 3);
276 await line.setContents('Dashed Captioned');
277 await line.setShowCaption(true);
278 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
279 const dash = [2.0, 2.0];
280 const bStyle = await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 2, 0, 0, dash);
281 line.setBorderStyle(bStyle);
282 line.refreshAppearance();
283 page.annotPushBack(line);
284 }
285 {
286 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(347, 377, 600, 600));
287 await line.setStartPoint(new PDFNet.Point(385, 410));
288 await line.setEndPoint(new PDFNet.Point(540, 555));
289 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
290 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_OpenArrow);
291 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
292 await line.setColor(redColorPt, 3);
293 const greenColorPt = await PDFNet.ColorPt.init(0, 1, 0);
294 await line.setInteriorColor(greenColorPt, 3);
295 await line.setContents('Inline Caption');
296 await line.setShowCaption(true);
297 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Inline);
298 await line.setLeaderLineExtensionLength(-4.0);
299 await line.setLeaderLineLength(-12);
300 await line.setLeaderLineOffset(2.0);
301 await line.refreshAppearance();
302 page.annotPushBack(line);
303 }
304 {
305 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(10, 400, 200, 600));
306 await line.setStartPoint(new PDFNet.Point(25, 426));
307 await line.setEndPoint(new PDFNet.Point(180, 555));
308 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
309 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Square);
310 const blueColorPt = await PDFNet.ColorPt.init(0, 0, 1);
311 await line.setColor(blueColorPt, 3);
312 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
313 await line.setInteriorColor(redColorPt, 3);
314 await line.setContents('Offset Caption');
315 await line.setShowCaption(true);
316 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
317 await line.setTextHOffset(-60);
318 await line.setTextVOffset(10);
319 await line.refreshAppearance();
320 page.annotPushBack(line);
321 }
322 {
323 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(200, 10, 400, 70));
324 line.setStartPoint(new PDFNet.Point(220, 25));
325 line.setEndPoint(new PDFNet.Point(370, 60));
326 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Butt);
327 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_OpenArrow);
328 line.setColor((await PDFNet.ColorPt.init(0, 0, 1)), 3);
329 line.setContents('Regular Caption');
330 line.setShowCaption(true);
331 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
332 await line.refreshAppearance();
333 page.annotPushBack(line);
334 }
335 {
336 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(200, 70, 400, 130));
337 line.setStartPoint(new PDFNet.Point(220, 111));
338 line.setEndPoint(new PDFNet.Point(370, 78));
339 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
340 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Diamond);
341 line.setContents('Circle to Diamond');
342 line.setColor((await PDFNet.ColorPt.init(0, 0, 1)), 3);
343 line.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
344 line.setShowCaption(true);
345 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
346 line.refreshAppearance();
347 page.annotPushBack(line);
348 }
349 {
350 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(10, 100, 160, 200));
351 line.setStartPoint(new PDFNet.Point(15, 110));
352 line.setEndPoint(new PDFNet.Point(150, 190));
353 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Slash);
354 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
355 line.setContents('Slash to CArrow');
356 line.setColor((await PDFNet.ColorPt.init(1, 0, 0)), 3);
357 line.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 1)), 3);
358 line.setShowCaption(true);
359 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
360 line.refreshAppearance();
361 page.annotPushBack(line);
362 }
363 {
364 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(270, 270, 570, 433));
365 line.setStartPoint(new PDFNet.Point(300, 400));
366 line.setEndPoint(new PDFNet.Point(550, 300));
367 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_RClosedArrow);
368 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ROpenArrow);
369 line.setContents('ROpen & RClosed arrows');
370 line.setColor((await PDFNet.ColorPt.init(0, 0, 1)), 3);
371 line.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
372 line.setShowCaption(true);
373 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
374 line.refreshAppearance();
375 page.annotPushBack(line);
376 }
377 {
378 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(195, 395, 205, 505));
379 line.setStartPoint(new PDFNet.Point(200, 400));
380 line.setEndPoint(new PDFNet.Point(200, 500));
381 line.refreshAppearance();
382 page.annotPushBack(line);
383 }
384 {
385 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(55, 299, 150, 301));
386 line.setStartPoint(new PDFNet.Point(55, 300));
387 line.setEndPoint(new PDFNet.Point(155, 300));
388 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
389 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
390 line.setContents(("Caption that's longer than its line."));
391 line.setColor((await PDFNet.ColorPt.init(1, 0, 1)), 3);
392 line.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
393 line.setShowCaption(true);
394 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
395 line.refreshAppearance();
396 page.annotPushBack(line);
397 }
398 {
399 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(300, 200, 390, 234));
400 line.setStartPoint(new PDFNet.Point(310, 210));
401 line.setEndPoint(new PDFNet.Point(380, 220));
402 line.setColor((await PDFNet.ColorPt.init(0, 0, 0)), 3);
403 line.refreshAppearance();
404 page.annotPushBack(line);
405 }
406 const page3 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
407 ew.beginOnPage(page3); // begin writing to the page
408 ew.end(); // save changes to the current page
409 doc.pagePushBack(page3);
410 {
411 const circle = await PDFNet.CircleAnnot.create(doc, new PDFNet.Rect(300, 300, 390, 350));
412 circle.setColor((await PDFNet.ColorPt.init(0, 0, 0)), 3);
413 circle.refreshAppearance();
414 page3.annotPushBack(circle);
415 }
416 {
417 const circle = await PDFNet.CircleAnnot.create(doc, new PDFNet.Rect(100, 100, 200, 200));
418 circle.setColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
419 circle.setInteriorColor((await PDFNet.ColorPt.init(0, 0, 1)), 3);
420 const dash = [2, 4];
421 circle.setBorderStyle((await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 3, 0, 0, dash)));
422 circle.setPadding(new PDFNet.Rect(2, 2, 2, 2));
423 circle.refreshAppearance();
424 page3.annotPushBack(circle);
425 }
426 {
427 const sq = await PDFNet.SquareAnnot.create(doc, new PDFNet.Rect(10, 200, 80, 300));
428 sq.setColor((await PDFNet.ColorPt.init(0, 0, 0)), 3);
429 sq.refreshAppearance();
430 page3.annotPushBack(sq);
431 }
432
433 {
434 const sq = await PDFNet.SquareAnnot.create(doc, new PDFNet.Rect(500, 200, 580, 300));
435 sq.setColor((await PDFNet.ColorPt.init(1, 0, 0)), 3);
436 sq.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 1)), 3);
437 const dash = [4, 2];
438 sq.setBorderStyle((await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 6, 0, 0, dash)));
439 sq.setPadding(new PDFNet.Rect(4, 4, 4, 4));
440 sq.refreshAppearance();
441 page3.annotPushBack(sq);
442 }
443
444 {
445 const poly = await PDFNet.PolygonAnnot.create(doc, new PDFNet.Rect(5, 500, 125, 590));
446 poly.setColor((await PDFNet.ColorPt.init(1, 0, 0)), 3);
447 poly.setInteriorColor((await PDFNet.ColorPt.init(1, 1, 0)), 3);
448 poly.setVertex(0, new PDFNet.Point(12, 510));
449 poly.setVertex(1, new PDFNet.Point(100, 510));
450 poly.setVertex(2, new PDFNet.Point(100, 555));
451 poly.setVertex(3, new PDFNet.Point(35, 544));
452 const solidBorderStyle = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 4, 0, 0);
453 poly.setBorderStyle(solidBorderStyle);
454 poly.setPadding(new PDFNet.Rect(4, 4, 4, 4));
455 poly.refreshAppearance();
456 page3.annotPushBack(poly);
457 }
458 {
459 const poly = await PDFNet.PolyLineAnnot.create(doc, new PDFNet.Rect(400, 10, 500, 90));
460 poly.setColor((await PDFNet.ColorPt.init(1, 0, 0)), 3);
461 poly.setInteriorColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
462 poly.setVertex(0, new PDFNet.Point(405, 20));
463 poly.setVertex(1, new PDFNet.Point(440, 40));
464 poly.setVertex(2, new PDFNet.Point(410, 60));
465 poly.setVertex(3, new PDFNet.Point(470, 80));
466 poly.setBorderStyle(await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 2, 0, 0));
467 poly.setPadding(new PDFNet.Rect(4, 4, 4, 4));
468 poly.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_RClosedArrow);
469 poly.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
470 poly.refreshAppearance();
471 page3.annotPushBack(poly);
472 }
473 {
474 const lk = await PDFNet.LinkAnnot.create(doc, new PDFNet.Rect(5, 5, 55, 24));
475 // lk.setColor(await PDFNet.ColorPt.init(0,1,0), 3 );
476 lk.refreshAppearance();
477 page3.annotPushBack(lk);
478 }
479
480 const page4 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
481 ew.beginOnPage(page4); // begin writing to the page
482 ew.end(); // save changes to the current page
483 doc.pagePushBack(page4);
484
485 {
486 ew.beginOnPage(page4);
487 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_helvetica);
488 element = await eb.createTextBeginWithFont(font, 16);
489 element.setPathFill(true);
490 ew.writeElement(element);
491 element = await eb.createTextRun('Some random text on the page', font, 16);
492 element.setTextMatrixEntries(1, 0, 0, 1, 100, 500);
493 ew.writeElement(element);
494 ew.writeElement((await eb.createTextEnd()));
495 ew.end();
496 }
497 {
498 const hl = await PDFNet.HighlightAnnot.create(doc, new PDFNet.Rect(100, 490, 150, 515));
499 hl.setColor((await PDFNet.ColorPt.init(0, 1, 0)), 3);
500 hl.refreshAppearance();
501 page4.annotPushBack(hl);
502 }
503 {
504 const sq = await PDFNet.SquigglyAnnot.create(doc, new PDFNet.Rect(100, 450, 250, 600));
505 // sq.setColor(await PDFNet.ColorPt.init(1,0,0), 3 );
506 sq.setQuadPoint(0, new PDFNet.QuadPoint(122, 455, 240, 545, 230, 595, 101, 500));
507 sq.refreshAppearance();
508 page4.annotPushBack(sq);
509 }
510 {
511 const cr = await PDFNet.CaretAnnot.create(doc, new PDFNet.Rect(100, 40, 129, 69));
512 cr.setColor((await PDFNet.ColorPt.init(0, 0, 1)), 3);
513 cr.setSymbol('P');
514 cr.refreshAppearance();
515 page4.annotPushBack(cr);
516 }
517
518
519 const page5 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
520 ew.beginOnPage(page5); // begin writing to the page
521 ew.end(); // save changes to the current page
522 doc.pagePushBack(page5);
523 const fs = await PDFNet.FileSpec.create(doc, inputPath + 'butterfly.png', false);
524 const page6 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
525 ew.beginOnPage(page6); // begin writing to the page
526 ew.end(); // save changes to the current page
527 doc.pagePushBack(page6);
528
529 {
530 const txt = await PDFNet.TextAnnot.create(doc, new PDFNet.Rect(10, 20, 30, 40));
531 txt.setIconName('UserIcon');
532 txt.setContents('User defined icon, unrecognized by appearance generator');
533 txt.setColor((await PDFNet.ColorPt.init(0, 1, 0)));
534 txt.refreshAppearance();
535 page6.annotPushBack(txt);
536 }
537 {
538 const ink = await PDFNet.InkAnnot.create(doc, new PDFNet.Rect(100, 400, 200, 550));
539 ink.setColor((await PDFNet.ColorPt.init(0, 0, 1)));
540 ink.setPoint(1, 3, new PDFNet.Point(220, 505));
541 ink.setPoint(1, 0, new PDFNet.Point(100, 490));
542 ink.setPoint(0, 1, new PDFNet.Point(120, 410));
543 ink.setPoint(0, 0, new PDFNet.Point(100, 400));
544 ink.setPoint(1, 2, new PDFNet.Point(180, 490));
545 ink.setPoint(1, 1, new PDFNet.Point(140, 440));
546 ink.setBorderStyle(await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 3, 0, 0));
547 ink.refreshAppearance();
548 page6.annotPushBack(ink);
549 }
550
551
552 const page7 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
553 ew.beginOnPage(page7); // begin writing to the page
554 ew.end(); // save changes to the current page
555 doc.pagePushBack(page7);
556
557 {
558 const snd = await PDFNet.SoundAnnot.create(doc, new PDFNet.Rect(100, 500, 120, 520));
559 snd.setColor((await PDFNet.ColorPt.init(1, 1, 0)));
560 snd.setIcon(PDFNet.SoundAnnot.Icon.e_Speaker);
561 snd.refreshAppearance();
562 page7.annotPushBack(snd);
563 }
564 {
565 const snd = await PDFNet.SoundAnnot.create(doc, new PDFNet.Rect(200, 500, 220, 520));
566 snd.setColor((await PDFNet.ColorPt.init(1, 1, 0)));
567 snd.setIcon(PDFNet.SoundAnnot.Icon.e_Mic);
568 snd.refreshAppearance();
569 page7.annotPushBack(snd);
570 }
571
572 const page8 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
573 ew.beginOnPage(page8); // begin writing to the page
574 ew.end(); // save changes to the current page
575 doc.pagePushBack(page8);
576
577 for (let ipage = 0; ipage < 2; ++ipage) {
578 let px = 5;
579 let py = 520;
580 for (let istamp = PDFNet.RubberStampAnnot.Icon.e_Approved; istamp <= PDFNet.RubberStampAnnot.Icon.e_Draft; istamp++) {
581 const st = await PDFNet.RubberStampAnnot.create(doc, new PDFNet.Rect(1, 1, 100, 100));
582 st.setIcon(istamp);
583 st.setContents((await st.getIconName()));
584 st.setRect(new PDFNet.Rect(px, py, px + 100, py + 25));
585 py -= 100;
586 if (py < 0) {
587 py = 520;
588 px += 200;
589 }
590 if (ipage === 0) {
591 // page7.annotPushBack( st );
592 } else {
593 page8.annotPushBack(st);
594 st.refreshAppearance();
595 }
596 }
597 }
598 const st = await PDFNet.RubberStampAnnot.create(doc, new PDFNet.Rect(400, 5, 550, 45));
599 st.setIconName('UserStamp');
600 st.setContents('User defined stamp');
601 page8.annotPushBack(st);
602 st.refreshAppearance();
603
604 await PDFNet.endDeallocateStack();
605 };
606
607 const main = async() => {
608 try {
609 const ret = 0;
610
611 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'numbered.pdf');
612 doc.initSecurityHandler();
613
614
615 await AnnotationLowLevelAPI(doc);
616 await doc.save(inputPath + 'Output/annotation_test1.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
617
618 console.log('Done. Results saved in annotation_test1.pdf');
619
620 // eslint-disable-next-line no-unused-vars
621 const firstPage = await doc.getPage(1);
622
623 await AnnotationHighLevelAPI(doc);
624 await doc.save(inputPath + 'Output/annotation_test2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
625 console.log('Done. Results saved in annotation_test2.pdf');
626
627 // creating various annotations in a brand new document
628 const docnew = await PDFNet.PDFDoc.create();
629 await CreateTestAnnots(docnew);
630 await docnew.save(inputPath + 'Output/new_annot_test_api.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
631 console.log('Saved new_annot_test_api.pdf');
632 return ret;
633 } catch (err) {
634 console.log(err);
635 }
636 };
637 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
638 };
639 exports.runAnnotationTest();
640})(exports);
641// eslint-disable-next-line spaced-comment
642//# sourceURL=AnnotationTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14function AnnotationHighLevelAPI($doc) {
15 // The following code snippet traverses all annotations in the document
16 echo nl2br("Traversing all annotations in the document...\n");
17 $page_num = 1;
18
19 for ( $itr = $doc->GetPageIterator(); $itr->HasNext(); $itr->Next() ) {
20 echo nl2br("Page ".$page_num++.": \n");
21
22 $page = $itr->Current();
23 $num_annots = $page->GetNumAnnots();
24
25 for ($i=0; $i<$num_annots; ++$i)
26 {
27 $annot = $page->GetAnnot($i);
28 if (!$annot->IsValid()) continue;
29 echo nl2br("Annot Type: ".$annot->GetSDFObj()->Get("Subtype")->Value()->GetName()."\n");
30
31 $bbox = $annot->GetRect();
32 echo nl2br(" Position: ".$bbox->x1.", ".$bbox->y1.", ".$bbox->x2.", ".$bbox->y2."\n");
33
34 switch ($annot->GetType())
35 {
36 case Annot::e_Link:
37 {
38 $link = new Link($annot);
39 $action = $link->GetAction();
40 if (!$action->IsValid()) continue 2;
41 if ($action->GetType() == Action::e_GoTo)
42 {
43 $dest = $action->GetDest();
44 if (!$dest->IsValid()) {
45 echo nl2br(" Destination is not valid\n");
46 }
47 else {
48 $page_n = $dest->GetPage()->GetIndex();
49 echo nl2br(" Links to: page number ".$page_n." in this document\n");
50 }
51 }
52 else if ($action->GetType() == Action::e_URI)
53 {
54 $uri = $action->GetSDFObj()->Get("URI")->Value()->GetAsPDFText();
55 echo nl2br(" Links to: ".$uri."\n");
56 }
57 // ...
58 }
59 break;
60 case Annot::e_Widget:
61 break;
62 case Annot::e_FileAttachment:
63 break;
64 // ...
65 default:
66 break;
67 }
68 }
69 }
70
71 // Use the high-level API to create new annotations.
72 $first_page = $doc->GetPage(1);
73
74 // Create a hyperlink...
75 $hyperlink = Link::CreateAnnot($doc->GetSDFDoc(), new Rect(85.0, 570.0, 503.0, 524.0), Action::CreateURI($doc->GetSDFDoc(), "http://www.pdftron.com"));
76 $first_page->AnnotPushBack($hyperlink);
77
78 // Create an intra-document link...
79 $goto_page_3 = Action::CreateGoto(Destination::CreateFitH($doc->GetPage(3), 0));
80 $link = Link::CreateAnnot($doc->GetSDFDoc(), new Rect(85.0, 458.0, 503.0, 502.0), $goto_page_3);
81 $link->SetColor(new ColorPt(0.0, 0.0, 1.0));
82
83 // Add the new annotation to the first page
84 $first_page->AnnotPushBack($link);
85
86 // Create a stamp annotation ...
87 $stamp = RubberStamp::CreateAnnot($doc->GetSDFDoc(), new Rect(30.0, 30.0, 300.0, 200.0));
88 $stamp->SetIcon("Draft");
89 $first_page->AnnotPushBack($stamp);
90
91 // Create a file attachment annotation (embed the 'peppers.jpg').
92 global $input_path;
93 $file_attach = FileAttachment::CreateAnnot($doc->GetSDFDoc(), new Rect(80.0, 280.0, 108.0, 320.0), ($input_path."peppers.jpg"));
94 $first_page->AnnotPushBack($file_attach);
95
96 $ink = Ink::CreateAnnot($doc->GetSDFDoc(), new Rect(110.0, 10.0, 300.0, 200.0));
97 $pt3 = new Point(110.0, 10.0);
98 $ink->SetPoint(0, 0, $pt3);
99 $pt3->x = 150; $pt3->y = 50;
100 $ink->SetPoint(0, 1, $pt3);
101 $pt3->x = 190; $pt3->y = 60;
102 $ink->SetPoint(0, 2, $pt3);
103 $pt3->x = 180; $pt3->y = 90;
104 $ink->SetPoint(1, 0, $pt3);
105 $pt3->x = 190; $pt3->y = 95;
106 $ink->SetPoint(1, 1, $pt3);
107 $pt3->x = 200; $pt3->y = 100;
108 $ink->SetPoint(1, 2, $pt3);
109 $pt3->x = 166; $pt3->y = 86;
110 $ink->SetPoint(2, 0, $pt3);
111 $pt3->x = 196; $pt3->y = 96;
112 $ink->SetPoint(2, 1, $pt3);
113 $pt3->x = 221; $pt3->y = 121;
114 $ink->SetPoint(2, 2, $pt3);
115 $pt3->x = 288; $pt3->y = 188;
116 $ink->SetPoint(2, 3, $pt3);
117 $ink->SetColor(new ColorPt(0.0, 1.0, 1.0), 3);
118 $first_page->AnnotPushBack($ink);
119
120
121}
122
123function AnnotationLowLevelAPI($doc) {
124 $ir = $doc->GetPageIterator();
125 $page = $ir->Current();
126 $annots = $page->GetAnnots();
127
128 if (!$annots)
129 {
130 // If there are no annotations, create a new annotation
131 // array for the page.
132 $annots = $doc->CreateIndirectArray();
133 $page->GetSDFObj()->Put("Annots", $annots);
134 }
135
136 // Create a Text annotation
137 $annot = $doc->CreateIndirectDict();
138 $annot->PutName("Subtype", "Text");
139 $annot->PutBool("Open", true);
140 $annot->PutString("Contents", "The quick brown fox ate the lazy mouse.");
141 $annot->PutRect("Rect", 266, 116, 430, 204);
142
143 // Insert the annotation in the page annotation array
144 $annots->PushBack($annot);
145
146 // Create a Link annotation
147 $link1 = $doc->CreateIndirectDict();
148 $link1->PutName("Subtype", "Link");
149 $dest = Destination::CreateFit($doc->GetPage(2));
150 $link1->Put("Dest", $dest->GetSDFObj());
151 $link1->PutRect("Rect", 85, 705, 503, 661);
152 $annots->PushBack($link1);
153
154 // Create another Link annotation
155 $link2 = $doc->CreateIndirectDict();
156 $link2->PutName("Subtype", "Link");
157 $dest2 = Destination::CreateFit($doc->GetPage(3));
158 $link2->Put("Dest", $dest2->GetSDFObj());
159 $link2->PutRect("Rect", 85, 638, 503, 594);
160 $annots->PushBack($link2);
161
162 // Note that PDFNet API can be used to modify existing annotations.
163 // In the following example we will modify the second link annotation
164 // (link2) so that it points to the 10th page. We also use a different
165 // destination page fit type.
166
167 // $link2 = $annots->GetAt($annots->Size()-1);
168 $link2->Put("Dest", Destination::CreateXYZ($doc->GetPage(10), 100, 792-70, 10)->GetSDFObj());
169
170 // Create a third link annotation with a hyperlink action (all other
171 // annotation types can be created in a similar way)
172 $link3 = $doc->CreateIndirectDict();
173 $link3->PutName("Subtype", "Link");
174 $link3->PutRect("Rect", 85, 570, 503, 524);
175
176 // Create a URI action
177 $action = $link3->PutDict("A");
178 $action->PutName("S", "URI");
179 $action->PutString("URI", "http://www.pdftron.com");
180
181 $annots->PushBack($link3);
182}
183
184function CreateTestAnnots($doc) {
185 $ew = new ElementWriter();
186 $eb = new ElementBuilder();
187
188 $first_page= $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
189 $doc->PagePushBack($first_page);
190 $ew->Begin($first_page, ElementWriter::e_overlay, false ); // begin writing to this page
191 $ew->End(); // save changes to the current page
192
193 //
194 // Test of a free text annotation.
195 //
196 $txtannot = FreeText::CreateAnnot( $doc->GetSDFDoc(), new Rect(10.0, 400.0, 160.0, 570.0) );
197 $txtannot->SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
198 ."\n\nAha!\n\nAnd there was much rejoicing!");
199 $txtannot->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 1.0, 10.0, 20.0 ), false );
200 $txtannot->SetQuaddingFormat(0);
201 $first_page->AnnotPushBack($txtannot);
202 $txtannot->RefreshAppearance();
203
204 $txtannot = FreeText::CreateAnnot( $doc->GetSDFDoc(), new Rect(100.0, 100.0, 350.0, 500.0) );
205 $txtannot->SetContentRect( new Rect( 200.0, 200.0, 350.0, 500.0 ) );
206 $txtannot->SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
207 ."\n\nAha!\n\nAnd there was much rejoicing!");
208 $txtannot->SetCalloutLinePoints( new Point(200.0,300.0), new Point(150.0,290.0), new Point(110.0,110.0) );
209 $txtannot->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 1.0, 10.0, 20.0 ), false );
210 $txtannot->SetEndingStyle( LineAnnot::e_ClosedArrow );
211 $txtannot->SetColor( new ColorPt( 0.0, 1.0, 0.0 ) );
212 $txtannot->SetQuaddingFormat(1);
213 $first_page->AnnotPushBack($txtannot);
214 $txtannot->RefreshAppearance();
215
216 $txtannot = FreeText::CreateAnnot( $doc->GetSDFDoc(), new Rect(400.0, 10.0, 550.0, 400.0) );
217 $txtannot->SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare."
218 ."\n\nAha!\n\nAnd there was much rejoicing!");
219 $txtannot->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 1.0, 10.0, 20.0 ), false );
220 $txtannot->SetColor( new ColorPt( 0.0, 0.0, 1.0 ) );
221 $txtannot->SetOpacity( 0.2 );
222 $txtannot->SetQuaddingFormat(2);
223 $first_page->AnnotPushBack($txtannot);
224 $txtannot->RefreshAppearance();
225
226 $page = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
227 $doc->PagePushBack($page);
228 $ew->Begin($page, ElementWriter::e_overlay, false ); // begin writing to this page
229 $eb->Reset(); // Reset the GState to default
230 $ew->End(); // save changes to the current page
231
232 //Create a Line annotation...
233 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(250.0, 250.0, 400.0, 400.0));
234 $line->SetStartPoint( new Point(350.0, 270.0) );
235 $line->SetEndPoint( new Point(260.0,370.0) );
236 $line->SetStartStyle(LineAnnot::e_Square);
237 $line->SetEndStyle(LineAnnot::e_Circle);
238 $line->SetColor(new ColorPt(0.3, 0.5, 0.0), 3);
239 $line->SetContents( "Dashed Captioned" );
240 $line->SetShowCaption(true);
241 $line->SetCaptionPosition( LineAnnot::e_Top );
242 $line->SetBorderStyle(new BorderStyle(BorderStyle::e_dashed, 2.0, 0.0, 0.0, array(2.0, 2.0)));
243 $line->RefreshAppearance();
244 $page->AnnotPushBack($line);
245
246 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(347.0, 377.0, 600.0, 600.0));
247 $line->SetStartPoint( new Point(385.0, 410.0) );
248 $line->SetEndPoint( new Point(540.0,555.0) );
249 $line->SetStartStyle(LineAnnot::e_Circle);
250 $line->SetEndStyle(LineAnnot::e_OpenArrow);
251 $line->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
252 $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3);
253 $line->SetContents("Inline Caption");
254 $line->SetShowCaption(true);
255 $line->SetCaptionPosition( LineAnnot::e_Inline );
256 $line->SetLeaderLineExtensionLength( 4.0 );
257 $line->SetLeaderLineLength( -12.0 );
258 $line->SetLeaderLineOffset( 2.0 );
259 $line->RefreshAppearance();
260 $page->AnnotPushBack($line);
261
262 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(10.0, 400.0, 200.0, 600.0));
263 $line->SetStartPoint( new Point(25.0, 426.0) );
264 $line->SetEndPoint( new Point(180.0,555.0) );
265 $line->SetStartStyle(LineAnnot::e_Circle);
266 $line->SetEndStyle(LineAnnot::e_Square);
267 $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3);
268 $line->SetInteriorColor(new ColorPt(1.0, 0.0, 0.0), 3);
269 $line->SetContents("Offset Caption");
270 $line->SetShowCaption(true);
271 $line->SetCaptionPosition( LineAnnot::e_Top );
272 $line->SetTextHOffset( -60 );
273 $line->SetTextVOffset( 10 );
274 $line->RefreshAppearance();
275 $page->AnnotPushBack($line);
276
277 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(200.0, 10.0, 400.0, 70.0));
278 $line->SetStartPoint( new Point(220.0, 25.0) );
279 $line->SetEndPoint( new Point(370.0,60.0) );
280 $line->SetStartStyle(LineAnnot::e_Butt);
281 $line->SetEndStyle(LineAnnot::e_OpenArrow);
282 $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3);
283 $line->SetContents("Regular Caption");
284 $line->SetShowCaption(true);
285 $line->SetCaptionPosition( LineAnnot::e_Top );
286 $line->RefreshAppearance();
287 $page->AnnotPushBack($line);
288
289 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(200.0, 70.0, 400.0, 130.0));
290 $line->SetStartPoint( new Point(220.0, 111.0) );
291 $line->SetEndPoint( new Point(370.0,78.0) );
292 $line->SetStartStyle(LineAnnot::e_Circle);
293 $line->SetEndStyle(LineAnnot::e_Diamond);
294 $line->SetContents("Circle to Diamond");
295 $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3);
296 $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3);
297 $line->SetShowCaption(true);
298 $line->SetCaptionPosition( LineAnnot::e_Top );
299 $line->RefreshAppearance();
300 $page->AnnotPushBack($line);
301
302 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect(10.0, 100.0, 160.0, 200.0));
303 $line->SetStartPoint( new Point(15.0, 110.0) );
304 $line->SetEndPoint( new Point(150.0, 190.0) );
305 $line->SetStartStyle(LineAnnot::e_Slash);
306 $line->SetEndStyle(LineAnnot::e_ClosedArrow);
307 $line->SetContents("Slash to CArrow");
308 $line->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
309 $line->SetInteriorColor(new ColorPt(0.0, 1.0, 1.0), 3);
310 $line->SetShowCaption(true);
311 $line->SetCaptionPosition( LineAnnot::e_Top );
312 $line->RefreshAppearance();
313 $page->AnnotPushBack($line);
314
315 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect( 270.0, 270.0, 570.0, 433.0 ));
316 $line->SetStartPoint( new Point(300.0, 400.0 ) );
317 $line->SetEndPoint( new Point(550.0, 300.0) );
318 $line->SetStartStyle(LineAnnot::e_RClosedArrow);
319 $line->SetEndStyle(LineAnnot::e_ROpenArrow);
320 $line->SetContents("ROpen & RClosed arrows");
321 $line->SetColor(new ColorPt(0.0, 0.0, 1.0), 3);
322 $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3);
323 $line->SetShowCaption(true);
324 $line->SetCaptionPosition( LineAnnot::e_Top );
325 $line->RefreshAppearance();
326 $page->AnnotPushBack($line);
327
328 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect( 195.0, 395.0, 205.0, 505.0 ));
329 $line->SetStartPoint( new Point(200.0, 400.0 ) );
330 $line->SetEndPoint( new Point(200.0, 500.0) );
331 $line->RefreshAppearance();
332 $page->AnnotPushBack($line);
333
334 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect( 55.0, 299.0, 150.0, 301.0 ));
335 $line->SetStartPoint( new Point(55.0, 300.0 ) );
336 $line->SetEndPoint( new Point(155.0, 300.0) );
337 $line->SetStartStyle(LineAnnot::e_Circle);
338 $line->SetEndStyle(LineAnnot::e_Circle);
339 $line->SetContents("Caption that's longer than its line.");
340 $line->SetColor(new ColorPt(1.0, 0.0, 1.0), 3);
341 $line->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3);
342 $line->SetShowCaption(true);
343 $line->SetCaptionPosition( LineAnnot::e_Top );
344 $line->RefreshAppearance();
345 $page->AnnotPushBack($line);
346
347 $line = LineAnnot::CreateAnnot($doc->GetSDFDoc(), new Rect( 300.0, 200.0, 390.0, 234.0 ));
348 $line->SetStartPoint( new Point(310.0, 210.0 ) );
349 $line->SetEndPoint( new Point(380.0, 220.0) );
350 $line->SetColor(new ColorPt(0.0, 0.0, 0.0), 3);
351 $line->RefreshAppearance();
352 $page->AnnotPushBack($line);
353
354 $page3 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
355 $ew->Begin($page3); // begin writing to the page
356 $ew->End(); // save changes to the current page
357 $doc->PagePushBack($page3);
358
359 $circle = Circle::CreateAnnot($doc->GetSDFDoc(), new Rect( 300.0, 300.0, 390.0, 350.0 ));
360 $circle->SetColor(new ColorPt(0.0, 0.0, 0.0), 3);
361 $circle->RefreshAppearance();
362 $page3->AnnotPushBack($circle);
363
364 $circle = Circle::CreateAnnot($doc->GetSDFDoc(), new Rect( 100.0, 100.0, 200.0, 200.0 ));
365 $circle->SetColor(new ColorPt(0.0, 1.0, 0.0), 3);
366 $circle->SetInteriorColor(new ColorPt(0.0, 0.0, 1.0), 3);
367 $circle->SetBorderStyle( new BorderStyle( BorderStyle::e_dashed, 3.0, 0.0, 0.0, array(2.0, 4.0)) );
368 $circle->SetPadding( 2.0 );
369 $circle->RefreshAppearance();
370 $page3->AnnotPushBack($circle);
371
372 $sq = Square::CreateAnnot( $doc->GetSDFDoc(), new Rect(10.0,200.0, 80.0, 300.0 ) );
373 $sq->SetColor(new ColorPt(0.0, 0.0, 0.0), 3);
374 $sq->RefreshAppearance();
375 $page3->AnnotPushBack( $sq );
376
377 $sq = Square::CreateAnnot( $doc->GetSDFDoc(), new Rect(500.0,200.0, 580.0, 300.0 ) );
378 $sq->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
379 $sq->SetInteriorColor(new ColorPt(0.0, 1.0, 1.0), 3);
380 $sq->SetBorderStyle( new BorderStyle( BorderStyle::e_dashed, 6.0, 0.0, 0.0, array(4.0, 2.0) ) );
381 $sq->SetPadding( 4.0 );
382 $sq->RefreshAppearance();
383 $page3->AnnotPushBack( $sq );
384
385 $poly = Polygon::CreateAnnot($doc->GetSDFDoc(), new Rect(5.0, 500.0, 125.0, 590.0));
386 $poly->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
387 $poly->SetInteriorColor(new ColorPt(1.0, 1.0, 0.0), 3);
388 $poly->SetVertex(0, new Point(12.0,510.0) );
389 $poly->SetVertex(1, new Point(100.0,510.0) );
390 $poly->SetVertex(2, new Point(100.0,555.0) );
391 $poly->SetVertex(3, new Point(35.0,544.0) );
392 $poly->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 4.0, 0.0, 0.0 ) );
393 $poly->SetPadding( 4.0 );
394 $poly->RefreshAppearance();
395 $page3->AnnotPushBack( $poly );
396
397 $poly = PolyLine::CreateAnnot($doc->GetSDFDoc(), new Rect(400.0, 10.0, 500.0, 90.0));
398 $poly->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
399 $poly->SetInteriorColor(new ColorPt(0.0, 1.0, 0.0), 3);
400 $poly->SetVertex(0, new Point(405.0,20.0) );
401 $poly->SetVertex(1, new Point(440.0,40.0) );
402 $poly->SetVertex(2, new Point(410.0,60.0) );
403 $poly->SetVertex(3, new Point(470.0,80.0) );
404 $poly->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 2.0, 0.0, 0.0 ) );
405 $poly->SetPadding( 4.0 );
406 $poly->SetStartStyle( LineAnnot::e_RClosedArrow );
407 $poly->SetEndStyle( LineAnnot::e_ClosedArrow );
408 $poly->RefreshAppearance();
409 $page3->AnnotPushBack( $poly );
410
411 $lk = Link::CreateAnnot( $doc->GetSDFDoc(), new Rect(5.0,5.0,55.0,24.0) );
412 //$lk->SetColor( new ColorPt(0.0,1.0,0.0), 3.0 );
413 $lk->RefreshAppearance();
414 $page3->AnnotPushBack( $lk );
415
416 $page4 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
417 $ew->Begin($page4); // begin writing to the page
418 $ew->End(); // save changes to the current page
419 $doc->PagePushBack($page4);
420
421 $ew->Begin( $page4 );
422 $font = Font::Create($doc->GetSDFDoc(), Font::e_helvetica);
423 $element = $eb->CreateTextBegin( $font, 16.0 );
424 $element->SetPathFill(true);
425 $ew->WriteElement($element);
426 $element = $eb->CreateTextRun( "Some random text on the page", $font, 16.0 );
427 $element->SetTextMatrix(1.0, 0.0, 0.0, 1.0, 100.0, 500.0 );
428 $ew->WriteElement($element);
429 $ew->WriteElement( $eb->CreateTextEnd() );
430 $ew->End();
431
432 $hl = HighlightAnnot::CreateAnnot( $doc->GetSDFDoc(), new Rect(100.0,490.0,150.0,515.0) );
433 $hl->SetColor( new ColorPt(0.0,1.0,0.0), 3 );
434 $hl->RefreshAppearance();
435 $page4->AnnotPushBack( $hl );
436
437 $sq = Squiggly::CreateAnnot( $doc->GetSDFDoc(), new Rect(100.0,450.0,250.0,600.0) );
438 $sq->SetQuadPoint( 0, new QuadPoint( new Point( 122.0,455.0), new Point(240.0, 545.0), new Point(230.0, 595.0), new Point(101.0,500.0 ) ) );
439 $sq->RefreshAppearance();
440 $page4->AnnotPushBack( $sq );
441
442 $cr = Caret::CreateAnnot( $doc->GetSDFDoc(), new Rect(100.0,40.0,129.0,69.0) );
443 $cr->SetColor( new ColorPt(0.0,0.0,1.0), 3 );
444 $cr->SetSymbol( "P" );
445 $cr->RefreshAppearance();
446 $page4->AnnotPushBack( $cr );
447
448 $page5 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
449 $ew->Begin($page5); // begin writing to the page
450 $ew->End(); // save changes to the current page
451 $doc->PagePushBack($page5);
452 global $input_path;
453 $fs = FileSpec::Create( $doc->GetSDFDoc(), $input_path."butterfly.png", false );
454 $page6 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
455 $ew->Begin($page6); // begin writing to the page
456 $ew->End(); // save changes to the current page
457 $doc->PagePushBack($page6);
458
459
460 $txt = Text::CreateAnnot( $doc->GetSDFDoc(), new Point(10.0, 20.0) );
461 $txt->SetIcon( "UserIcon" );
462 $txt->SetContents( "User defined icon, unrecognized by appearance generator" );
463 $txt->SetColor( new ColorPt(0.0,1.0,0.0) );
464 $txt->RefreshAppearance();
465 $page6->AnnotPushBack( $txt );
466
467 $ink = Ink::CreateAnnot( $doc->GetSDFDoc(), new Rect( 100.0, 400.0, 200.0, 550.0 ) );
468 $ink->SetColor( new ColorPt(0.0,0.0,1.0) );
469 $ink->SetPoint( 1, 3, new Point( 220.0, 505.0) );
470 $ink->SetPoint( 1, 0, new Point( 100.0, 490.0) );
471 $ink->SetPoint( 0, 1, new Point( 120.0, 410.0) );
472 $ink->SetPoint( 0, 0, new Point( 100.0, 400.0) );
473 $ink->SetPoint( 1, 2, new Point( 180.0, 490.0) );
474 $ink->SetPoint( 1, 1, new Point( 140.0, 440.0) );
475 $ink->SetBorderStyle( new BorderStyle( BorderStyle::e_solid, 3.0, 0.0, 0.0 ) );
476 $ink->RefreshAppearance();
477 $page6->AnnotPushBack( $ink );
478
479 $page7 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
480 $ew->Begin($page7); // begin writing to the page
481 $ew->End(); // save changes to the current page
482 $doc->PagePushBack($page7);
483
484 $snd = Sound::CreateAnnot( $doc->GetSDFDoc(), new Rect( 100.0, 500.0, 120.0, 520.0 ) );
485 $snd->SetColor( new ColorPt(1.0,1.0,0.0) );
486 $snd->SetIcon( Sound::e_Speaker );
487 $snd->RefreshAppearance();
488 $page7->AnnotPushBack( $snd );
489
490 $snd = Sound::CreateAnnot( $doc->GetSDFDoc(), new Rect( 200.0, 500.0, 220.0, 520.0 ) );
491 $snd->SetColor( new ColorPt(1.0,1.0,0.0) );
492 $snd->SetIcon( Sound::e_Mic );
493 $snd->RefreshAppearance();
494 $page7->AnnotPushBack( $snd );
495
496 $page8 = $doc->PageCreate(new Rect(0.0, 0.0, 600.0, 600.0));
497 $ew->Begin($page8); // begin writing to the page
498 $ew->End(); // save changes to the current page
499 $doc->PagePushBack($page8);
500
501 for( $ipage =0; $ipage < 2; ++$ipage ) {
502 $px = 5;
503 $py = 520;
504 for( $istamp = RubberStamp::e_Approved; $istamp <= RubberStamp::e_Draft; $istamp = $istamp + 1 ) {
505 $st = RubberStamp::CreateAnnot( $doc->GetSDFDoc(), new Rect(1.0,1.0,100.0,100.0) );
506 $st->SetIcon( $istamp );
507 $st->SetContents( $st->GetIconName() );
508 $st->SetRect( new Rect((double)$px, (double)$py, (double)$px+100.0, (double)$py+25.0 ) );
509 $py -= 100;
510 if( $py < 0 ) {
511 $py = 520;
512 $px += 200;
513 }
514 if( $ipage == 0 ) {
515 //$page7->AnnotPushBack( $st );
516 }
517 else {
518 $page8->AnnotPushBack( $st );
519 $st->RefreshAppearance();
520 }
521 }
522 }
523
524 $st = RubberStamp::CreateAnnot( $doc->GetSDFDoc(), new Rect(400.0,5.0,550.0,45.0) );
525 $st->SetIcon( "UserStamp" );
526 $st->SetContents( "User defined stamp" );
527 $page8->AnnotPushBack( $st );
528 $st->RefreshAppearance();
529}
530
531 PDFNet::Initialize($LicenseKey);
532 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
533
534 $doc = new PDFDoc($input_path."numbered.pdf");
535 $doc->InitSecurityHandler();
536
537 // An example of using SDF/Cos API to add any type of annotations.
538 AnnotationLowLevelAPI($doc);
539 $doc->Save($output_path."annotation_test1.pdf", SDFDoc::e_remove_unused);
540 echo nl2br("Done. Results saved in annotation_test1.pdf\n");
541
542 // An example of using the high-level PDFNet API to read existing annotations,
543 // to edit existing annotations, and to create new annotation from scratch.
544 AnnotationHighLevelAPI($doc);
545 $doc->Save($output_path."annotation_test2.pdf", SDFDoc::e_linearized);
546 echo nl2br("Done. Results saved in annotation_test2.pdf\n");
547
548 // an example of creating various annotations in a brand new document
549 $doc1 = new PDFDoc();
550 CreateTestAnnots( $doc1 );
551 $outfname = $output_path."new_annot_test_api.pdf";
552 $doc1->Save($outfname, SDFDoc::e_linearized);
553 echo nl2br("Saved new_annot_test_api.pdf");
554
555 $doc->Close();
556 PDFNet::Terminate();
557?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14def AnnotationHighLevelAPI(doc):
15 # The following code snippet traverses all annotations in the document
16 print("Traversing all annotations in the document...")
17 page_num = 1
18 itr = doc.GetPageIterator()
19
20 while itr.HasNext():
21 print("Page " + str(page_num) + ": ")
22 page_num = page_num + 1
23 page = itr.Current()
24 num_annots = page.GetNumAnnots()
25 i = 0
26 while i < num_annots:
27 annot = page.GetAnnot(i)
28 if not annot.IsValid():
29 continue
30 print("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName())
31
32 bbox = annot.GetRect()
33 formatter = '{0:g}'
34 print(" Position: " + formatter.format(bbox.x1) +
35 ", " + formatter.format(bbox.y1) +
36 ", " + formatter.format(bbox.x2) +
37 ", " + formatter.format(bbox.y2))
38
39 type = annot.GetType()
40
41 if type == Annot.e_Link:
42 link = Link(annot)
43 action = link.GetAction()
44 if not action.IsValid():
45 continue
46 if action.GetType() == Action.e_GoTo:
47 dest = action.GetDest()
48 if not dest.IsValid():
49 print(" Destination is not valid.")
50 else:
51 page_n = dest.GetPage().GetIndex()
52 print(" Links to: page number " + str(page_n) + " in this document")
53 elif action.GetType() == Action.e_URI:
54 uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText()
55 print(" Links to: " + str(uri))
56 elif type == Annot.e_Widget:
57 pass
58 elif type == Annot.e_FileAttachment:
59 pass
60 i = i + 1
61 itr.Next()
62
63 # Use the high-level API to create new annotations.
64 first_page = doc.GetPage(1)
65
66 # Create a hyperlink...
67 hyperlink = Link.Create(doc.GetSDFDoc(), Rect(85, 570, 503, 524), Action.CreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"))
68 first_page.AnnotPushBack(hyperlink)
69
70 # Create an intra-document link...
71 goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0))
72 link = Link.Create(doc.GetSDFDoc(), Rect(85, 458, 503, 502), goto_page_3)
73 link.SetColor(ColorPt(0, 0, 1))
74
75 # Add the new annotation to the first page
76 first_page.AnnotPushBack(link)
77
78 # Create a stamp annotation ...
79 stamp = RubberStamp.Create(doc.GetSDFDoc(), Rect(30, 30, 300, 200))
80 stamp.SetIcon("Draft")
81 first_page.AnnotPushBack(stamp)
82
83 # Create a file attachment annotation (embed the 'peppers.jpg').
84 file_attach = FileAttachment.Create(doc.GetSDFDoc(), Rect(80, 280, 108, 320), (input_path + "peppers.jpg"))
85 first_page.AnnotPushBack(file_attach)
86
87
88 ink = Ink.Create(doc.GetSDFDoc(), Rect(110, 10, 300, 200))
89 pt3 = Point(110, 10)
90 pt3.x = 110
91 pt3.y = 10
92 ink.SetPoint(0, 0, pt3)
93 pt3.x = 150
94 pt3.y = 50
95 ink.SetPoint(0, 1, pt3)
96 pt3.x = 190
97 pt3.y = 60
98 ink.SetPoint(0, 2, pt3)
99 pt3.x = 180
100 pt3.y = 90
101 ink.SetPoint(1, 0, pt3)
102 pt3.x = 190
103 pt3.y = 95
104 ink.SetPoint(1, 1, pt3)
105 pt3.x = 200
106 pt3.y = 100
107 ink.SetPoint(1, 2, pt3)
108 pt3.x = 166
109 pt3.y = 86
110 ink.SetPoint(2, 0, pt3)
111 pt3.x = 196
112 pt3.y = 96
113 ink.SetPoint(2, 1, pt3)
114 pt3.x = 221
115 pt3.y = 121
116 ink.SetPoint(2, 2, pt3)
117 pt3.x = 288
118 pt3.y = 188
119 ink.SetPoint(2, 3, pt3)
120 ink.SetColor(ColorPt(0, 1, 1), 3)
121 first_page.AnnotPushBack(ink)
122
123def AnnotationLowLevelAPI(doc):
124 itr = doc.GetPageIterator()
125 page = itr.Current()
126 annots = page.GetAnnots()
127
128 if annots == None:
129 # If there are no annotations, create a new annotation
130 # array for the page.
131 annots = doc.CreateIndirectArray()
132 page.GetSDFObj().Put("Annots", annots)
133
134 # Create a Text annotation
135 annot = doc.CreateIndirectDict()
136 annot.PutName("Subtype", "Text")
137 annot.PutBool("Open", True)
138 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
139 annot.PutRect("Rect", 266, 116, 430, 204)
140
141 # Insert the annotation in the page annotation array
142 annots.PushBack(annot)
143
144 # Create a Link annotation
145 link1 = doc.CreateIndirectDict()
146 link1.PutName("Subtype", "Link")
147 dest = Destination.CreateFit(doc.GetPage(2))
148 link1.Put("Dest", dest.GetSDFObj())
149 link1.PutRect("Rect", 85, 705, 503, 661)
150 annots.PushBack(link1)
151
152 # Create another Link annotation
153 link2 = doc.CreateIndirectDict()
154 link2.PutName("Subtype", "Link")
155 dest2 = Destination.CreateFit((doc.GetPage(3)))
156 link2.Put("Dest", dest2.GetSDFObj())
157 link2.PutRect("Rect", 85, 638, 503, 594)
158 annots.PushBack(link2)
159
160 # Note that PDFNet APi can be used to modify existing annotations.
161 # In the following example we will modify the second link annotation
162 # (link2) so that it points to the 10th page. We also use a different
163 # destination page fit type.
164
165 # link2 = annots.GetAt(annots.Size()-1)
166 link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj())
167
168 # Create a third link annotation with a hyperlink action (all other
169 # annotation types can be created in a similar way)
170 link3 = doc.CreateIndirectDict()
171 link3.PutName("Subtype", "Link")
172 link3.PutRect("Rect", 85, 570, 503, 524)
173
174 # Create a URI action
175 action = link3.PutDict("A")
176 action.PutName("S", "URI")
177 action.PutString("URI", "http://www.pdftron.com")
178
179 annots.PushBack(link3)
180
181def CreateTestAnnots(doc):
182 ew = ElementWriter()
183 eb = ElementBuilder()
184
185 first_page = doc.PageCreate(Rect(0, 0, 600, 600))
186 doc.PagePushBack(first_page)
187 ew.Begin(first_page, ElementWriter.e_overlay, False ) # begin writing to this page
188 ew.End() # save changes to the current page
189
190 # Test of a free text annotation.
191 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(10, 400, 160, 570) )
192 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out " +
193 "of the air by freezing it with an angry glare." +
194 "\n\nAha!\n\nAnd there was much rejoicing!" )
195 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
196 txtannot.SetQuaddingFormat(0)
197 first_page.AnnotPushBack(txtannot)
198 txtannot.RefreshAppearance()
199
200 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(100, 100, 350, 500) )
201 txtannot.SetContentRect( Rect( 200, 200, 350, 500 ) )
202 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air "
203 "by freezing it with an angry glare."
204 "\n\nAha!\n\nAnd there was much rejoicing!" )
205 txtannot.SetCalloutLinePoints( Point(200,300), Point(150,290), Point(110,110) )
206 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
207 txtannot.SetEndingStyle( LineAnnot.e_ClosedArrow )
208 txtannot.SetColor( ColorPt( 0, 1, 0 ) )
209 txtannot.SetQuaddingFormat(1)
210 first_page.AnnotPushBack(txtannot)
211 txtannot.RefreshAppearance()
212
213 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(400, 10, 550, 400) )
214 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air "
215 "by freezing it with an angry glare."
216 "\n\nAha!\n\nAnd there was much rejoicing!" )
217 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
218 txtannot.SetColor( ColorPt( 0, 0, 1 ) )
219 txtannot.SetOpacity( 0.2 )
220 txtannot.SetQuaddingFormat(2)
221 first_page.AnnotPushBack(txtannot)
222 txtannot.RefreshAppearance()
223
224 page= doc.PageCreate(Rect(0, 0, 600, 600))
225 doc.PagePushBack(page)
226 ew.Begin(page, ElementWriter.e_overlay, False ) # begin writing to this page
227 eb.Reset() # Reset the GState to default
228 ew.End() # save changes to the current page
229
230 # Create a Line annotation...
231 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(250, 250, 400, 400))
232 line.SetStartPoint( Point(350, 270 ) )
233 line.SetEndPoint( Point(260,370) )
234 line.SetStartStyle(LineAnnot.e_Square)
235 line.SetEndStyle(LineAnnot.e_Circle)
236 line.SetColor(ColorPt(.3, .5, 0), 3)
237 line.SetContents( "Dashed Captioned" )
238 line.SetShowCaption(True)
239 line.SetCaptionPosition( LineAnnot.e_Top )
240 line.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 2, 0, 0, [2.0, 2.0] ) )
241 line.RefreshAppearance()
242 page.AnnotPushBack(line)
243
244 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(347, 377, 600, 600))
245 line.SetStartPoint( Point(385, 410 ) )
246 line.SetEndPoint( Point(540,555) )
247 line.SetStartStyle(LineAnnot.e_Circle)
248 line.SetEndStyle(LineAnnot.e_OpenArrow)
249 line.SetColor(ColorPt(1, 0, 0), 3)
250 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
251 line.SetContents( "Inline Caption" )
252 line.SetShowCaption(True)
253 line.SetCaptionPosition( LineAnnot.e_Inline )
254 line.SetLeaderLineExtensionLength( 4. )
255 line.SetLeaderLineLength( -12. )
256 line.SetLeaderLineOffset( 2. )
257 line.RefreshAppearance()
258 page.AnnotPushBack(line)
259
260 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(10, 400, 200, 600))
261 line.SetStartPoint( Point(25, 426 ) )
262 line.SetEndPoint( Point(180,555) )
263 line.SetStartStyle(LineAnnot.e_Circle)
264 line.SetEndStyle(LineAnnot.e_Square)
265 line.SetColor(ColorPt(0, 0, 1), 3)
266 line.SetInteriorColor(ColorPt(1, 0, 0), 3)
267 line.SetContents("Offset Caption")
268 line.SetShowCaption(True)
269 line.SetCaptionPosition( LineAnnot.e_Top )
270 line.SetTextHOffset( -60 )
271 line.SetTextVOffset( 10 )
272 line.RefreshAppearance()
273 page.AnnotPushBack(line)
274
275 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(200, 10, 400, 70))
276 line.SetStartPoint( Point(220, 25 ) )
277 line.SetEndPoint( Point(370,60) )
278 line.SetStartStyle(LineAnnot.e_Butt)
279 line.SetEndStyle(LineAnnot.e_OpenArrow)
280 line.SetColor(ColorPt(0, 0, 1), 3)
281 line.SetContents( "Regular Caption" )
282 line.SetShowCaption(True)
283 line.SetCaptionPosition( LineAnnot.e_Top )
284 line.RefreshAppearance()
285 page.AnnotPushBack(line)
286
287 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(200, 70, 400, 130))
288 line.SetStartPoint( Point(220, 111 ) )
289 line.SetEndPoint( Point(370,78) )
290 line.SetStartStyle(LineAnnot.e_Circle)
291 line.SetEndStyle(LineAnnot.e_Diamond)
292 line.SetContents( "Circle to Diamond" )
293 line.SetColor(ColorPt(0, 0, 1), 3)
294 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
295 line.SetShowCaption(True)
296 line.SetCaptionPosition( LineAnnot.e_Top )
297 line.RefreshAppearance()
298 page.AnnotPushBack(line)
299
300 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(10, 100, 160, 200))
301 line.SetStartPoint( Point(15, 110 ) )
302 line.SetEndPoint( Point(150, 190) )
303 line.SetStartStyle(LineAnnot.e_Slash)
304 line.SetEndStyle(LineAnnot.e_ClosedArrow)
305 line.SetContents( "Slash to CArrow" )
306 line.SetColor(ColorPt(1, 0, 0), 3)
307 line.SetInteriorColor(ColorPt(0, 1, 1), 3)
308 line.SetShowCaption(True)
309 line.SetCaptionPosition( LineAnnot.e_Top )
310 line.RefreshAppearance()
311 page.AnnotPushBack(line)
312
313 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 270, 270, 570, 433 ))
314 line.SetStartPoint( Point(300, 400 ) )
315 line.SetEndPoint( Point(550, 300) )
316 line.SetStartStyle(LineAnnot.e_RClosedArrow)
317 line.SetEndStyle(LineAnnot.e_ROpenArrow)
318 line.SetContents( "ROpen & RClosed arrows" )
319 line.SetColor(ColorPt(0, 0, 1), 3)
320 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
321 line.SetShowCaption(True)
322 line.SetCaptionPosition( LineAnnot.e_Top )
323 line.RefreshAppearance()
324 page.AnnotPushBack(line)
325
326 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 195, 395, 205, 505 ))
327 line.SetStartPoint( Point(200, 400 ) )
328 line.SetEndPoint( Point(200, 500) )
329 line.RefreshAppearance()
330 page.AnnotPushBack(line)
331
332 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 55, 299, 150, 301 ))
333 line.SetStartPoint( Point(55, 300 ) )
334 line.SetEndPoint( Point(155, 300) )
335 line.SetStartStyle(LineAnnot.e_Circle)
336 line.SetEndStyle(LineAnnot.e_Circle)
337 line.SetContents( "Caption that's longer than its line." )
338 line.SetColor(ColorPt(1, 0, 1), 3)
339 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
340 line.SetShowCaption(True)
341 line.SetCaptionPosition( LineAnnot.e_Top )
342 line.RefreshAppearance()
343 page.AnnotPushBack(line)
344
345 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 300, 200, 390, 234 ))
346 line.SetStartPoint( Point(310, 210 ) )
347 line.SetEndPoint( Point(380, 220) )
348 line.SetColor(ColorPt(0, 0, 0), 3)
349 line.RefreshAppearance()
350 page.AnnotPushBack(line)
351
352 page3 = doc.PageCreate(Rect(0, 0, 600, 600))
353 ew.Begin(page3) # begin writing to the page
354 ew.End() # save changes to the current page
355 doc.PagePushBack(page3)
356
357 circle=Circle.Create(doc.GetSDFDoc(), Rect( 300, 300, 390, 350 ))
358 circle.SetColor(ColorPt(0, 0, 0), 3)
359 circle.RefreshAppearance()
360 page3.AnnotPushBack(circle)
361
362 circle=Circle.Create(doc.GetSDFDoc(), Rect( 100, 100, 200, 200 ))
363 circle.SetColor(ColorPt(0, 1, 0), 3)
364 circle.SetInteriorColor(ColorPt(0, 0, 1), 3)
365 circle.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 3, 0, 0, [2, 4] ) )
366 circle.SetPadding( 2 )
367 circle.RefreshAppearance()
368 page3.AnnotPushBack(circle)
369
370 sq = Square.Create( doc.GetSDFDoc(), Rect(10,200, 80, 300 ) )
371 sq.SetColor(ColorPt(0, 0, 0), 3)
372 sq.RefreshAppearance()
373 page3.AnnotPushBack( sq )
374
375 sq = Square.Create( doc.GetSDFDoc(), Rect(500,200, 580, 300 ) )
376 sq.SetColor(ColorPt(1, 0, 0), 3)
377 sq.SetInteriorColor(ColorPt(0, 1, 1), 3)
378 sq.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 6, 0, 0, [4, 2] ) )
379 sq.SetPadding( 4 )
380 sq.RefreshAppearance()
381 page3.AnnotPushBack( sq )
382
383 poly = Polygon.Create(doc.GetSDFDoc(), Rect(5, 500, 125, 590))
384 poly.SetColor(ColorPt(1, 0, 0), 3)
385 poly.SetInteriorColor(ColorPt(1, 1, 0), 3)
386 poly.SetVertex(0, Point(12,510) )
387 poly.SetVertex(1, Point(100,510) )
388 poly.SetVertex(2, Point(100,555) )
389 poly.SetVertex(3, Point(35,544) )
390 poly.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 4, 0, 0 ) )
391 poly.SetPadding( 4 )
392 poly.RefreshAppearance()
393 page3.AnnotPushBack( poly )
394
395 poly = PolyLine.Create(doc.GetSDFDoc(), Rect(400, 10, 500, 90))
396 poly.SetColor(ColorPt(1, 0, 0), 3)
397 poly.SetInteriorColor(ColorPt(0, 1, 0), 3)
398 poly.SetVertex(0, Point(405,20) )
399 poly.SetVertex(1, Point(440,40) )
400 poly.SetVertex(2, Point(410,60) )
401 poly.SetVertex(3, Point(470,80) )
402 poly.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 2, 0, 0 ) )
403 poly.SetPadding( 4 )
404 poly.SetStartStyle( LineAnnot.e_RClosedArrow )
405 poly.SetEndStyle( LineAnnot.e_ClosedArrow )
406 poly.RefreshAppearance()
407 page3.AnnotPushBack( poly )
408
409 lk = Link.Create( doc.GetSDFDoc(), Rect(5,5,55,24) )
410 lk.RefreshAppearance()
411 page3.AnnotPushBack( lk )
412
413 page4 = doc.PageCreate(Rect(0, 0, 600, 600))
414 ew.Begin(page4) # begin writing to the page
415 ew.End() # save changes to the current page
416 doc.PagePushBack(page4)
417
418 ew.Begin( page4 )
419 font = Font.Create(doc.GetSDFDoc(), Font.e_helvetica)
420 element = eb.CreateTextBegin( font, 16 )
421 element.SetPathFill(True)
422 ew.WriteElement(element)
423 element = eb.CreateTextRun( "Some random text on the page", font, 16 )
424 element.SetTextMatrix(1, 0, 0, 1, 100, 500 )
425 ew.WriteElement(element)
426 ew.WriteElement( eb.CreateTextEnd() )
427 ew.End()
428
429 hl = HighlightAnnot.Create( doc.GetSDFDoc(), Rect(100,490,150,515) )
430 hl.SetColor( ColorPt(0,1,0), 3 )
431 hl.RefreshAppearance()
432 page4.AnnotPushBack( hl )
433
434 sq = Squiggly.Create( doc.GetSDFDoc(), Rect(100,450,250,600) )
435 sq.SetQuadPoint( 0, QuadPoint( Point( 122,455), Point(240, 545), Point(230, 595), Point(101,500 ) ) )
436 sq.RefreshAppearance()
437 page4.AnnotPushBack( sq )
438
439 cr = Caret.Create( doc.GetSDFDoc(), Rect(100,40,129,69) )
440 cr.SetColor( ColorPt(0,0,1), 3 )
441 cr.SetSymbol( "P" )
442 cr.RefreshAppearance()
443 page4.AnnotPushBack( cr )
444
445 page5 = doc.PageCreate(Rect(0, 0, 600, 600))
446 ew.Begin(page5) # begin writing to the page
447 ew.End() # save changes to the current page
448 doc.PagePushBack(page5)
449 fs = FileSpec.Create( doc.GetSDFDoc(), (input_path + "butterfly.png"), False )
450 page6 = doc.PageCreate(Rect(0, 0, 600, 600))
451 ew.Begin(page6) # begin writing to the page
452 ew.End() # save changes to the current page
453 doc.PagePushBack(page6)
454
455
456 txt = Text.Create( doc.GetSDFDoc(), Point(10, 20) )
457 txt.SetIcon( "UserIcon" )
458 txt.SetContents( "User defined icon, unrecognized by appearance generator" )
459 txt.SetColor( ColorPt(0,1,0) )
460 txt.RefreshAppearance()
461 page6.AnnotPushBack( txt )
462
463 ink = Ink.Create( doc.GetSDFDoc(), Rect( 100, 400, 200, 550 ) )
464 ink.SetColor( ColorPt(0,0,1) )
465 ink.SetPoint( 1, 3, Point( 220, 505) )
466 ink.SetPoint( 1, 0, Point( 100, 490) )
467 ink.SetPoint( 0, 1, Point( 120, 410) )
468 ink.SetPoint( 0, 0, Point( 100, 400) )
469 ink.SetPoint( 1, 2, Point( 180, 490) )
470 ink.SetPoint( 1, 1, Point( 140, 440) )
471 ink.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 3, 0, 0 ) )
472 ink.RefreshAppearance()
473 page6.AnnotPushBack( ink )
474
475 page7 = doc.PageCreate(Rect(0, 0, 600, 600))
476 ew.Begin(page7) # begin writing to the page
477 ew.End() # save changes to the current page
478 doc.PagePushBack(page7)
479
480 snd = Sound.Create( doc.GetSDFDoc(), Rect( 100, 500, 120, 520 ) )
481 snd.SetColor( ColorPt(1,1,0) )
482 snd.SetIcon( Sound.e_Speaker )
483 snd.RefreshAppearance()
484 page7.AnnotPushBack( snd )
485
486 snd = Sound.Create( doc.GetSDFDoc(), Rect( 200, 500, 220, 520 ) )
487 snd.SetColor( ColorPt(1,1,0) )
488 snd.SetIcon( Sound.e_Mic )
489 snd.RefreshAppearance()
490 page7.AnnotPushBack( snd )
491
492 page8 = doc.PageCreate(Rect(0, 0, 600, 600))
493 ew.Begin(page8) # begin writing to the page
494 ew.End() # save changes to the current page
495 doc.PagePushBack(page8)
496
497 ipage = 0
498 while ipage<2:
499 px = 5
500 py = 520
501 istamp = RubberStamp.e_Approved
502 while istamp <= RubberStamp.e_Draft:
503 st = RubberStamp.Create(doc.GetSDFDoc(), Rect(1,1,100,100))
504 st.SetIcon( istamp )
505 st.SetContents( st.GetIconName() )
506 st.SetRect( Rect(px, py, px+100, py+25 ) )
507 py -= 100
508 if py < 0:
509 py = 520
510 px+=200
511 if ipage == 0:
512 #page7.AnnotPushBack(st)
513 pass
514 else:
515 page8.AnnotPushBack( st )
516 st.RefreshAppearance()
517 istamp = istamp + 1
518 ipage = ipage + 1
519
520 st = RubberStamp.Create( doc.GetSDFDoc(), Rect(400,5,550,45) )
521 st.SetIcon( "UserStamp" )
522 st.SetContents( "User defined stamp" )
523 page8.AnnotPushBack( st )
524 st.RefreshAppearance()
525
526if __name__ == '__main__':
527 PDFNet.Initialize(LicenseKey)
528
529 output_path = "../../TestFiles/Output/"
530 input_path = "../../TestFiles/"
531
532 doc = PDFDoc(input_path + "numbered.pdf")
533 doc.InitSecurityHandler()
534
535 # An example of using SDF/Cos API to add any type of annotations.
536 AnnotationLowLevelAPI(doc)
537 doc.Save(output_path + "annotation_test1.pdf", SDFDoc.e_remove_unused)
538 print("Done. Results saved in annotation_test1.pdf")
539
540 # An example of using the high-level PDFNet API to read existing annotations,
541 # to edit existing annotations, and to create new annotation from scratch.
542 AnnotationHighLevelAPI(doc)
543 doc.Save((output_path + "annotation_test2.pdf"), SDFDoc.e_linearized)
544 doc.Close()
545 print("Done. Results saved in annotation_test2.pdf")
546
547 doc1 = PDFDoc()
548 CreateTestAnnots(doc1)
549 outfname = output_path + "new_annot_test_api.pdf"
550 doc1.Save(outfname, SDFDoc.e_linearized)
551 print("Saved new_annot_test_api.pdf")
552 PDFNet.Terminate()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12$output_path = "../../TestFiles/Output/"
13$input_path = "../../TestFiles/"
14
15def FloatToStr(float)
16 if float.to_i() == float.to_f()
17 return float.to_i().to_s()
18 else
19 return float.to_f().to_s()
20 end
21end
22
23def AnnotationHighLevelAPI(doc)
24 # The following code snippet traverses all annotations in the document
25 puts "Traversing all annotations in the document..."
26 page_num = 1
27 itr = doc.GetPageIterator()
28
29 while itr.HasNext() do
30 puts "Page " + page_num.to_s() + ": "
31 page_num = page_num + 1
32 page = itr.Current()
33 num_annots = page.GetNumAnnots()
34 i = 0
35 while i < num_annots do
36 annot = page.GetAnnot(i)
37 if !(annot.IsValid())
38 i = i + 1
39 next
40 end
41 puts "Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName()
42
43 bbox = annot.GetRect()
44 puts " Position: " + FloatToStr(bbox.x1.to_s()) +
45 ", " + FloatToStr(bbox.y1.to_s()) +
46 ", " + FloatToStr(bbox.x2.to_s()) +
47 ", " + FloatToStr(bbox.y2.to_s())
48
49 type = annot.GetType()
50 case type
51 when Annot::E_Link
52 link = Link.new(annot)
53 action = link.GetAction()
54 if !(action.IsValid())
55 i = i + 1
56 next
57 end
58 if action.GetType() == Action::E_GoTo
59 dest = action.GetDest()
60 if !(dest.IsValid())
61 puts " Destination is not valid."
62 else
63 page_n = dest.GetPage().GetIndex()
64 puts " Links to: page number " + page_n.to_s() + " in this document"
65 end
66 elsif action.GetType() == Action::E_URI
67 uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText()
68 puts " Links to: " + uri.to_s()
69 end
70 when Annot::E_Widget
71 when Annot::E_FileAttachment
72 end
73 i = i + 1
74 end
75 itr.Next()
76 end
77
78 # Use the high-level API to create new annotations.
79 first_page = doc.GetPage(1)
80
81 # Create a hyperlink...
82 hyperlink = Link.Create(doc.GetSDFDoc(), Rect.new(85, 570, 503, 524), Action.CreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"))
83 first_page.AnnotPushBack(hyperlink)
84
85 # Create an intra-document link...
86 goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0))
87 link = Link.Create(doc.GetSDFDoc(), Rect.new(85, 458, 503, 502), goto_page_3)
88 link.SetColor(ColorPt.new(0, 0, 1))
89
90 # Add the new annotation to the first page
91 first_page.AnnotPushBack(link)
92
93 # Create a stamp annotation ...
94 stamp = RubberStamp.Create(doc.GetSDFDoc(), Rect.new(30, 30, 300, 200))
95 stamp.SetIcon("Draft")
96 first_page.AnnotPushBack(stamp)
97
98 # Create a file attachment annotation (embed the 'peppers.jpg').
99 file_attach = FileAttachment.Create(doc.GetSDFDoc(), Rect.new(80, 280, 108, 320), $input_path + "peppers.jpg")
100 first_page.AnnotPushBack(file_attach)
101
102 ink = Ink.Create(doc.GetSDFDoc(), Rect.new(110, 10, 300, 200))
103 pt3 = Point.new(110, 10)
104 pt3.x = 110
105 pt3.y = 10
106 ink.SetPoint(0, 0, pt3)
107 pt3.x = 150
108 pt3.y = 50
109 ink.SetPoint(0, 1, pt3)
110 pt3.x = 190
111 pt3.y = 60
112 ink.SetPoint(0, 2, pt3)
113 pt3.x = 180
114 pt3.y = 90
115 ink.SetPoint(1, 0, pt3)
116 pt3.x = 190
117 pt3.y = 95
118 ink.SetPoint(1, 1, pt3)
119 pt3.x = 200
120 pt3.y = 100
121 ink.SetPoint(1, 2, pt3)
122 pt3.x = 166
123 pt3.y = 86
124 ink.SetPoint(2, 0, pt3)
125 pt3.x = 196
126 pt3.y = 96
127 ink.SetPoint(2, 1, pt3)
128 pt3.x = 221
129 pt3.y = 121
130 ink.SetPoint(2, 2, pt3)
131 pt3.x = 288
132 pt3.y = 188
133 ink.SetPoint(2, 3, pt3)
134 ink.SetColor(ColorPt.new(0, 1, 1), 3)
135 first_page.AnnotPushBack(ink)
136end
137
138def AnnotationLowLevelAPI(doc)
139 itr = doc.GetPageIterator()
140 page = itr.Current()
141 annots = page.GetAnnots()
142
143 if annots.nil?
144 # If there are no annotations, create a new annotation
145 # array for the page.
146 annots = doc.CreateIndirectArray()
147 page.GetSDFObj().Put("Annots", annots)
148 end
149
150 # Create a Text annotation
151 annot = doc.CreateIndirectDict()
152 annot.PutName("Subtype", "Text")
153 annot.PutBool("Open", true)
154 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
155 annot.PutRect("Rect", 266, 116, 430, 204)
156
157 # Insert the annotation in the page annotation array
158 annots.PushBack(annot)
159
160 # Create a Link annotation
161 link1 = doc.CreateIndirectDict()
162 link1.PutName("Subtype", "Link")
163 dest = Destination.CreateFit(doc.GetPage(2))
164 link1.Put("Dest", dest.GetSDFObj())
165 link1.PutRect("Rect", 85, 705, 503, 661)
166 annots.PushBack(link1)
167
168 # Create another Link annotation
169 link2 = doc.CreateIndirectDict()
170 link2.PutName("Subtype", "Link")
171 dest2 = Destination.CreateFit((doc.GetPage(3)))
172 link2.Put("Dest", dest2.GetSDFObj())
173 link2.PutRect("Rect", 85, 638, 503, 594)
174 annots.PushBack(link2)
175
176 # Note that PDFNet API can be used to modify existing annotations.
177 # In the following example we will modify the second link annotation
178 # (link2) so that it points to the 10th page. We also use a different
179 # destination page fit type.
180
181 # link2 = annots.GetAt(annots.Size()-1)
182 link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj())
183
184 # Create a third link annotation with a hyperlink action (all other
185 # annotation types can be created in a similar way)
186 link3 = doc.CreateIndirectDict()
187 link3.PutName("Subtype", "Link")
188 link3.PutRect("Rect", 85, 570, 503, 524)
189
190 # Create a URI action
191 action = link3.PutDict("A")
192 action.PutName("S", "URI")
193 action.PutString("URI", "http://www.pdftron.com")
194
195 annots.PushBack(link3)
196end
197
198def CreateTestAnnots(doc)
199 ew = ElementWriter.new()
200 eb = ElementBuilder.new()
201
202 first_page = doc.PageCreate(Rect.new(0, 0, 600, 600))
203 doc.PagePushBack(first_page)
204 ew.Begin(first_page, ElementWriter::E_overlay, false ) # begin writing to this page
205 ew.End() # save changes to the current page
206
207 # Test of a free text annotation.
208 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect.new(10, 400, 160, 570) )
209 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out " +
210 "of the air by freezing it with an angry glare." +
211 "\n\nAha!\n\nAnd there was much rejoicing!" )
212 txtannot.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 1, 10, 20 ), false )
213 txtannot.SetQuaddingFormat(0)
214 first_page.AnnotPushBack(txtannot)
215 txtannot.RefreshAppearance()
216
217 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect.new(100, 100, 350, 500) )
218 txtannot.SetContentRect( Rect.new( 200, 200, 350, 500 ) )
219 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air " +
220 "by freezing it with an angry glare." +
221 "\n\nAha!\n\nAnd there was much rejoicing!")
222 txtannot.SetCalloutLinePoints( Point.new(200,300), Point.new(150,290), Point.new(110,110) )
223 txtannot.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 1, 10, 20 ), false )
224 txtannot.SetEndingStyle( LineAnnot::E_ClosedArrow )
225 txtannot.SetColor( ColorPt.new( 0, 1, 0 ) )
226 txtannot.SetQuaddingFormat(1)
227 first_page.AnnotPushBack(txtannot)
228 txtannot.RefreshAppearance()
229
230 txtannot = FreeText.Create(doc.GetSDFDoc(), Rect.new(400, 10, 550, 400))
231 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air " +
232 "by freezing it with an angry glare." +
233 "\n\nAha!\n\nAnd there was much rejoicing!")
234 txtannot.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 1, 10, 20 ), false )
235 txtannot.SetColor( ColorPt.new( 0, 0, 1 ) )
236 txtannot.SetOpacity( 0.2 )
237 txtannot.SetQuaddingFormat(2)
238 first_page.AnnotPushBack(txtannot)
239 txtannot.RefreshAppearance()
240
241 page= doc.PageCreate(Rect.new(0, 0, 600, 600))
242 doc.PagePushBack(page)
243 ew.Begin(page, ElementWriter::E_overlay, false ) # begin writing to this page
244 eb.Reset() # Reset the GState to default
245 ew.End() # save changes to the current page
246
247 # Create a Line annotation...
248 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(250, 250, 400, 400))
249 line.SetStartPoint( Point.new(350, 270 ) )
250 line.SetEndPoint( Point.new(260,370) )
251 line.SetStartStyle(LineAnnot::E_Square)
252 line.SetEndStyle(LineAnnot::E_Circle)
253 line.SetColor(ColorPt.new(0.3, 0.5, 0), 3)
254 line.SetContents( "Dashed Captioned" )
255 line.SetShowCaption(true)
256 line.SetCaptionPosition( LineAnnot::E_Top )
257 line.SetBorderStyle( BorderStyle.new( BorderStyle::E_dashed, 2, 0, 0, [2.0, 2.0] ) )
258 line.RefreshAppearance()
259 page.AnnotPushBack(line)
260
261 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(347, 377, 600, 600))
262 line.SetStartPoint( Point.new(385, 410 ) )
263 line.SetEndPoint( Point.new(540,555) )
264 line.SetStartStyle(LineAnnot::E_Circle)
265 line.SetEndStyle(LineAnnot::E_OpenArrow)
266 line.SetColor(ColorPt.new(1, 0, 0), 3)
267 line.SetInteriorColor(ColorPt.new(0, 1, 0), 3)
268 line.SetContents( "Inline Caption" )
269 line.SetShowCaption(true)
270 line.SetCaptionPosition( LineAnnot::E_Inline )
271 line.SetLeaderLineExtensionLength( 4 )
272 line.SetLeaderLineLength( -12 )
273 line.SetLeaderLineOffset( 2 )
274 line.RefreshAppearance()
275 page.AnnotPushBack(line)
276
277 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(10, 400, 200, 600))
278 line.SetStartPoint( Point.new(25, 426 ) )
279 line.SetEndPoint( Point.new(180,555) )
280 line.SetStartStyle(LineAnnot::E_Circle)
281 line.SetEndStyle(LineAnnot::E_Square)
282 line.SetColor(ColorPt.new(0, 0, 1), 3)
283 line.SetInteriorColor(ColorPt.new(1, 0, 0), 3)
284 line.SetContents("Offset Caption")
285 line.SetShowCaption(true)
286 line.SetCaptionPosition( LineAnnot::E_Top )
287 line.SetTextHOffset( -60 )
288 line.SetTextVOffset( 10 )
289 line.RefreshAppearance()
290 page.AnnotPushBack(line)
291
292 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(200, 10, 400, 70))
293 line.SetStartPoint( Point.new(220, 25 ) )
294 line.SetEndPoint( Point.new(370,60) )
295 line.SetStartStyle(LineAnnot::E_Butt)
296 line.SetEndStyle(LineAnnot::E_OpenArrow)
297 line.SetColor(ColorPt.new(0, 0, 1), 3)
298 line.SetContents( "Regular Caption" )
299 line.SetShowCaption(true)
300 line.SetCaptionPosition( LineAnnot::E_Top )
301 line.RefreshAppearance()
302 page.AnnotPushBack(line)
303
304 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(200, 70, 400, 130))
305 line.SetStartPoint( Point.new(220, 111 ) )
306 line.SetEndPoint( Point.new(370,78) )
307 line.SetStartStyle(LineAnnot::E_Circle)
308 line.SetEndStyle(LineAnnot::E_Diamond)
309 line.SetContents( "Circle to Diamond" )
310 line.SetColor(ColorPt.new(0, 0, 1), 3)
311 line.SetInteriorColor(ColorPt.new(0, 1, 0), 3)
312 line.SetShowCaption(true)
313 line.SetCaptionPosition( LineAnnot::E_Top )
314 line.RefreshAppearance()
315 page.AnnotPushBack(line)
316
317 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new(10, 100, 160, 200))
318 line.SetStartPoint( Point.new(15, 110 ) )
319 line.SetEndPoint( Point.new(150, 190) )
320 line.SetStartStyle(LineAnnot::E_Slash)
321 line.SetEndStyle(LineAnnot::E_ClosedArrow)
322 line.SetContents( "Slash to CArrow" )
323 line.SetColor(ColorPt.new(1, 0, 0), 3)
324 line.SetInteriorColor(ColorPt.new(0, 1, 1), 3)
325 line.SetShowCaption(true)
326 line.SetCaptionPosition( LineAnnot::E_Top )
327 line.RefreshAppearance()
328 page.AnnotPushBack(line)
329
330 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new( 270, 270, 570, 433 ))
331 line.SetStartPoint( Point.new(300, 400 ) )
332 line.SetEndPoint( Point.new(550, 300) )
333 line.SetStartStyle(LineAnnot::E_RClosedArrow)
334 line.SetEndStyle(LineAnnot::E_ROpenArrow)
335 line.SetContents( "ROpen & RClosed arrows" )
336 line.SetColor(ColorPt.new(0, 0, 1), 3)
337 line.SetInteriorColor(ColorPt.new(0, 1, 0), 3)
338 line.SetShowCaption(true)
339 line.SetCaptionPosition( LineAnnot::E_Top )
340 line.RefreshAppearance()
341 page.AnnotPushBack(line)
342
343 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new( 195, 395, 205, 505 ))
344 line.SetStartPoint( Point.new(200, 400 ) )
345 line.SetEndPoint( Point.new(200, 500) )
346 line.RefreshAppearance()
347 page.AnnotPushBack(line)
348
349 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new( 55, 299, 150, 301 ))
350 line.SetStartPoint( Point.new(55, 300 ) )
351 line.SetEndPoint( Point.new(155, 300) )
352 line.SetStartStyle(LineAnnot::E_Circle)
353 line.SetEndStyle(LineAnnot::E_Circle)
354 line.SetContents( "Caption that's longer than its line." )
355 line.SetColor(ColorPt.new(1, 0, 1), 3)
356 line.SetInteriorColor(ColorPt.new(0, 1, 0), 3)
357 line.SetShowCaption(true)
358 line.SetCaptionPosition( LineAnnot::E_Top )
359 line.RefreshAppearance()
360 page.AnnotPushBack(line)
361
362 line=LineAnnot.Create(doc.GetSDFDoc(), Rect.new( 300, 200, 390, 234 ))
363 line.SetStartPoint( Point.new(310, 210 ) )
364 line.SetEndPoint( Point.new(380, 220) )
365 line.SetColor(ColorPt.new(0, 0, 0), 3)
366 line.RefreshAppearance()
367 page.AnnotPushBack(line)
368
369 page3 = doc.PageCreate(Rect.new(0, 0, 600, 600))
370 ew.Begin(page3) # begin writing to the page
371 ew.End() # save changes to the current page
372 doc.PagePushBack(page3)
373
374 circle=Circle.Create(doc.GetSDFDoc(), Rect.new( 300, 300, 390, 350 ))
375 circle.SetColor(ColorPt.new(0, 0, 0), 3)
376 circle.RefreshAppearance()
377 page3.AnnotPushBack(circle)
378
379 circle=Circle.Create(doc.GetSDFDoc(), Rect.new( 100, 100, 200, 200 ))
380 circle.SetColor(ColorPt.new(0, 1, 0), 3)
381 circle.SetInteriorColor(ColorPt.new(0, 0, 1), 3)
382 circle.SetBorderStyle( BorderStyle.new( BorderStyle::E_dashed, 3, 0, 0, [2, 4] ) )
383 circle.SetPadding( 2 )
384 circle.RefreshAppearance()
385 page3.AnnotPushBack(circle)
386
387 sq = Square.Create( doc.GetSDFDoc(), Rect.new(10,200, 80, 300 ) )
388 sq.SetColor(ColorPt.new(0, 0, 0), 3)
389 sq.RefreshAppearance()
390 page3.AnnotPushBack( sq )
391
392 sq = Square.Create( doc.GetSDFDoc(), Rect.new(500,200, 580, 300 ) )
393 sq.SetColor(ColorPt.new(1, 0, 0), 3)
394 sq.SetInteriorColor(ColorPt.new(0, 1, 1), 3)
395 sq.SetBorderStyle( BorderStyle.new( BorderStyle::E_dashed, 6, 0, 0, [4, 2] ) )
396 sq.SetPadding( 4 )
397 sq.RefreshAppearance()
398 page3.AnnotPushBack( sq )
399
400 poly = Polygon.Create(doc.GetSDFDoc(), Rect.new(5, 500, 125, 590))
401 poly.SetColor(ColorPt.new(1, 0, 0), 3)
402 poly.SetInteriorColor(ColorPt.new(1, 1, 0), 3)
403 poly.SetVertex(0, Point.new(12,510) )
404 poly.SetVertex(1, Point.new(100,510) )
405 poly.SetVertex(2, Point.new(100,555) )
406 poly.SetVertex(3, Point.new(35,544) )
407 poly.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 4, 0, 0 ) )
408 poly.SetPadding( 4 )
409 poly.RefreshAppearance()
410 page3.AnnotPushBack( poly )
411
412 poly = PolyLine.Create(doc.GetSDFDoc(), Rect.new(400, 10, 500, 90))
413 poly.SetColor(ColorPt.new(1, 0, 0), 3)
414 poly.SetInteriorColor(ColorPt.new(0, 1, 0), 3)
415 poly.SetVertex(0, Point.new(405,20) )
416 poly.SetVertex(1, Point.new(440,40) )
417 poly.SetVertex(2, Point.new(410,60) )
418 poly.SetVertex(3, Point.new(470,80) )
419 poly.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 2, 0, 0 ) )
420 poly.SetPadding( 4 )
421 poly.SetStartStyle( LineAnnot::E_RClosedArrow )
422 poly.SetEndStyle( LineAnnot::E_ClosedArrow )
423 poly.RefreshAppearance()
424 page3.AnnotPushBack( poly )
425
426 lk = Link.Create( doc.GetSDFDoc(), Rect.new(5,5,55,24) )
427 lk.RefreshAppearance()
428 page3.AnnotPushBack( lk )
429
430 page4 = doc.PageCreate(Rect.new(0, 0, 600, 600))
431 ew.Begin(page4) # begin writing to the page
432 ew.End() # save changes to the current page
433 doc.PagePushBack(page4)
434
435 ew.Begin( page4 )
436 font = Font.Create(doc.GetSDFDoc(), Font::E_helvetica)
437 element = eb.CreateTextBegin( font, 16 )
438 element.SetPathFill(true)
439 ew.WriteElement(element)
440 element = eb.CreateTextRun( "Some random text on the page", font, 16 )
441 element.SetTextMatrix(1, 0, 0, 1, 100, 500 )
442 ew.WriteElement(element)
443 ew.WriteElement( eb.CreateTextEnd() )
444 ew.End()
445
446 hl = HighlightAnnot.Create( doc.GetSDFDoc(), Rect.new(100,490,150,515) )
447 hl.SetColor( ColorPt.new(0,1,0), 3 )
448 hl.RefreshAppearance()
449 page4.AnnotPushBack( hl )
450
451 sq = Squiggly.Create( doc.GetSDFDoc(), Rect.new(100,450,250,600) )
452 sq.SetQuadPoint( 0, QuadPoint.new( Point.new(122,455), Point.new(240, 545), Point.new(230, 595), Point.new(101,500 ) ) )
453 sq.RefreshAppearance()
454 page4.AnnotPushBack( sq )
455
456 cr = Caret.Create( doc.GetSDFDoc(), Rect.new(100,40,129,69) )
457 cr.SetColor( ColorPt.new(0,0,1), 3 )
458 cr.SetSymbol( "P" )
459 cr.RefreshAppearance()
460 page4.AnnotPushBack( cr )
461
462 page5 = doc.PageCreate(Rect.new(0, 0, 600, 600))
463 ew.Begin(page5) # begin writing to the page
464 ew.End() # save changes to the current page
465 doc.PagePushBack(page5)
466 fs = FileSpec.Create( doc.GetSDFDoc(), ($input_path + "butterfly.png"), false )
467 page6 = doc.PageCreate(Rect.new(0, 0, 600, 600))
468 ew.Begin(page6) # begin writing to the page
469 ew.End() # save changes to the current page
470 doc.PagePushBack(page6)
471
472
473 txt = Text.Create( doc.GetSDFDoc(), Rect.new( 10, 20, 30, 40 ) )
474 txt.SetIcon( "UserIcon" )
475 txt.SetContents( "User defined icon, unrecognized by appearance generator" )
476 txt.SetColor( ColorPt.new(0,1,0) )
477 txt.RefreshAppearance()
478 page6.AnnotPushBack( txt )
479
480 ink = Ink.Create( doc.GetSDFDoc(), Rect.new( 100, 400, 200, 550 ) )
481 ink.SetColor( ColorPt.new(0,0,1) )
482 ink.SetPoint( 1, 3, Point.new( 220, 505) )
483 ink.SetPoint( 1, 0, Point.new( 100, 490) )
484 ink.SetPoint( 0, 1, Point.new( 120, 410) )
485 ink.SetPoint( 0, 0, Point.new( 100, 400) )
486 ink.SetPoint( 1, 2, Point.new( 180, 490) )
487 ink.SetPoint( 1, 1, Point.new( 140, 440) )
488 ink.SetBorderStyle( BorderStyle.new( BorderStyle::E_solid, 3, 0, 0 ) )
489 ink.RefreshAppearance()
490 page6.AnnotPushBack( ink )
491
492 page7 = doc.PageCreate(Rect.new(0, 0, 600, 600))
493 ew.Begin(page7) # begin writing to the page
494 ew.End() # save changes to the current page
495 doc.PagePushBack(page7)
496
497 snd = Sound.Create( doc.GetSDFDoc(), Rect.new( 100, 500, 120, 520 ) )
498 snd.SetColor( ColorPt.new(1,1,0) )
499 snd.SetIcon( Sound::E_Speaker )
500 snd.RefreshAppearance()
501 page7.AnnotPushBack( snd )
502
503 snd = Sound.Create( doc.GetSDFDoc(), Rect.new( 200, 500, 220, 520 ) )
504 snd.SetColor( ColorPt.new(1,1,0) )
505 snd.SetIcon( Sound::E_Mic )
506 snd.RefreshAppearance()
507 page7.AnnotPushBack( snd )
508
509 page8 = doc.PageCreate(Rect.new(0, 0, 600, 600))
510 ew.Begin(page8) # begin writing to the page
511 ew.End() # save changes to the current page
512 doc.PagePushBack(page8)
513
514 ipage = 0
515 while ipage<2 do
516 px = 5
517 py = 520
518 istamp = RubberStamp::E_Approved
519 while istamp <= RubberStamp::E_Draft do
520 st = RubberStamp.Create(doc.GetSDFDoc(), Rect.new(1,1,100,100))
521 st.SetIcon( istamp )
522 st.SetContents( st.GetIconName() )
523 st.SetRect( Rect.new(px, py, px+100, py+25 ) )
524 py -= 100
525 if py < 0
526 py = 520
527 px+=200
528 end
529 if ipage == 0
530 #page7.AnnotPushBack( st )
531 else
532 page8.AnnotPushBack( st )
533 st.RefreshAppearance()
534 end
535 istamp = istamp + 1
536 end
537 ipage = ipage + 1
538 end
539
540 st = RubberStamp.Create( doc.GetSDFDoc(), Rect.new(400,5,550,45) )
541 st.SetIcon( "UserStamp" )
542 st.SetContents( "User defined stamp" )
543 page8.AnnotPushBack( st )
544 st.RefreshAppearance()
545end
546
547 PDFNet.Initialize(PDFTronLicense.Key)
548
549 doc = PDFDoc.new($input_path + "numbered.pdf")
550 doc.InitSecurityHandler()
551
552 # An example of using SDF/Cos API to add any type of annotations.
553 AnnotationLowLevelAPI(doc)
554 doc.Save($output_path + "annotation_test1.pdf", SDFDoc::E_remove_unused)
555 puts "Done. Results saved in annotation_test1.pdf"
556
557 # An example of using the high-level PDFNet API to read existing annotations,
558 # to edit existing annotations, and to create new annotation from scratch.
559 AnnotationHighLevelAPI(doc)
560 doc.Save(($output_path + "annotation_test2.pdf"), SDFDoc::E_linearized)
561 doc.Close()
562 puts "Done. Results saved in annotation_test2.pdf"
563
564 doc1 = PDFDoc.new()
565 CreateTestAnnots(doc1)
566 outfname = $output_path + "new_annot_test_api.pdf"
567 doc1.Save(outfname, SDFDoc::E_linearized)
568 doc1.Close()
569 PDFNet.Terminate
570 puts "Saved new_annot_test_api.pdf"
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4Imports pdftron
5Imports pdftron.Common
6Imports pdftron.Filters
7Imports pdftron.SDF
8Imports pdftron.PDF
9Imports pdftron.PDF.Annots
10
11Namespace AnnotationTestVB
12
13 ''' <summary>
14 ''' Summary description for Class1.
15 ''' </summary>
16 Class Class1
17
18 Private Shared pdfNetLoader As pdftron.PDFNetLoader
19 Shared Sub New()
20 pdfNetLoader = pdftron.PDFNetLoader.Instance
21 End Sub
22
23 Private Shared Sub AnnotationHighLevelAPI(ByVal doc As PDFDoc)
24 ' The following code snippet traverses all annotations in the document
25 System.Console.WriteLine("Traversing all annotations in the document...")
26 Dim uri As String
27 Dim page_num As Integer = 1
28 Dim itr As PageIterator = doc.GetPageIterator
29 Do While itr.HasNext
30 System.Console.WriteLine("Page " & page_num & ": ")
31 page_num = page_num + 1
32 Dim page As Page = itr.Current
33 Dim num_annots As Integer = page.GetNumAnnots
34 Dim i As Integer = 0
35 Do While (i < num_annots)
36 Dim annot As Annot = page.GetAnnot(i)
37 If Not annot.IsValid Then
38 'TODO: Warning!!! continue If
39 End If
40
41 System.Console.WriteLine(("Annot Type: " & annot.GetSDFObj.Get("Subtype").Value.GetName))
42 Dim bbox As Rect = annot.GetRect
43 System.Console.WriteLine(" Position: " & bbox.x1 & ", " &
44 bbox.y1 & ", " & bbox.x2 & ", " & bbox.y2)
45 Select Case (annot.GetType)
46 Case annot.Type.e_Link
47 Dim action As Action = New Link(annot).GetAction
48 If Not action.IsValid Then
49 'TODO: Warning!!! continue If
50 End If
51
52 If (action.GetType = action.Type.e_GoTo) Then
53 Dim dest As Destination = action.GetDest
54 If Not dest.IsValid Then
55 System.Console.WriteLine(" Destination is not valid")
56 Else
57 Dim pg_num As Integer = dest.GetPage.GetIndex
58 System.Console.WriteLine(" Links to: page number " &
59 pg_num & " in this document")
60 End If
61
62 ElseIf (action.GetType = action.Type.e_URI) Then
63 uri = action.GetSDFObj.Get("URI").Value.GetAsPDFText
64 System.Console.WriteLine(" Links to: " & uri)
65 End If
66
67 ' ...
68 Case annot.Type.e_Widget
69 Case annot.Type.e_FileAttachment
70 End Select
71
72 i = (i + 1)
73 Loop
74
75 itr.Next()
76 Loop
77
78 ' Use the high-level API to create new annotations.
79 Dim first_page As Page = doc.GetPage(1)
80 ' Create a hyperlink...
81 Dim hyperlink As Link = Link.Create(doc, New Rect(85, 570, 503, 524), Action.CreateURI(doc, "http://www.pdftron.com"))
82 first_page.AnnotPushBack(hyperlink)
83 ' Create an intra-document link...
84 Dim goto_page_3 As Action = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0))
85 Dim lnk As Link = Link.Create(doc, New Rect(85, 458, 503, 502), goto_page_3)
86 lnk.SetColor(New ColorPt(0, 0, 1))
87 ' Add the new annotation to the first page
88 first_page.AnnotPushBack(lnk)
89 ' Create a stamp annotation ...
90 Dim stamp As RubberStamp = RubberStamp.Create(doc, New Rect(30, 30, 300, 200))
91 stamp.SetIcon("Draft")
92 first_page.AnnotPushBack(stamp)
93 ' Create a file attachment annotation (embed the 'peppers.jpg').
94 Dim file_attach As FileAttachment = FileAttachment.Create(doc, New Rect(80, 280, 108, 320), (input_path + "peppers.jpg"))
95 first_page.AnnotPushBack(file_attach)
96 Dim inkobj As Ink = Ink.Create(doc, New Rect(110, 10, 300, 200))
97 Dim pt3 As Point = New Point(110, 10)
98 'pt3.x = 110; pt3.y = 10;
99 inkobj.SetPoint(0, 0, pt3)
100 pt3.x = 150
101 pt3.y = 50
102 inkobj.SetPoint(0, 1, pt3)
103 pt3.x = 190
104 pt3.y = 60
105 inkobj.SetPoint(0, 2, pt3)
106 pt3.x = 180
107 pt3.y = 90
108 inkobj.SetPoint(1, 0, pt3)
109 pt3.x = 190
110 pt3.y = 95
111 inkobj.SetPoint(1, 1, pt3)
112 pt3.x = 200
113 pt3.y = 100
114 inkobj.SetPoint(1, 2, pt3)
115 pt3.x = 166
116 pt3.y = 86
117 inkobj.SetPoint(2, 0, pt3)
118 pt3.x = 196
119 pt3.y = 96
120 inkobj.SetPoint(2, 1, pt3)
121 pt3.x = 221
122 pt3.y = 121
123 inkobj.SetPoint(2, 2, pt3)
124 pt3.x = 288
125 pt3.y = 188
126 inkobj.SetPoint(2, 3, pt3)
127 inkobj.SetColor(New ColorPt(0, 1, 1), 3)
128 first_page.AnnotPushBack(inkobj)
129 End Sub
130
131 Private Shared Sub AnnotationLowLevelAPI(ByVal doc As PDFDoc)
132 Dim page As Page = doc.GetPage(1)
133 Dim annots As Obj = page.GetAnnots
134 If (annots Is Nothing) Then
135 ' If there are no annotations, create a new annotation
136 ' array for the page.
137 annots = doc.CreateIndirectArray
138 page.GetSDFObj.Put("Annots", annots)
139 End If
140
141 ' Create the Text annotation
142 Dim text_annot As Obj = doc.CreateIndirectDict
143 text_annot.PutName("Subtype", "Text")
144 text_annot.PutBool("Open", True)
145 text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
146 text_annot.PutRect("Rect", 266, 116, 430, 204)
147 ' Insert the annotation in the page annotation array
148 annots.PushBack(text_annot)
149 ' Create a Link annotation
150 Dim link1 As Obj = doc.CreateIndirectDict
151 link1.PutName("Subtype", "Link")
152 Dim dest As Destination = Destination.CreateFit(doc.GetPage(2))
153 link1.Put("Dest", dest.GetSDFObj)
154 link1.PutRect("Rect", 85, 705, 503, 661)
155 annots.PushBack(link1)
156 ' Create another Link annotation
157 Dim link2 As Obj = doc.CreateIndirectDict
158 link2.PutName("Subtype", "Link")
159 Dim dest2 As Destination = Destination.CreateFit(doc.GetPage(3))
160 link2.Put("Dest", dest2.GetSDFObj)
161 link2.PutRect("Rect", 85, 638, 503, 594)
162 annots.PushBack(link2)
163 ' Note that PDFNet APi can be used to modify existing annotations.
164 ' In the following example we will modify the second link annotation
165 ' (link2) so that it points to the 10th page. We also use a different
166 ' destination page fit type.
167 link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, (792 - 70), 10).GetSDFObj)
168 ' Create a third link annotation with a hyperlink action (all other
169 ' annotation types can be created in a similar way)
170 Dim link3 As Obj = doc.CreateIndirectDict
171 link3.PutName("Subtype", "Link")
172 link3.PutRect("Rect", 85, 570, 503, 524)
173 ' Create a URI action
174 Dim action As Obj = link3.PutDict("A")
175 action.PutName("S", "URI")
176 action.PutString("URI", "http://www.pdftron.com")
177 annots.PushBack(link3)
178 End Sub
179
180 Private Shared Sub CreateTestAnnots(ByVal doc As PDFDoc)
181 Dim ew As ElementWriter = New ElementWriter
182 Dim eb As ElementBuilder = New ElementBuilder
183 Dim element As Element
184 Dim first_page As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
185 doc.PagePushBack(first_page)
186 ew.Begin(first_page, ElementWriter.WriteMode.e_overlay, False)
187 ' begin writing to this page
188 ew.End()
189 ' save changes to the current page
190 '
191 ' Test of a free text annotation.
192 '
193 Dim txtannot As FreeText = FreeText.Create(doc, New Rect(10, 400, 160, 570))
194 txtannot.SetContents(ControlChars.Lf + ControlChars.Lf + "Some swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." + ControlChars.Lf + ControlChars.Lf + "Aha!" + ControlChars.Lf + ControlChars.Lf + "And there was much rejoicing!")
195 txtannot.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 1, 10, 20))
196 txtannot.SetQuaddingFormat(0)
197 first_page.AnnotPushBack(txtannot)
198 txtannot.RefreshAppearance()
199 txtannot = FreeText.Create(doc, New Rect(100, 100, 350, 500))
200 txtannot.SetContentRect(New Rect(200, 200, 350, 500))
201 txtannot.SetContents(ControlChars.Lf + ControlChars.Lf + "Some swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." + ControlChars.Lf + ControlChars.Lf + "Aha!" + ControlChars.Lf + ControlChars.Lf + "And there was much rejoicing!")
202 txtannot.SetCalloutLinePoints(New Point(200, 300), New Point(150, 290), New Point(110, 110))
203 txtannot.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 1, 10, 20))
204 txtannot.SetEndingStyle(Line.EndingStyle.e_ClosedArrow)
205 txtannot.SetColor(New ColorPt(0, 1, 0))
206 txtannot.SetQuaddingFormat(1)
207 first_page.AnnotPushBack(txtannot)
208 txtannot.RefreshAppearance()
209 txtannot = FreeText.Create(doc, New Rect(400, 10, 550, 400))
210 txtannot.SetContents(ControlChars.Lf + ControlChars.Lf + "Some swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." + ControlChars.Lf + ControlChars.Lf + "Aha!" + ControlChars.Lf + ControlChars.Lf + "And there was much rejoicing!")
211 txtannot.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 1, 10, 20))
212 txtannot.SetColor(New ColorPt(0, 0, 1))
213 txtannot.SetOpacity(0.2)
214 txtannot.SetQuaddingFormat(2)
215 first_page.AnnotPushBack(txtannot)
216 txtannot.RefreshAppearance()
217 Dim page As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
218 doc.PagePushBack(page)
219 ew.Begin(page, ElementWriter.WriteMode.e_overlay, False)
220 ' begin writing to this page
221 eb.Reset()
222 ' Reset the GState to default
223 ew.End()
224 ' save changes to the current page
225 'Create a Line annotation...
226 Dim lineobj As Line = Line.Create(doc, New Rect(250, 250, 400, 400))
227 lineobj.SetStartPoint(New Point(350, 270))
228 lineobj.SetEndPoint(New Point(260, 370))
229 lineobj.SetStartStyle(Line.EndingStyle.e_Square)
230 lineobj.SetEndStyle(Line.EndingStyle.e_Circle)
231 lineobj.SetColor(New ColorPt(0.3, 0.5, 0), 3)
232 lineobj.SetContents("Dashed Captioned")
233 lineobj.SetShowCaption(True)
234 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
235 Dim dash() As Double = New Double((2) - 1) {}
236 dash(0) = 2
237 dash(1) = 2
238 lineobj.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_dashed, 2, 0, 0, dash))
239 lineobj.RefreshAppearance()
240 page.AnnotPushBack(lineobj)
241 lineobj = Line.Create(doc, New Rect(347, 377, 600, 600))
242 lineobj.SetStartPoint(New Point(385, 410))
243 lineobj.SetEndPoint(New Point(540, 555))
244 lineobj.SetStartStyle(Line.EndingStyle.e_Circle)
245 lineobj.SetEndStyle(Line.EndingStyle.e_OpenArrow)
246 lineobj.SetColor(New ColorPt(1, 0, 0), 3)
247 lineobj.SetInteriorColor(New ColorPt(0, 1, 0), 3)
248 lineobj.SetContents("Inline Caption")
249 lineobj.SetShowCaption(True)
250 lineobj.SetCaptionPosition(Line.CapPos.e_Inline)
251 lineobj.SetLeaderLineExtensionLength(4)
252 lineobj.SetLeaderLineLength(-12)
253 lineobj.SetLeaderLineOffset(2)
254 lineobj.RefreshAppearance()
255 page.AnnotPushBack(lineobj)
256 lineobj = Line.Create(doc, New Rect(10, 400, 200, 600))
257 lineobj.SetStartPoint(New Point(25, 426))
258 lineobj.SetEndPoint(New Point(180, 555))
259 lineobj.SetStartStyle(Line.EndingStyle.e_Circle)
260 lineobj.SetEndStyle(Line.EndingStyle.e_Square)
261 lineobj.SetColor(New ColorPt(0, 0, 1), 3)
262 lineobj.SetInteriorColor(New ColorPt(1, 0, 0), 3)
263 lineobj.SetContents("Offset Caption")
264 lineobj.SetShowCaption(True)
265 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
266 lineobj.SetTextHOffset(-60)
267 lineobj.SetTextVOffset(10)
268 lineobj.RefreshAppearance()
269 page.AnnotPushBack(lineobj)
270 lineobj = Line.Create(doc, New Rect(200, 10, 400, 70))
271 lineobj.SetStartPoint(New Point(220, 25))
272 lineobj.SetEndPoint(New Point(370, 60))
273 lineobj.SetStartStyle(Line.EndingStyle.e_Butt)
274 lineobj.SetEndStyle(Line.EndingStyle.e_OpenArrow)
275 lineobj.SetColor(New ColorPt(0, 0, 1), 3)
276 lineobj.SetContents("Regular Caption")
277 lineobj.SetShowCaption(True)
278 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
279 lineobj.RefreshAppearance()
280 page.AnnotPushBack(lineobj)
281 lineobj = Line.Create(doc, New Rect(200, 70, 400, 130))
282 lineobj.SetStartPoint(New Point(220, 111))
283 lineobj.SetEndPoint(New Point(370, 78))
284 lineobj.SetStartStyle(Line.EndingStyle.e_Circle)
285 lineobj.SetEndStyle(Line.EndingStyle.e_Diamond)
286 lineobj.SetContents("Circle to Diamond")
287 lineobj.SetColor(New ColorPt(0, 0, 1), 3)
288 lineobj.SetInteriorColor(New ColorPt(0, 1, 0), 3)
289 lineobj.SetShowCaption(True)
290 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
291 lineobj.RefreshAppearance()
292 page.AnnotPushBack(lineobj)
293 lineobj = Line.Create(doc, New Rect(10, 100, 160, 200))
294 lineobj.SetStartPoint(New Point(15, 110))
295 lineobj.SetEndPoint(New Point(150, 190))
296 lineobj.SetStartStyle(Line.EndingStyle.e_Slash)
297 lineobj.SetEndStyle(Line.EndingStyle.e_ClosedArrow)
298 lineobj.SetContents("Slash to CArrow")
299 lineobj.SetColor(New ColorPt(1, 0, 0), 3)
300 lineobj.SetInteriorColor(New ColorPt(0, 1, 1), 3)
301 lineobj.SetShowCaption(True)
302 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
303 lineobj.RefreshAppearance()
304 page.AnnotPushBack(lineobj)
305 lineobj = Line.Create(doc, New Rect(270, 270, 570, 433))
306 lineobj.SetStartPoint(New Point(300, 400))
307 lineobj.SetEndPoint(New Point(550, 300))
308 lineobj.SetStartStyle(Line.EndingStyle.e_RClosedArrow)
309 lineobj.SetEndStyle(Line.EndingStyle.e_ROpenArrow)
310 lineobj.SetContents("ROpen & RClosed arrows")
311 lineobj.SetColor(New ColorPt(0, 0, 1), 3)
312 lineobj.SetInteriorColor(New ColorPt(0, 1, 0), 3)
313 lineobj.SetShowCaption(True)
314 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
315 lineobj.RefreshAppearance()
316 page.AnnotPushBack(lineobj)
317 lineobj = Line.Create(doc, New Rect(195, 395, 205, 505))
318 lineobj.SetStartPoint(New Point(200, 400))
319 lineobj.SetEndPoint(New Point(200, 500))
320 lineobj.RefreshAppearance()
321 page.AnnotPushBack(lineobj)
322 lineobj = Line.Create(doc, New Rect(55, 299, 150, 301))
323 lineobj.SetStartPoint(New Point(55, 300))
324 lineobj.SetEndPoint(New Point(155, 300))
325 lineobj.SetStartStyle(Line.EndingStyle.e_Circle)
326 lineobj.SetEndStyle(Line.EndingStyle.e_Circle)
327 lineobj.SetContents("Caption that's longer than its line.")
328 lineobj.SetColor(New ColorPt(1, 0, 1), 3)
329 lineobj.SetInteriorColor(New ColorPt(0, 1, 0), 3)
330 lineobj.SetShowCaption(True)
331 lineobj.SetCaptionPosition(Line.CapPos.e_Top)
332 lineobj.RefreshAppearance()
333 page.AnnotPushBack(lineobj)
334 lineobj = Line.Create(doc, New Rect(300, 200, 390, 234))
335 lineobj.SetStartPoint(New Point(310, 210))
336 lineobj.SetEndPoint(New Point(380, 220))
337 lineobj.SetColor(New ColorPt(0, 0, 0), 3)
338 lineobj.RefreshAppearance()
339 page.AnnotPushBack(lineobj)
340 Dim page3 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
341 ew.Begin(page3)
342 ' begin writing to the page
343 ew.End()
344 ' save changes to the current page
345 doc.PagePushBack(page3)
346 Dim circle As Circle = Circle.Create(doc, New Rect(300, 300, 390, 350))
347 circle.SetColor(New ColorPt(0, 0, 0), 3)
348 circle.RefreshAppearance()
349 page3.AnnotPushBack(circle)
350 circle = Circle.Create(doc, New Rect(100, 100, 200, 200))
351 circle.SetColor(New ColorPt(0, 1, 0), 3)
352 circle.SetInteriorColor(New ColorPt(0, 0, 1), 3)
353 dash = New Double((2) - 1) {}
354 dash(0) = 2
355 dash(1) = 4
356 circle.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_dashed, 3, 0, 0, dash))
357 circle.SetPadding(New Rect(2, 2, 2, 2))
358 circle.RefreshAppearance()
359 page3.AnnotPushBack(circle)
360 Dim sq As Square = Square.Create(doc, New Rect(10, 200, 80, 300))
361 sq.SetColor(New ColorPt(0, 0, 0), 3)
362 sq.RefreshAppearance()
363 page3.AnnotPushBack(sq)
364 sq = Square.Create(doc, New Rect(500, 200, 580, 300))
365 sq.SetColor(New ColorPt(1, 0, 0), 3)
366 sq.SetInteriorColor(New ColorPt(0, 1, 1), 3)
367 dash = New Double((2) - 1) {}
368 dash(0) = 4
369 dash(1) = 2
370 sq.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_dashed, 6, 0, 0, dash))
371 sq.SetPadding(New Rect(4, 4, 4, 4))
372 sq.RefreshAppearance()
373 page3.AnnotPushBack(sq)
374 Dim poly As Polygon = Polygon.Create(doc, New Rect(5, 500, 125, 590))
375 poly.SetColor(New ColorPt(1, 0, 0), 3)
376 poly.SetInteriorColor(New ColorPt(1, 1, 0), 3)
377 poly.SetVertex(0, New Point(12, 510))
378 poly.SetVertex(1, New Point(100, 510))
379 poly.SetVertex(2, New Point(100, 555))
380 poly.SetVertex(3, New Point(35, 544))
381 poly.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 4, 0, 0))
382 poly.SetPadding(New Rect(4, 4, 4, 4))
383 poly.RefreshAppearance()
384 page3.AnnotPushBack(poly)
385 Dim polyln As PolyLine = PolyLine.Create(doc, New Rect(400, 10, 500, 90))
386 polyln.SetColor(New ColorPt(1, 0, 0), 3)
387 polyln.SetInteriorColor(New ColorPt(0, 1, 0), 3)
388 polyln.SetVertex(0, New Point(405, 20))
389 polyln.SetVertex(1, New Point(440, 40))
390 polyln.SetVertex(2, New Point(410, 60))
391 polyln.SetVertex(3, New Point(470, 80))
392 polyln.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 2, 0, 0))
393 polyln.SetPadding(New Rect(4, 4, 4, 4))
394 polyln.SetStartStyle(Line.EndingStyle.e_RClosedArrow)
395 polyln.SetEndStyle(Line.EndingStyle.e_ClosedArrow)
396 polyln.RefreshAppearance()
397 page3.AnnotPushBack(polyln)
398 Dim lk As Link = Link.Create(doc, New Rect(5, 5, 55, 24))
399 'lk.SetColor( ColorPt(0,1,0), 3 );
400 lk.RefreshAppearance()
401 page3.AnnotPushBack(lk)
402 Dim page4 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
403 ew.Begin(page4)
404 ' begin writing to the page
405 ew.End()
406 ' save changes to the current page
407 doc.PagePushBack(page4)
408 ew.Begin(page4)
409 Dim font As Font = Font.Create(doc, Font.StandardType1Font.e_helvetica)
410 element = eb.CreateTextBegin(font, 16)
411 element.SetPathFill(True)
412 ew.WriteElement(element)
413 element = eb.CreateTextRun("Some random text on the page", font, 16)
414 element.SetTextMatrix(1, 0, 0, 1, 100, 500)
415 ew.WriteElement(element)
416 ew.WriteElement(eb.CreateTextEnd)
417 ew.End()
418 Dim hl As Highlight = Highlight.Create(doc, New Rect(100, 490, 150, 515))
419 hl.SetColor(New ColorPt(0, 1, 0), 3)
420 hl.RefreshAppearance()
421 page4.AnnotPushBack(hl)
422 Dim squig As Squiggly = Squiggly.Create(doc, New Rect(100, 450, 250, 600))
423 'sq.SetColor( ColorPt(1,0,0), 3 );
424 squig.SetQuadPoint(0, New QuadPoint(New Point(122, 455), New Point(240, 545), New Point(230, 595), New Point(101, 500)))
425 squig.RefreshAppearance()
426 page4.AnnotPushBack(squig)
427 Dim cr As Caret = Caret.Create(doc, New Rect(100, 40, 129, 69))
428 cr.SetColor(New ColorPt(0, 0, 1), 3)
429 cr.SetSymbol("P")
430 cr.RefreshAppearance()
431 page4.AnnotPushBack(cr)
432 Dim page5 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
433 ew.Begin(page5)
434 ' begin writing to the page
435 ew.End()
436 ' save changes to the current page
437 doc.PagePushBack(page5)
438 Dim page6 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
439 ew.Begin(page6)
440 ' begin writing to the page
441 ew.End()
442 ' save changes to the current page
443 doc.PagePushBack(page6)
444 Dim txt As Text = Text.Create(doc, New Rect(10, 20, 30, 40))
445 txt.SetIcon("UserIcon")
446 txt.SetContents("User defined icon, unrecognized by appearance generator")
447 txt.SetColor(New ColorPt(0, 1, 0))
448 txt.RefreshAppearance()
449 page6.AnnotPushBack(txt)
450 Dim ink As Ink = Ink.Create(doc, New Rect(100, 400, 200, 550))
451 ink.SetColor(New ColorPt(0, 0, 1))
452 ink.SetPoint(1, 3, New Point(220, 505))
453 ink.SetPoint(1, 0, New Point(100, 490))
454 ink.SetPoint(0, 1, New Point(120, 410))
455 ink.SetPoint(0, 0, New Point(100, 400))
456 ink.SetPoint(1, 2, New Point(180, 490))
457 ink.SetPoint(1, 1, New Point(140, 440))
458 ink.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 3, 0, 0))
459 ink.RefreshAppearance()
460 page6.AnnotPushBack(ink)
461 Dim page7 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
462 ew.Begin(page7)
463 ' begin writing to the page
464 ew.End()
465 ' save changes to the current page
466 doc.PagePushBack(page7)
467 Dim snd As Sound = Sound.Create(doc, New Rect(100, 500, 120, 520))
468 snd.SetColor(New ColorPt(1, 1, 0))
469 snd.SetIcon(Sound.Icon.e_Speaker)
470 snd.RefreshAppearance()
471 page7.AnnotPushBack(snd)
472 snd = Sound.Create(doc, New Rect(200, 500, 220, 520))
473 snd.SetColor(New ColorPt(1, 1, 0))
474 snd.SetIcon(Sound.Icon.e_Mic)
475 snd.RefreshAppearance()
476 page7.AnnotPushBack(snd)
477 Dim page8 As Page = doc.PageCreate(New Rect(0, 0, 600, 600))
478 ew.Begin(page8)
479 ' begin writing to the page
480 ew.End()
481 ' save changes to the current page
482 doc.PagePushBack(page8)
483 Dim ipage As Integer = 0
484 Do While (ipage < 2)
485 Dim py As Double = 520
486 Dim px As Double = 5
487 Dim istamp As RubberStamp.Icon = RubberStamp.Icon.e_Approved
488 Do While (istamp <= RubberStamp.Icon.e_Draft)
489 Dim stmp As RubberStamp = RubberStamp.Create(doc, New Rect(1, 1, 100, 100))
490 stmp.SetIcon(istamp)
491 stmp.SetContents(stmp.GetIconName)
492 stmp.SetRect(New Rect(px, py, (px + 100), (py + 25)))
493 py = (py - 100)
494 If (py < 0) Then
495 py = 520
496 px = (px + 200)
497 End If
498
499 If (ipage = 0) Then
500
501 Else
502 page8.AnnotPushBack(stmp)
503 stmp.RefreshAppearance()
504 End If
505
506 istamp = CType((CType(istamp, Integer) + 1), RubberStamp.Icon)
507 Loop
508
509 ipage = (ipage + 1)
510 Loop
511
512 Dim st As RubberStamp = RubberStamp.Create(doc, New Rect(400, 5, 550, 45))
513 st.SetIcon("UserStamp")
514 st.SetContents("User defined stamp")
515 page8.AnnotPushBack(st)
516 st.RefreshAppearance()
517 End Sub
518
519 ' Relative path to the folder containing test files.
520 Private Const input_path As String = "../../../../TestFiles/"
521
522 Private Const output_path As String = "../../../../TestFiles/Output/"
523
524 ''' <summary>
525 ''' The main entry point for the application.
526 ''' </summary>
527 <System.STAThread()>
528 Public Shared Sub Main(ByVal args() As String)
529 PDFNet.Initialize(PDFTronLicense.Key)
530
531 Try
532 Dim doc As PDFDoc = New PDFDoc((input_path + "numbered.pdf"))
533 doc.InitSecurityHandler()
534 ' An example of using SDF/Cos API to add any type of annotations.
535 Class1.AnnotationLowLevelAPI(doc)
536 doc.Save((output_path + "annotation_test1.pdf"), SDFDoc.SaveOptions.e_linearized)
537 System.Console.WriteLine("Done. Results saved in annotation_test1.pdf")
538 ' An example of using the high-level PDFNet API to read existing annotations,
539 ' to edit existing annotations, and to create new annotation from scratch.
540 Class1.AnnotationHighLevelAPI(doc)
541 doc.Save((output_path + "annotation_test2.pdf"), SDFDoc.SaveOptions.e_linearized)
542 System.Console.WriteLine("Done. Results saved in annotation_test2.pdf")
543 ' an example of creating various annotations in a brand new document
544 Dim doc1 As PDFDoc = New PDFDoc
545 Class1.CreateTestAnnots(doc1)
546 doc1.Save((output_path + "new_annot_test_api.pdf"), SDFDoc.SaveOptions.e_linearized)
547 System.Console.WriteLine("Saved new_annot_test_api.pdf")
548 Catch e As PDFNetException
549 System.Console.WriteLine(e.Message)
550 End Try
551 PDFNet.Terminate()
552 End Sub
553 End Class
554End Namespace
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales