Sample C# code to use Apryse SDK for adding or editing PDF annotations. The annotation types included in this sample are: hyperlink, intra-document link, stamp, rubber stamp, file attachment, sound, text, free-text, line, circle, square, polygon, polyline, highlight, squiggly, caret, and ink. Learn more about our Xamarin SDK and PDF Annotation Library.
1//
2// Copyright (c) 2001-2021 by PDFTron Systems 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
12using NUnit.Framework;
13
14namespace MiscellaneousSamples
15{
16 /// <summary>
17 /// Summary description for Class1.
18 /// </summary>
19 [TestFixture]
20 public class AnnotationTest
21 {
22
23 private static void AnnotationHighLevelAPI(PDFDoc doc)
24 {
25 // The following code snippet traverses all annotations in the document
26 System.Console.WriteLine("Traversing all annotations in the document...");
27
28 string uri;
29 int page_num = 1;
30 for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
31 {
32 System.Console.WriteLine("Page " + page_num++ + ": ");
33
34 Page page = itr.Current();
35 int num_annots = page.GetNumAnnots();
36 for (int i = 0; i < num_annots; ++i)
37 {
38 Annot annot = page.GetAnnot(i);
39 if (!annot.IsValid()) continue;
40 System.Console.WriteLine("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName());
41
42 Rect bbox = annot.GetRect();
43 System.Console.WriteLine(" Position: " + bbox.x1
44 + ", " + bbox.y1
45 + ", " + bbox.x2
46 + ", " + bbox.y2);
47
48 switch (annot.GetType())
49 {
50 case Annot.Type.e_Link:
51 {
52 Link lnk = new Link(annot);
53 Action action = lnk.GetAction();
54 if (!action.IsValid()) continue;
55 if (action.GetType() == Action.Type.e_GoTo)
56 {
57 Destination dest = action.GetDest();
58 if (!dest.IsValid())
59 {
60 System.Console.WriteLine(" Destination is not valid");
61 }
62 else
63 {
64 int pg_num = dest.GetPage().GetIndex();
65 System.Console.WriteLine(" Links to: page number " + pg_num + " in this document");
66 }
67 }
68 else if (action.GetType() == Action.Type.e_URI)
69 {
70 uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText();
71 System.Console.WriteLine(" Links to: " + uri);
72 }
73 // ...
74 }
75 break;
76 case Annot.Type.e_Widget:
77 break;
78 case Annot.Type.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 Link hyperlink = Link.Create(doc, new 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 Link link = Link.Create(doc, new Rect(85, 458, 503, 502), goto_page_3);
97 link.SetColor(new 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 RubberStamp stamp = RubberStamp.Create(doc, new 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 FileAttachment file_attach = FileAttachment.Create(doc, new Rect(80, 280, 108, 320), (Utils.GetAssetTempFile(input_path + "peppers.jpg")));
109 first_page.AnnotPushBack(file_attach);
110
111
112 Ink ink = Ink.Create(doc, new Rect(110, 10, 300, 200));
113 Point pt3 = new Point(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(new ColorPt(0, 1, 1), 3);
135 first_page.AnnotPushBack(ink);
136 }
137
138 static void AnnotationLowLevelAPI(PDFDoc doc)
139 {
140 Page page = doc.GetPage(1);
141
142 Obj annots = page.GetAnnots();
143 if (annots == null)
144 {
145 // If there are no annotations, create a new annotation
146 // array for the page.
147 annots = doc.CreateIndirectArray();
148 page.GetSDFObj().Put("Annots", annots);
149 }
150
151 // Create the Text annotation
152 Obj text_annot = doc.CreateIndirectDict();
153 text_annot.PutName("Subtype", "Text");
154 text_annot.PutBool("Open", true);
155 text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
156 text_annot.PutRect("Rect", 266, 116, 430, 204);
157
158 // Insert the annotation in the page annotation array
159 annots.PushBack(text_annot);
160
161 // Create a Link annotation
162 Obj link1 = doc.CreateIndirectDict();
163 link1.PutName("Subtype", "Link");
164 Destination dest = Destination.CreateFit(doc.GetPage(2));
165 link1.Put("Dest", dest.GetSDFObj());
166 link1.PutRect("Rect", 85, 705, 503, 661);
167 annots.PushBack(link1);
168
169 // Create another Link annotation
170 Obj link2 = doc.CreateIndirectDict();
171 link2.PutName("Subtype", "Link");
172 Destination dest2 = Destination.CreateFit(doc.GetPage(3));
173 link2.Put("Dest", dest2.GetSDFObj());
174 link2.PutRect("Rect", 85, 638, 503, 594);
175 annots.PushBack(link2);
176
177 // Note that PDFNet APi can be used to modify existing annotations.
178 // In the following example we will modify the second link annotation
179 // (link2) so that it points to the 10th page. We also use a different
180 // destination page fit type.
181
182 link2.Put("Dest",
183 Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj());
184
185 // Create a third link annotation with a hyperlink action (all other
186 // annotation types can be created in a similar way)
187 Obj link3 = doc.CreateIndirectDict();
188 link3.PutName("Subtype", "Link");
189 link3.PutRect("Rect", 85, 570, 503, 524);
190
191 // Create a URI action
192 Obj action = link3.PutDict("A");
193 action.PutName("S", "URI");
194 action.PutString("URI", "http://www.pdftron.com");
195 annots.PushBack(link3);
196 }
197
198 private static void CreateTestAnnots(PDFDoc doc) {
199
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.WriteMode.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.Style.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.Style.e_solid, 1, 10, 20 ) );
228 txtannot.SetEndingStyle(Line.EndingStyle.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.Style.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.WriteMode.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.EndingStyle.e_Square);
258 line.SetEndStyle(Line.EndingStyle.e_Circle);
259 line.SetColor(new ColorPt(.3, .5, 0), 3);
260 line.SetContents( "Dashed Captioned" );
261 line.SetShowCaption(true);
262 line.SetCaptionPosition(Line.CapPos.e_Top );
263 double[] dash = new double[2];
264 dash[0] = 2;
265 dash[1] = 2.0;
266 line.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 2, 0, 0, dash ) );
267 line.RefreshAppearance();
268 page.AnnotPushBack(line);
269 }
270 {
271 Line line=Line.Create(doc, new Rect(347, 377, 600, 600));
272 line.SetStartPoint( new Point(385, 410 ) );
273 line.SetEndPoint(new Point(540,555) );
274 line.SetStartStyle(Line.EndingStyle.e_Circle);
275 line.SetEndStyle(Line.EndingStyle.e_OpenArrow);
276 line.SetColor(new ColorPt(1, 0, 0), 3);
277 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
278 line.SetContents( "Inline Caption" );
279 line.SetShowCaption(true);
280 line.SetCaptionPosition( Line.CapPos.e_Inline);
281 line.SetLeaderLineExtensionLength( -4 );
282 line.SetLeaderLineLength( -12 );
283 line.SetLeaderLineOffset( 2 );
284 line.RefreshAppearance();
285 page.AnnotPushBack(line);
286 }
287 {
288 Line line=Line.Create(doc, new Rect(10, 400, 200, 600));
289 line.SetStartPoint(new Point(25, 426 ) );
290 line.SetEndPoint(new Point(180,555) );
291 line.SetStartStyle(Line.EndingStyle.e_Circle);
292 line.SetEndStyle(Line.EndingStyle.e_Square);
293 line.SetColor(new ColorPt(0, 0, 1), 3);
294 line.SetInteriorColor(new ColorPt(1, 0, 0), 3);
295 line.SetContents("Offset Caption");
296 line.SetShowCaption(true);
297 line.SetCaptionPosition( Line.CapPos.e_Top );
298 line.SetTextHOffset( -60 );
299 line.SetTextVOffset( 10 );
300 line.RefreshAppearance();
301 page.AnnotPushBack(line);
302 }
303 {
304 Line line=Line.Create(doc, new Rect(200, 10, 400, 70));
305 line.SetStartPoint(new Point(220, 25 ) );
306 line.SetEndPoint(new Point(370,60) );
307 line.SetStartStyle(Line.EndingStyle.e_Butt);
308 line.SetEndStyle(Line.EndingStyle.e_OpenArrow);
309 line.SetColor(new ColorPt(0, 0, 1), 3);
310 line.SetContents("Regular Caption");
311 line.SetShowCaption(true);
312 line.SetCaptionPosition( Line.CapPos.e_Top );
313 line.RefreshAppearance();
314 page.AnnotPushBack(line);
315 }
316 {
317 Line line=Line.Create(doc, new Rect(200, 70, 400, 130));
318 line.SetStartPoint(new Point(220, 111 ) );
319 line.SetEndPoint(new Point(370,78) );
320 line.SetStartStyle(Line.EndingStyle.e_Circle);
321 line.SetEndStyle(Line.EndingStyle.e_Diamond);
322 line.SetContents("Circle to Diamond");
323 line.SetColor(new ColorPt(0, 0, 1), 3);
324 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
325 line.SetShowCaption(true);
326 line.SetCaptionPosition( Line.CapPos.e_Top );
327 line.RefreshAppearance();
328 page.AnnotPushBack(line);
329 }
330 {
331 Line line=Line.Create(doc, new Rect(10, 100, 160, 200));
332 line.SetStartPoint(new Point(15, 110 ) );
333 line.SetEndPoint(new Point(150, 190) );
334 line.SetStartStyle(Line.EndingStyle.e_Slash);
335 line.SetEndStyle(Line.EndingStyle.e_ClosedArrow);
336 line.SetContents("Slash to CArrow");
337 line.SetColor(new ColorPt(1, 0, 0), 3);
338 line.SetInteriorColor(new ColorPt(0, 1, 1), 3);
339 line.SetShowCaption(true);
340 line.SetCaptionPosition( Line.CapPos.e_Top );
341 line.RefreshAppearance();
342 page.AnnotPushBack(line);
343 }
344 {
345 Line line=Line.Create(doc, new Rect( 270, 270, 570, 433 ));
346 line.SetStartPoint(new Point(300, 400 ) );
347 line.SetEndPoint(new Point(550, 300) );
348 line.SetStartStyle(Line.EndingStyle.e_RClosedArrow);
349 line.SetEndStyle(Line.EndingStyle.e_ROpenArrow);
350 line.SetContents("ROpen & RClosed arrows");
351 line.SetColor(new ColorPt(0, 0, 1), 3);
352 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
353 line.SetShowCaption(true);
354 line.SetCaptionPosition( Line.CapPos.e_Top );
355 line.RefreshAppearance();
356 page.AnnotPushBack(line);
357 }
358 {
359 Line line=Line.Create(doc, new Rect( 195, 395, 205, 505 ));
360 line.SetStartPoint(new Point(200, 400 ) );
361 line.SetEndPoint(new Point(200, 500) );
362 line.RefreshAppearance();
363 page.AnnotPushBack(line);
364 }
365 {
366 Line line=Line.Create(doc, new Rect( 55, 299, 150, 301 ));
367 line.SetStartPoint(new Point(55, 300 ) );
368 line.SetEndPoint(new Point(155, 300) );
369 line.SetStartStyle(Line.EndingStyle.e_Circle);
370 line.SetEndStyle(Line.EndingStyle.e_Circle);
371 line.SetContents("Caption that's longer than its line.");
372 line.SetColor(new ColorPt(1, 0, 1), 3);
373 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
374 line.SetShowCaption(true);
375 line.SetCaptionPosition( Line.CapPos.e_Top );
376 line.RefreshAppearance();
377 page.AnnotPushBack(line);
378 }
379 {
380 Line line=Line.Create(doc, new Rect( 300, 200, 390, 234 ));
381 line.SetStartPoint(new Point(310, 210 ) );
382 line.SetEndPoint(new Point(380, 220) );
383 line.SetColor(new ColorPt(0, 0, 0), 3);
384 line.RefreshAppearance();
385 page.AnnotPushBack(line);
386 }
387
388 Page page3 = doc.PageCreate(new Rect(0, 0, 600, 600));
389 ew.Begin(page3); // begin writing to the page
390 ew.End(); // save changes to the current page
391 doc.PagePushBack(page3);
392 {
393 Circle circle = Circle.Create(doc, new Rect( 300, 300, 390, 350 ));
394 circle.SetColor(new ColorPt(0, 0, 0), 3);
395 circle.RefreshAppearance();
396 page3.AnnotPushBack(circle);
397 }
398 {
399 Circle circle= Circle.Create(doc, new Rect( 100, 100, 200, 200 ));
400 circle.SetColor(new ColorPt(0, 1, 0), 3);
401 circle.SetInteriorColor(new ColorPt(0, 0, 1), 3);
402 double[] dash = new double[2];
403 dash[0]=2;dash[1]=4;
404 circle.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 3, 0, 0, dash ) );
405 circle.SetPadding( new Rect(2,2,2,2) );
406 circle.RefreshAppearance();
407 page3.AnnotPushBack(circle);
408 }
409 {
410 Square sq = Square.Create( doc, new Rect(10,200, 80, 300 ) );
411 sq.SetColor(new ColorPt(0, 0, 0), 3);
412 sq.RefreshAppearance();
413 page3.AnnotPushBack( sq );
414 }
415 {
416 Square sq = Square.Create( doc, new Rect(500,200, 580, 300 ) );
417 sq.SetColor(new ColorPt(1, 0, 0), 3);
418 sq.SetInteriorColor(new ColorPt(0, 1, 1), 3);
419 double[] dash = new double[2];
420 dash[0]=4;dash[1]=2;
421 sq.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 6, 0, 0, dash ) );
422 sq.SetPadding( new Rect(4,4,4,4) );
423 sq.RefreshAppearance();
424 page3.AnnotPushBack( sq );
425 }
426 {
427 Polygon poly = Polygon.Create(doc, new Rect(5, 500, 125, 590));
428 poly.SetColor(new ColorPt(1, 0, 0), 3);
429 poly.SetInteriorColor(new ColorPt(1, 1, 0), 3);
430 poly.SetVertex(0, new Point(12,510) );
431 poly.SetVertex(1, new Point(100,510) );
432 poly.SetVertex(2, new Point(100,555) );
433 poly.SetVertex(3, new Point(35,544) );
434 poly.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 4, 0, 0 ) );
435 poly.SetPadding( new Rect(4,4,4,4) );
436 poly.RefreshAppearance();
437 page3.AnnotPushBack( poly );
438 }
439 {
440 PolyLine poly = PolyLine.Create(doc, new Rect(400, 10, 500, 90));
441 poly.SetColor(new ColorPt(1, 0, 0), 3);
442 poly.SetInteriorColor(new ColorPt(0, 1, 0), 3);
443 poly.SetVertex(0, new Point(405,20) );
444 poly.SetVertex(1, new Point(440,40) );
445 poly.SetVertex(2, new Point(410,60) );
446 poly.SetVertex(3, new Point(470,80) );
447 poly.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 2, 0, 0 ) );
448 poly.SetPadding( new Rect(4,4,4,4) );
449 poly.SetStartStyle( Line.EndingStyle.e_RClosedArrow );
450 poly.SetEndStyle( Line.EndingStyle.e_ClosedArrow );
451 poly.RefreshAppearance();
452 page3.AnnotPushBack( poly );
453 }
454 {
455 Link lk = Link.Create( doc, new Rect(5,5,55,24) );
456 //lk.SetColor( ColorPt(0,1,0), 3 );
457 lk.RefreshAppearance();
458 page3.AnnotPushBack( lk );
459 }
460
461
462 Page page4 = doc.PageCreate(new Rect(0, 0, 600, 600));
463 ew.Begin(page4); // begin writing to the page
464 ew.End(); // save changes to the current page
465 doc.PagePushBack(page4);
466
467 {
468 ew.Begin( page4 );
469 Font font = Font.Create(doc, Font.StandardType1Font.e_helvetica);
470 element = eb.CreateTextBegin( font, 16 );
471 element.SetPathFill(true);
472 ew.WriteElement(element);
473 element = eb.CreateTextRun( "Some random text on the page", font, 16 );
474 element.SetTextMatrix(1, 0, 0, 1, 100, 500 );
475 ew.WriteElement(element);
476 ew.WriteElement( eb.CreateTextEnd() );
477 ew.End();
478 }
479 {
480 Highlight hl = Highlight.Create( doc, new Rect(100,490,150,515) );
481 hl.SetColor(new ColorPt(0,1,0), 3 );
482 hl.RefreshAppearance();
483 page4.AnnotPushBack( hl );
484 }
485 {
486 Squiggly sq = Squiggly.Create( doc, new Rect(100,450,250,600) );
487 //sq.SetColor( ColorPt(1,0,0), 3 );
488 sq.SetQuadPoint( 0, new QuadPoint(new Point( 122,455), new Point(240, 545), new Point(230, 595), new Point(101,500 ) ) );
489 sq.RefreshAppearance();
490 page4.AnnotPushBack( sq );
491 }
492 {
493 Caret cr = Caret.Create( doc, new Rect(100,40,129,69) );
494 cr.SetColor( new ColorPt(0,0,1), 3 );
495 cr.SetSymbol( "P" );
496 cr.RefreshAppearance();
497 page4.AnnotPushBack( cr );
498 }
499
500
501 Page page5 = doc.PageCreate(new Rect(0, 0, 600, 600));
502 ew.Begin(page5); // begin writing to the page
503 ew.End(); // save changes to the current page
504 doc.PagePushBack(page5);
505 Page page6 = doc.PageCreate(new Rect(0, 0, 600, 600));
506 ew.Begin(page6); // begin writing to the page
507 ew.End(); // save changes to the current page
508 doc.PagePushBack(page6);
509
510 {
511 Text txt = Text.Create( doc, new Rect( 10, 20, 30, 40 ) );
512 txt.SetIcon( "UserIcon" );
513 txt.SetContents( "User defined icon, unrecognized by appearance generator" );
514 txt.SetColor(new ColorPt(0,1,0) );
515 txt.RefreshAppearance();
516 page6.AnnotPushBack( txt );
517 }
518 {
519 Ink ink = Ink.Create( doc, new Rect( 100, 400, 200, 550 ) );
520 ink.SetColor(new ColorPt(0,0,1) );
521 ink.SetPoint( 1, 3, new Point( 220, 505) );
522 ink.SetPoint( 1, 0, new Point( 100, 490) );
523 ink.SetPoint( 0, 1, new Point( 120, 410) );
524 ink.SetPoint( 0, 0, new Point( 100, 400) );
525 ink.SetPoint( 1, 2, new Point( 180, 490) );
526 ink.SetPoint( 1, 1, new Point( 140, 440) );
527 ink.SetBorderStyle( new Annot.BorderStyle( Annot.BorderStyle.Style.e_solid, 3, 0, 0 ) );
528 ink.RefreshAppearance();
529 page6.AnnotPushBack( ink );
530 }
531
532
533 Page page7 = doc.PageCreate(new Rect(0, 0, 600, 600));
534 ew.Begin(page7); // begin writing to the page
535 ew.End(); // save changes to the current page
536 doc.PagePushBack(page7);
537
538 {
539 Sound snd = Sound.Create( doc, new Rect( 100, 500, 120, 520 ) );
540 snd.SetColor( new ColorPt(1,1,0) );
541 snd.SetIcon(Sound.Icon.e_Speaker );
542 snd.RefreshAppearance();
543 page7.AnnotPushBack( snd );
544 }
545 {
546 Sound snd = Sound.Create( doc, new Rect( 200, 500, 220, 520 ) );
547 snd.SetColor(new ColorPt(1,1,0) );
548 snd.SetIcon(Sound.Icon.e_Mic );
549 snd.RefreshAppearance();
550 page7.AnnotPushBack( snd );
551 }
552
553 Page page8 = doc.PageCreate(new Rect(0, 0, 600, 600));
554 ew.Begin(page8); // begin writing to the page
555 ew.End(); // save changes to the current page
556 doc.PagePushBack(page8);
557
558 for( int ipage =0; ipage < 2; ++ipage ) {
559 double px = 5, py = 520;
560 for (RubberStamp.Icon istamp = RubberStamp.Icon.e_Approved;
561 istamp <= RubberStamp.Icon.e_Draft;
562 istamp = (RubberStamp.Icon) ( (int)(istamp) + 1 ) ) {
563 RubberStamp stmp = RubberStamp.Create( doc, new Rect(1,1,100,100) );
564 stmp.SetIcon( istamp );
565 stmp.SetContents(stmp.GetIconName());
566 stmp.SetRect(new Rect(px, py, px+100, py+25 ) );
567 py -= 100;
568 if( py < 0 ) {
569 py = 520;
570 px += 200;
571 }
572 if( ipage == 0 ){
573 //page7.AnnotPushBack( st );
574 ;
575 }
576 else {
577 page8.AnnotPushBack(stmp);
578 stmp.RefreshAppearance();
579 }
580 }
581 }
582 RubberStamp st = RubberStamp.Create( doc, new Rect(400,5,550,45) );
583 st.SetIcon( "UserStamp" );
584 st.SetContents( "User defined stamp" );
585 page8.AnnotPushBack( st );
586 st.RefreshAppearance();
587
588
589
590 }
591
592
593 // Relative path to the folder containing test files.
594 const string input_path = "TestFiles/";
595
596 /// <summary>
597 /// The main entry point for the application.
598 /// </summary>
599 [Test]
600 public static void Sample()
601 {
602
603 try
604 {
605 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "numbered.pdf")))
606 {
607 doc.InitSecurityHandler();
608
609 // An example of using SDF/Cos API to add any type of annotations.
610 AnnotationLowLevelAPI(doc);
611 doc.Save(Utils.CreateExternalFile("annotation_test1.pdf"), SDFDoc.SaveOptions.e_linearized);
612 System.Console.WriteLine("Done. Results saved in annotation_test1.pdf");
613
614 // An example of using the high-level PDFNet API to read existing annotations,
615 // to edit existing annotations, and to create new annotation from scratch.
616 AnnotationHighLevelAPI(doc);
617 doc.Save(Utils.CreateExternalFile("annotation_test2.pdf"), SDFDoc.SaveOptions.e_linearized);
618 System.Console.WriteLine("Done. Results saved in annotation_test2.pdf");
619 }
620
621 // an example of creating various annotations in a brand new document
622 using (PDFDoc doc1 = new PDFDoc())
623 {
624 CreateTestAnnots(doc1);
625 doc1.Save(Utils.CreateExternalFile("new_annot_test_api.pdf"), SDFDoc.SaveOptions.e_linearized);
626 System.Console.WriteLine("Saved new_annot_test_api.pdf");
627 }
628 }
629 catch (PDFNetException e)
630 {
631 System.Console.WriteLine(e.Message);
632 Assert.True(false);
633 }
634 }
635 }
636}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales