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 UWP SDK and PDF Annotation Library.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14using pdftron.PDF.Annots;
15
16namespace PDFNetSamples
17{
18 //-----------------------------------------------------------------------------------------
19 // This sample shows how to add a text annotation to an existing page and how to create
20 // link annotations between several pages.
21 //-----------------------------------------------------------------------------------------
22 public sealed class AnnotationTest : Sample
23 {
24 public AnnotationTest() :
25 base("Annotation", "This sample shows how to add a text annotation to an existing page and how to create link annotations between several pages.")
26 {
27 }
28
29 public override IAsyncAction RunAsync()
30 {
31 return Task.Run(new System.Action(async() => {
32 WriteLine("--------------------------------");
33 WriteLine("Starting Annotation Test...");
34 WriteLine("--------------------------------\n");
35
36 try
37 {
38 String input_file_path = Path.Combine(InputPath, "numbered.pdf");
39 WriteLine("Opening input file " + input_file_path);
40
41 PDFDoc doc = new PDFDoc(input_file_path);
42 doc.InitSecurityHandler();
43
44 // Example 1: An example of using SDF/Cos API to add any type of annotations.
45 await AnnotationLowLevelAPI(doc);
46
47 /* Example 2: An example of using the high-level PDFNet API to read existing annotations,
48 * to edit existing annotations, and to create new annotation from scratch. */
49 await AnnotationHighLevelAPI(doc);
50
51 doc.Destroy();
52
53 // Example 3: an example of creating various annotations in a brand new document
54 PDFDoc pdfDoc = new PDFDoc();
55 await CreateTestAnnots(pdfDoc);
56 }
57 catch (Exception e)
58 {
59 WriteLine(GetExceptionMessage(e));
60 }
61
62 WriteLine("\n--------------------------------");
63 WriteLine("Done Annotation Test.");
64 WriteLine("--------------------------------\n");
65 })).AsAsyncAction();
66 }
67
68 /// <summary>
69 /// An example of using SDF/Cos API to add any type of annotations.
70 /// </summary>
71 async Task AnnotationLowLevelAPI(PDFDoc doc)
72 {
73 pdftron.PDF.Page page = doc.GetPage(1);
74
75 Obj annots = page.GetAnnots();
76 if (annots == null)
77 {
78 // If there are no annotations, create a new annotation
79 // array for the page.
80 annots = doc.CreateIndirectArray();
81 page.GetSDFObj().Put("Annots", annots);
82 }
83
84 // Create the Text annotation
85 Obj text_annot = doc.CreateIndirectDict();
86 text_annot.PutName("Subtype", "Text");
87 text_annot.PutBool("Open", true);
88 text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse.");
89 text_annot.PutRect("Rect", 266, 116, 430, 204);
90
91 // Insert the annotation in the page annotation array
92 annots.PushBack(text_annot);
93
94 // Create a Link annotation
95 Obj link1 = doc.CreateIndirectDict();
96 link1.PutName("Subtype", "Link");
97 pdftron.PDF.Page p = doc.GetPage(2);
98 Destination dest = Destination.CreateFit(p);
99 link1.Put("Dest", dest.GetSDFObj());
100 link1.PutRect("Rect", 85, 705, 503, 661);
101 annots.PushBack(link1);
102
103 // Create another Link annotation
104 Obj link2 = doc.CreateIndirectDict();
105 link2.PutName("Subtype", "Link");
106 Destination dest2 = Destination.CreateFit(doc.GetPage(3));
107 link2.Put("Dest", dest2.GetSDFObj());
108 link2.PutRect("Rect", 85, 638, 503, 594);
109 annots.PushBack(link2);
110
111 // Note that PDFNet APi can be used to modify existing annotations.
112 // In the following example we will modify the second link annotation
113 // (link2) so that it points to the 10th page. We also use a different
114 // destination page fit type.
115
116 link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj());
117
118 // Create a third link annotation with a hyperlink action (all other
119 // annotation types can be created in a similar way)
120 Obj link3 = doc.CreateIndirectDict();
121 link3.PutName("Subtype", "Link");
122 link3.PutRect("Rect", 85, 570, 503, 524);
123
124 // Create a URI action
125 Obj action = link3.PutDict("A");
126 action.PutName("S", "URI");
127 action.PutString("URI", "http://www.pdftron.com");
128 annots.PushBack(link3);
129
130 String output_file_path = Path.Combine(OutputPath, "annotation_test1.pdf");
131 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
132
133 WriteLine("Done. Results saved in " + output_file_path);
134 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
135 }
136
137 /// <summary>
138 /// An example of using the high-level PDFNet API to read existing annotations,
139 /// to edit existing annotations, and to create new annotation from scratch.
140 /// </summary>
141 async Task AnnotationHighLevelAPI(PDFDoc doc)
142 {
143 // The following code snippet traverses all annotations in the document
144 WriteLine("Traversing all annotations in the document...");
145
146 int page_num = 1;
147 PageIterator itr = doc.GetPageIterator();
148 for (; itr.HasNext(); itr.Next())
149 {
150 WriteLine(String.Format("Page " + page_num++ + ": "));
151
152 pdftron.PDF.Page page = itr.Current();
153 int num_annots = page.GetNumAnnots();
154 for (int i = 0; i < num_annots; ++i)
155 {
156 IAnnot annot = page.GetAnnot(i);
157 if (annot.IsValid() == false) continue;
158 WriteLine("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName());
159
160 pdftron.PDF.Rect bbox = annot.GetRect();
161 WriteLine(" Position: " + bbox.x1
162 + ", " + bbox.y1
163 + ", " + bbox.x2
164 + ", " + bbox.y2);
165
166 switch (annot.GetAnnotType())
167 {
168 case AnnotType.e_Link:
169 pdftron.PDF.Annots.Link lk = (pdftron.PDF.Annots.Link)annot;
170 pdftron.PDF.Action action = lk.GetAction();
171 if (action.GetType() == pdftron.PDF.ActionType.e_GoTo)
172 {
173 Destination dest = action.GetDest();
174 if (dest.IsValid() == false)
175 WriteLine(" Not a valid destination.");
176 else
177 {
178 int pg_num = dest.GetPage().GetIndex();
179 WriteLine(" Links to: page number " + pg_num + " in this document");
180 }
181 }
182 else if (action.GetType() == pdftron.PDF.ActionType.e_URI)
183 {
184 System.String uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText();
185 WriteLine(" Links to: " + uri);
186 }
187 break;
188
189 case AnnotType.e_Widget:
190 case AnnotType.e_FileAttachment:
191 break;
192 default:
193 break;
194 }
195
196 }
197 }
198
199 // Use the high-level API to create new annotations.
200 pdftron.PDF.Page first_page = doc.GetPage(1);
201
202 // Create a hyperlink...
203 Link hyperlink = Link.Create(
204 doc.GetSDFDoc(),
205 new pdftron.PDF.Rect(85, 570, 503, 524),
206 pdftron.PDF.Action.CreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"));
207
208 first_page.AnnotPushBack(hyperlink);
209
210 // Create an intra-document link...
211 pdftron.PDF.Action goto_page_3 = pdftron.PDF.Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0));
212 pdftron.PDF.Annots.Link link = pdftron.PDF.Annots.Link.Create(
213 doc.GetSDFDoc(),
214 new pdftron.PDF.Rect(85, 458, 503, 502),
215 goto_page_3);
216
217 // Set the annotation border width to 3 points...
218 link.SetBorderStyle(new pdftron.PDF.AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 3));
219 link.SetColor(new pdftron.PDF.ColorPt(0, 0, 1), 3);
220
221 // Add the new annotation to the first page
222 first_page.AnnotPushBack(link);
223
224 // Create a stamp annotation ...
225 pdftron.PDF.Annots.RubberStamp stamp = pdftron.PDF.Annots.RubberStamp.Create(
226 doc.GetSDFDoc(),
227 new pdftron.PDF.Rect(30, 30, 300, 200));
228
229 stamp.SetIcon("Draft");
230 first_page.AnnotPushBack(stamp);
231
232 // Create a file attachment annotation (embed the 'peppers.jpg').
233 pdftron.PDF.Annots.FileAttachment file_attach = pdftron.PDF.Annots.FileAttachment.Create(
234 doc.GetSDFDoc(),
235 new pdftron.PDF.Rect(80, 280, 108, 320),
236 Path.Combine(InputPath, "peppers.jpg"));
237
238 first_page.AnnotPushBack(file_attach);
239
240 //Create an ink annotation.
241 pdftron.PDF.Annots.Ink ink = pdftron.PDF.Annots.Ink.Create(
242 doc.GetSDFDoc(),
243 new pdftron.PDF.Rect(110, 10, 300, 200));
244
245 pdftron.PDF.Point pt3 = new pdftron.PDF.Point();
246 pt3.x = 110; pt3.y = 10;
247 ink.SetPoint(0, 0, pt3);
248 pt3.x = 150; pt3.y = 50;
249 ink.SetPoint(0, 1, pt3);
250 pt3.x = 190; pt3.y = 60;
251 ink.SetPoint(0, 2, pt3);
252 pt3.x = 180; pt3.y = 90;
253 ink.SetPoint(1, 0, pt3);
254 pt3.x = 190; pt3.y = 95;
255 ink.SetPoint(1, 1, pt3);
256 pt3.x = 200; pt3.y = 100;
257 ink.SetPoint(1, 2, pt3);
258 pt3.x = 166; pt3.y = 86;
259 ink.SetPoint(2, 0, pt3);
260 pt3.x = 196; pt3.y = 96;
261 ink.SetPoint(2, 1, pt3);
262 pt3.x = 221; pt3.y = 121;
263 ink.SetPoint(2, 2, pt3);
264 pt3.x = 288; pt3.y = 188;
265 ink.SetPoint(2, 3, pt3);
266 ink.SetColor(new ColorPt(0, 1, 1), 3);
267 first_page.AnnotPushBack(ink);
268
269 string output_file_path = Path.Combine(OutputPath, "annotation_test2.pdf");
270 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
271
272 WriteLine("Done. Results saved in " + output_file_path);
273 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
274 }
275
276 /// <summary>
277 /// An example of creating various annotations in a brand new document
278 /// </summary>
279 async Task CreateTestAnnots(PDFDoc doc)
280 {
281 ElementWriter ew = new ElementWriter();
282 ElementBuilder eb = new ElementBuilder();
283 Element element;
284
285 Page first_page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
286 doc.PagePushBack(first_page);
287 ew.Begin(first_page, ElementWriterWriteMode.e_overlay, false); // begin writing to this page
288 ew.End(); // save changes to the current page
289
290 //
291 // Test of a free text annotation.
292 //
293 {
294 FreeText txtannot = FreeText.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(10, 400, 160, 570));
295 txtannot.SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
296 "\n\nAha!\n\nAnd there was much rejoicing!");
297 txtannot.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 1, 10, 20));
298 txtannot.SetQuaddingFormat(0);
299 first_page.AnnotPushBack(txtannot);
300 txtannot.RefreshAppearance();
301 }
302 {
303 FreeText txtannot = FreeText.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 100, 350, 500));
304 txtannot.SetContentRect(new pdftron.PDF.Rect(200, 200, 350, 500));
305 txtannot.SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
306 "\n\nAha!\n\nAnd there was much rejoicing!");
307 txtannot.SetCalloutLinePoints(
308 new pdftron.PDF.Point(200, 300),
309 new pdftron.PDF.Point(150, 290),
310 new pdftron.PDF.Point(110, 110));
311
312 txtannot.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 1, 10, 20));
313 txtannot.SetEndingStyle(LineEndingStyle.e_ClosedArrow);
314 txtannot.SetColor(new ColorPt(0, 1, 0));
315 txtannot.SetQuaddingFormat(1);
316 first_page.AnnotPushBack(txtannot);
317 txtannot.RefreshAppearance();
318 }
319 {
320 FreeText txtannot = FreeText.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(400, 10, 550, 400));
321 txtannot.SetContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
322 "\n\nAha!\n\nAnd there was much rejoicing!");
323 txtannot.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 1, 10, 20));
324 txtannot.SetColor(new ColorPt(0, 0, 1));
325 txtannot.SetOpacity(0.2);
326 txtannot.SetQuaddingFormat(2);
327 first_page.AnnotPushBack(txtannot);
328 txtannot.RefreshAppearance();
329 }
330
331 Page page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
332 doc.PagePushBack(page);
333 ew.Begin(page, ElementWriterWriteMode.e_overlay, false); // begin writing to this page
334 eb.Reset(); // Reset the GState to default
335 ew.End(); // save changes to the current page
336
337 {
338 //Create a Line annotation...
339 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(250, 250, 400, 400));
340 line.SetStartPoint(new pdftron.PDF.Point(350, 270));
341 line.SetEndPoint(new pdftron.PDF.Point(260, 370));
342 line.SetStartStyle(LineEndingStyle.e_Square);
343 line.SetEndStyle(LineEndingStyle.e_Circle);
344 line.SetColor(new ColorPt(.3, .5, 0), 3);
345 line.SetContents("Dashed Captioned");
346 line.SetShowCaption(true);
347 line.SetCaptionPosition(LineCapPos.e_Top);
348 line.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_dashed, 2, 0, 0));
349 line.RefreshAppearance();
350 page.AnnotPushBack(line);
351 }
352 {
353 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(347, 377, 600, 600));
354 line.SetStartPoint(new pdftron.PDF.Point(385, 410));
355 line.SetEndPoint(new pdftron.PDF.Point(540, 555));
356 line.SetStartStyle(LineEndingStyle.e_Circle);
357 line.SetEndStyle(LineEndingStyle.e_OpenArrow);
358 line.SetColor(new ColorPt(1, 0, 0), 3);
359 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
360 line.SetContents("Inline Caption");
361 line.SetShowCaption(true);
362 line.SetCaptionPosition(LineCapPos.e_Inline);
363 line.SetLeaderLineExtensionLength(-4);
364 line.SetLeaderLineLength(-12);
365 line.SetLeaderLineOffset(2);
366 line.RefreshAppearance();
367 page.AnnotPushBack(line);
368 }
369 {
370 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(10, 400, 200, 600));
371 line.SetStartPoint(new pdftron.PDF.Point(25, 426));
372 line.SetEndPoint(new pdftron.PDF.Point(180, 555));
373 line.SetStartStyle(LineEndingStyle.e_Circle);
374 line.SetEndStyle(LineEndingStyle.e_Square);
375 line.SetColor(new ColorPt(0, 0, 1), 3);
376 line.SetInteriorColor(new ColorPt(1, 0, 0), 3);
377 line.SetContents("Offset Caption");
378 line.SetShowCaption(true);
379 line.SetCaptionPosition(LineCapPos.e_Top);
380 line.SetTextHOffset(-60);
381 line.SetTextVOffset(10);
382 line.RefreshAppearance();
383 page.AnnotPushBack(line);
384 }
385 {
386 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(200, 10, 400, 70));
387 line.SetStartPoint(new pdftron.PDF.Point(220, 25));
388 line.SetEndPoint(new pdftron.PDF.Point(370, 60));
389 line.SetStartStyle(LineEndingStyle.e_Butt);
390 line.SetEndStyle(LineEndingStyle.e_OpenArrow);
391 line.SetColor(new ColorPt(0, 0, 1), 3);
392 line.SetContents("Regular Caption");
393 line.SetShowCaption(true);
394 line.SetCaptionPosition(LineCapPos.e_Top);
395 line.RefreshAppearance();
396 page.AnnotPushBack(line);
397 }
398 {
399 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(200, 70, 400, 130));
400 line.SetStartPoint(new pdftron.PDF.Point(220, 111));
401 line.SetEndPoint(new pdftron.PDF.Point(370, 78));
402 line.SetStartStyle(LineEndingStyle.e_Circle);
403 line.SetEndStyle(LineEndingStyle.e_Diamond);
404 line.SetContents("Circle to Diamond");
405 line.SetColor(new ColorPt(0, 0, 1), 3);
406 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
407 line.SetShowCaption(true);
408 line.SetCaptionPosition(LineCapPos.e_Top);
409 line.RefreshAppearance();
410 page.AnnotPushBack(line);
411 }
412 {
413 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(10, 100, 160, 200));
414 line.SetStartPoint(new pdftron.PDF.Point(15, 110));
415 line.SetEndPoint(new pdftron.PDF.Point(150, 190));
416 line.SetStartStyle(LineEndingStyle.e_Slash);
417 line.SetEndStyle(LineEndingStyle.e_ClosedArrow);
418 line.SetContents("Slash to CArrow");
419 line.SetColor(new ColorPt(1, 0, 0), 3);
420 line.SetInteriorColor(new ColorPt(0, 1, 1), 3);
421 line.SetShowCaption(true);
422 line.SetCaptionPosition(LineCapPos.e_Top);
423 line.RefreshAppearance();
424 page.AnnotPushBack(line);
425 }
426 {
427 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(270, 270, 570, 433));
428 line.SetStartPoint(new pdftron.PDF.Point(300, 400));
429 line.SetEndPoint(new pdftron.PDF.Point(550, 300));
430 line.SetStartStyle(LineEndingStyle.e_RClosedArrow);
431 line.SetEndStyle(LineEndingStyle.e_ROpenArrow);
432 line.SetContents("ROpen & RClosed arrows");
433 line.SetColor(new ColorPt(0, 0, 1), 3);
434 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
435 line.SetShowCaption(true);
436 line.SetCaptionPosition(LineCapPos.e_Top);
437 line.RefreshAppearance();
438 page.AnnotPushBack(line);
439 }
440 {
441 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(195, 395, 205, 505));
442 line.SetStartPoint(new pdftron.PDF.Point(200, 400));
443 line.SetEndPoint(new pdftron.PDF.Point(200, 500));
444 line.RefreshAppearance();
445 page.AnnotPushBack(line);
446 }
447 {
448 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(55, 299, 150, 301));
449 line.SetStartPoint(new pdftron.PDF.Point(55, 300));
450 line.SetEndPoint(new pdftron.PDF.Point(155, 300));
451 line.SetStartStyle(LineEndingStyle.e_Circle);
452 line.SetEndStyle(LineEndingStyle.e_Circle);
453 line.SetContents("Caption that's longer than its line.");
454 line.SetColor(new ColorPt(1, 0, 1), 3);
455 line.SetInteriorColor(new ColorPt(0, 1, 0), 3);
456 line.SetShowCaption(true);
457 line.SetCaptionPosition(LineCapPos.e_Top);
458 line.RefreshAppearance();
459 page.AnnotPushBack(line);
460 }
461 {
462 Line line = Line.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(300, 200, 390, 234));
463 line.SetStartPoint(new pdftron.PDF.Point(310, 210));
464 line.SetEndPoint(new pdftron.PDF.Point(380, 220));
465 line.SetColor(new ColorPt(0, 0, 0), 3);
466 line.RefreshAppearance();
467 page.AnnotPushBack(line);
468 }
469
470 Page page3 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
471 ew.Begin(page3); // begin writing to the page
472 ew.End(); // save changes to the current page
473 doc.PagePushBack(page3);
474
475 {
476 Circle circle = Circle.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(300, 300, 390, 350));
477 circle.SetColor(new ColorPt(0, 0, 0), 3);
478 circle.RefreshAppearance();
479 page3.AnnotPushBack(circle);
480 }
481 {
482 Circle circle = Circle.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 100, 200, 200));
483 circle.SetColor(new ColorPt(0, 1, 0), 3);
484 circle.SetInteriorColor(new ColorPt(0, 0, 1), 3);
485 double[] dash = new double[2];
486 dash[0] = 2; dash[1] = 4;
487 circle.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_dashed, 3, 0, 0, dash));
488 circle.SetPadding(new pdftron.PDF.Rect(2, 2, 2, 2));
489 circle.RefreshAppearance();
490 page3.AnnotPushBack(circle);
491 }
492 {
493 Square sq = Square.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(10, 200, 80, 300));
494 sq.SetColor(new ColorPt(0, 0, 0), 3); sq.RefreshAppearance();
495 page3.AnnotPushBack(sq);
496 }
497 {
498 Square sq = Square.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(500, 200, 580, 300));
499 sq.SetColor(new ColorPt(1, 0, 0), 3);
500 sq.SetInteriorColor(new ColorPt(0, 1, 1), 3);
501 double[] dash = new double[2];
502 dash[0] = 4; dash[1] = 2;
503 sq.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_dashed, 6, 0, 0, dash));
504 sq.SetPadding(new pdftron.PDF.Rect(4, 4, 4, 4));
505 sq.RefreshAppearance();
506 page3.AnnotPushBack(sq);
507 }
508 {
509 Polygon poly = Polygon.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(5, 500, 125, 590));
510 poly.SetColor(new ColorPt(1, 0, 0), 3);
511 poly.SetInteriorColor(new ColorPt(1, 1, 0), 3);
512 poly.SetVertex(0, new pdftron.PDF.Point(12, 510));
513 poly.SetVertex(1, new pdftron.PDF.Point(100, 510));
514 poly.SetVertex(2, new pdftron.PDF.Point(100, 555));
515 poly.SetVertex(3, new pdftron.PDF.Point(35, 544));
516 poly.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 4, 0, 0));
517 poly.SetPadding(new pdftron.PDF.Rect(4, 4, 4, 4));
518 poly.RefreshAppearance();
519 page3.AnnotPushBack(poly);
520 }
521 {
522 PolyLine poly = PolyLine.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(400, 10, 500, 90));
523 poly.SetColor(new ColorPt(1, 0, 0), 3);
524 poly.SetInteriorColor(new ColorPt(0, 1, 0), 3);
525 poly.SetVertex(0, new pdftron.PDF.Point(405, 20));
526 poly.SetVertex(1, new pdftron.PDF.Point(440, 40));
527 poly.SetVertex(2, new pdftron.PDF.Point(410, 60));
528 poly.SetVertex(3, new pdftron.PDF.Point(470, 80));
529 poly.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 2, 0, 0));
530 poly.SetPadding(new pdftron.PDF.Rect(4, 4, 4, 4));
531 poly.SetStartStyle(LineEndingStyle.e_RClosedArrow);
532 poly.SetEndStyle(LineEndingStyle.e_RClosedArrow);
533 poly.RefreshAppearance();
534 page3.AnnotPushBack(poly);
535 }
536 {
537 Link lk = Link.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(5, 5, 55, 24));
538 //lk.SetColor( ColorPt(0,1,0), 3 );
539 lk.RefreshAppearance();
540 page3.AnnotPushBack(lk);
541 }
542
543
544 Page page4 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
545 ew.Begin(page4); // begin writing to the page
546 ew.End(); // save changes to the current page
547 doc.PagePushBack(page4);
548
549 {
550 ew.Begin(page4);
551 Font font = Font.Create(doc, FontStandardType1Font.e_helvetica);
552 element = eb.CreateTextBegin(font, 16);
553 element.SetPathFill(true);
554 ew.WriteElement(element);
555 element = eb.CreateTextRun("Some random text on the page", font, 16);
556 element.SetTextMatrix(1, 0, 0, 1, 100, 500);
557 ew.WriteElement(element);
558 ew.WriteElement(eb.CreateTextEnd());
559 ew.End();
560 }
561 {
562 Highlight hl = Highlight.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 490, 150, 515));
563 hl.SetColor(new ColorPt(0, 1, 0), 3);
564 hl.RefreshAppearance();
565 page4.AnnotPushBack(hl);
566 }
567 {
568 Squiggly sq = Squiggly.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 450, 250, 600));
569 //sq.SetColor( ColorPt(1,0,0), 3 );
570 sq.SetQuadPoint(0, new QuadPoint(
571 new pdftron.PDF.Point(122, 455),
572 new pdftron.PDF.Point(240, 545),
573 new pdftron.PDF.Point(230, 595),
574 new pdftron.PDF.Point(101, 500)));
575 sq.RefreshAppearance();
576 page4.AnnotPushBack(sq);
577 }
578 {
579 Caret cr = Caret.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 40, 129, 69));
580 cr.SetColor(new ColorPt(0, 0, 1), 3);
581 cr.SetSymbol("P");
582 cr.RefreshAppearance();
583 page4.AnnotPushBack(cr);
584 }
585
586
587 Page page5 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
588 ew.Begin(page5); // begin writing to the page
589 ew.End(); // save changes to the current page
590 doc.PagePushBack(page5);
591 Page page6 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
592 ew.Begin(page6); // begin writing to the page
593 ew.End(); // save changes to the current page
594 doc.PagePushBack(page6);
595
596 {
597 Text txt = Text.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(10, 20, 30, 40));
598 txt.SetIcon("UserIcon");
599 txt.SetContents("User defined icon, unrecognized by appearance generator");
600 txt.SetColor(new ColorPt(0, 1, 0));
601 txt.RefreshAppearance();
602 page6.AnnotPushBack(txt);
603 }
604 {
605 Ink ink = Ink.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 400, 200, 550));
606 ink.SetColor(new ColorPt(0, 0, 1));
607 ink.SetPoint(1, 3, new pdftron.PDF.Point(220, 505));
608 ink.SetPoint(1, 0, new pdftron.PDF.Point(100, 490));
609 ink.SetPoint(0, 1, new pdftron.PDF.Point(120, 410));
610 ink.SetPoint(0, 0, new pdftron.PDF.Point(100, 400));
611 ink.SetPoint(1, 2, new pdftron.PDF.Point(180, 490));
612 ink.SetPoint(1, 1, new pdftron.PDF.Point(140, 440));
613 ink.SetBorderStyle(new AnnotBorderStyle(AnnotBorderStyleStyle.e_solid, 3, 0, 0));
614 ink.RefreshAppearance();
615 page6.AnnotPushBack(ink);
616 }
617
618
619 Page page7 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
620 ew.Begin(page7); // begin writing to the page
621 ew.End(); // save changes to the current page
622 doc.PagePushBack(page7);
623
624 {
625 Sound snd = Sound.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(100, 500, 120, 520));
626 snd.SetColor(new ColorPt(1, 1, 0));
627 snd.SetIcon(SoundIcon.e_Speaker);
628 snd.RefreshAppearance();
629 page7.AnnotPushBack(snd);
630 }
631 {
632 Sound snd = Sound.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(200, 500, 220, 520));
633 snd.SetColor(new ColorPt(1, 1, 0));
634 snd.SetIcon(SoundIcon.e_Mic);
635 snd.RefreshAppearance();
636 page7.AnnotPushBack(snd);
637 }
638
639 Page page8 = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 600, 600));
640 ew.Begin(page8); // begin writing to the page
641 ew.End(); // save changes to the current page
642 doc.PagePushBack(page8);
643
644 for (int ipage = 0; ipage < 2; ++ipage)
645 {
646 double px = 5, py = 520;
647 for (RubberStampIcon istamp = RubberStampIcon.e_Approved;
648 istamp <= RubberStampIcon.e_Draft;
649 istamp = (RubberStampIcon)((int)(istamp) + 1))
650 {
651 RubberStamp stmp = RubberStamp.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(1, 1, 100, 100));
652 stmp.SetIcon(istamp);
653 stmp.SetContents(stmp.GetIconName());
654 stmp.SetRect(new pdftron.PDF.Rect(px, py, px + 100, py + 25));
655 py -= 100;
656 if (py < 0)
657 {
658 py = 520;
659 px += 200;
660 }
661 if (ipage == 0)
662 {
663 //page7.AnnotPushBack( st );
664 ;
665 }
666 else
667 {
668 page8.AnnotPushBack(stmp);
669 stmp.RefreshAppearance();
670 }
671 }
672 }
673 RubberStamp st = RubberStamp.Create(doc.GetSDFDoc(), new pdftron.PDF.Rect(400, 5, 550, 45));
674 st.SetIcon("UserStamp");
675 st.SetContents("User defined stamp");
676 page8.AnnotPushBack(st);
677 st.RefreshAppearance();
678
679 string output_file_path = Path.Combine(OutputPath, "new_annot_test_api.pdf");
680 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
681
682 WriteLine("Done. Results saved in " + output_file_path);
683 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
684 }
685 }
686}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales