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');