Annotation

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales