Sample Java 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 Android SDK and PDF Annotation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.PDFNetException;
13import com.pdftron.pdf.Action;
14import com.pdftron.pdf.Annot;
15import com.pdftron.pdf.Annot.BorderStyle;
16import com.pdftron.pdf.ColorPt;
17import com.pdftron.pdf.Destination;
18import com.pdftron.pdf.Element;
19import com.pdftron.pdf.ElementBuilder;
20import com.pdftron.pdf.ElementWriter;
21import com.pdftron.pdf.FileSpec;
22import com.pdftron.pdf.Font;
23import com.pdftron.pdf.PDFDoc;
24import com.pdftron.pdf.Page;
25import com.pdftron.pdf.PageIterator;
26import com.pdftron.pdf.Point;
27import com.pdftron.pdf.QuadPoint;
28import com.pdftron.pdf.Rect;
29import com.pdftron.pdf.annots.Caret;
30import com.pdftron.pdf.annots.Circle;
31import com.pdftron.pdf.annots.FreeText;
32import com.pdftron.pdf.annots.Highlight;
33import com.pdftron.pdf.annots.Ink;
34import com.pdftron.pdf.annots.Line;
35import com.pdftron.pdf.annots.Link;
36import com.pdftron.pdf.annots.PolyLine;
37import com.pdftron.pdf.annots.Polygon;
38import com.pdftron.pdf.annots.RubberStamp;
39import com.pdftron.pdf.annots.Sound;
40import com.pdftron.pdf.annots.Square;
41import com.pdftron.pdf.annots.Squiggly;
42import com.pdftron.pdf.annots.Text;
43import com.pdftron.sdf.Obj;
44import com.pdftron.sdf.SDFDoc;
45
46import java.text.DecimalFormat;
47import java.util.ArrayList;
48
49public class AnnotationTest extends PDFNetSample {
50
51 private static OutputListener mOutputListener;
52
53 private static ArrayList<String> mFileList = new ArrayList<>();
54
55 public AnnotationTest() {
56 setTitle(R.string.sample_annotation_title);
57 setDescription(R.string.sample_annotation_description);
58 }
59
60 @Override
61 public void run(OutputListener outputListener) {
62 super.run(outputListener);
63 mOutputListener = outputListener;
64 mFileList.clear();
65 printHeader(outputListener);
66
67 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "numbered.pdf").getAbsolutePath()))) {
68 doc.initSecurityHandler();
69
70 // An example of using SDF/Cos API to add any type of annotations.
71 AnnotationLowLevelAPI(doc);
72 doc.save(Utils.createExternalFile("annotation_test1.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
73 mOutputListener.println("Done. Results saved in annotation_test1.pdf");
74
75 // An example of using the high-level PDFNet API to read existing annotations,
76 // to edit existing annotations, and to create new annotation from scratch.
77 AnnotationHighLevelAPI(doc);
78 doc.save(Utils.createExternalFile("annotation_test2.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
79 mOutputListener.println("Done. Results saved in annotation_test2.pdf");
80 } catch (Exception e) {
81 mOutputListener.printError(e.getStackTrace());
82 }
83
84 // an example of creating various annotations in a brand new document
85 try (PDFDoc doc1 = new PDFDoc()) {
86 CreateTestAnnots(doc1);
87 doc1.save(Utils.createExternalFile("new_annot_test_api.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
88 mOutputListener.println("Saved new_annot_test_api.pdf");
89 } catch (Exception e) {
90 mOutputListener.printError(e.getStackTrace());
91 }
92
93 for (String file : mFileList) {
94 addToFileList(file);
95 }
96 printFooter(outputListener);
97 }
98
99 public static final DecimalFormat format = new DecimalFormat("0.#");
100
101 static void AnnotationHighLevelAPI(PDFDoc doc) throws PDFNetException {
102 // The following code snippet traverses all annotations in the document
103 mOutputListener.println("Traversing all annotations in the document...");
104
105 int page_num = 1;
106 for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) {
107 mOutputListener.println("Page " + (page_num++) + ": ");
108
109 Page page = itr.next();
110 int num_annots = page.getNumAnnots();
111 for (int i = 0; i < num_annots; ++i) {
112 Annot annot = page.getAnnot(i);
113 if (annot.isValid() == false) continue;
114 mOutputListener.println("Annot Type: " + annot.getSDFObj().get("Subtype").value().getName());
115
116 double[] bbox = annot.getRect().get();
117 mOutputListener.println(" Position: " + format.format(bbox[0])
118 + ", " + format.format(bbox[1])
119 + ", " + format.format(bbox[2])
120 + ", " + format.format(bbox[3]));
121
122 switch (annot.getType()) {
123 case Annot.e_Link: {
124 com.pdftron.pdf.annots.Link link = new com.pdftron.pdf.annots.Link(annot);
125 Action action = link.getAction();
126 if (action.isValid() == false) continue;
127 if (action.getType() == Action.e_GoTo) {
128 Destination dest = action.getDest();
129 if (dest.isValid() == false) {
130 mOutputListener.println(" Destination is not valid.");
131 } else {
132 int page_link = dest.getPage().getIndex();
133 mOutputListener.println(" Links to: page number " + page_link + " in this document");
134 }
135 } else if (action.getType() == Action.e_URI) {
136 String uri = action.getSDFObj().get("URI").value().getAsPDFText();
137 mOutputListener.println(" Links to: " + uri);
138 }
139 // ...
140 }
141 break;
142 case Annot.e_Widget:
143 break;
144 case Annot.e_FileAttachment:
145 break;
146 // ...
147 default:
148 break;
149 }
150 }
151 }
152
153 // Use the high-level API to create new annotations.
154 Page first_page = doc.getPage(1);
155
156 // Create a hyperlink...
157 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"));
158 first_page.annotPushBack(hyperlink);
159
160 // Create an intra-document link...
161 Action goto_page_3 = Action.createGoto(Destination.createFitH(doc.getPage(3), 0));
162 com.pdftron.pdf.annots.Link link = com.pdftron.pdf.annots.Link.create(doc.getSDFDoc(),
163 new Rect(85, 458, 503, 502),
164 goto_page_3);
165 link.setColor(new ColorPt(0, 0, 1), 3);
166
167 // Add the new annotation to the first page
168 first_page.annotPushBack(link);
169
170 // Create a stamp annotation ...
171 com.pdftron.pdf.annots.RubberStamp stamp = com.pdftron.pdf.annots.RubberStamp.create(doc, new Rect(30, 30, 300, 200));
172 stamp.setIcon("Draft");
173 first_page.annotPushBack(stamp);
174
175 // Create a file attachment annotation (embed the 'peppers.jpg').
176 com.pdftron.pdf.annots.FileAttachment file_attach = com.pdftron.pdf.annots.FileAttachment.create(doc, new Rect(80, 280, 108, 320), (Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath()));
177 first_page.annotPushBack(file_attach);
178
179 com.pdftron.pdf.annots.Ink ink = com.pdftron.pdf.annots.Ink.create(doc, new Rect(110, 10, 300, 200));
180 Point pt3 = new Point(110, 10);
181 //pt3.x = 110; pt3.y = 10;
182 ink.setPoint(0, 0, pt3);
183 pt3.x = 150;
184 pt3.y = 50;
185 ink.setPoint(0, 1, pt3);
186 pt3.x = 190;
187 pt3.y = 60;
188 ink.setPoint(0, 2, pt3);
189 pt3.x = 180;
190 pt3.y = 90;
191 ink.setPoint(1, 0, pt3);
192 pt3.x = 190;
193 pt3.y = 95;
194 ink.setPoint(1, 1, pt3);
195 pt3.x = 200;
196 pt3.y = 100;
197 ink.setPoint(1, 2, pt3);
198 pt3.x = 166;
199 pt3.y = 86;
200 ink.setPoint(2, 0, pt3);
201 pt3.x = 196;
202 pt3.y = 96;
203 ink.setPoint(2, 1, pt3);
204 pt3.x = 221;
205 pt3.y = 121;
206 ink.setPoint(2, 2, pt3);
207 pt3.x = 288;
208 pt3.y = 188;
209 ink.setPoint(2, 3, pt3);
210 ink.setColor(new ColorPt(0, 1, 1), 3);
211 first_page.annotPushBack(ink);
212
213 }
214
215 static void AnnotationLowLevelAPI(PDFDoc doc) throws PDFNetException {
216 Page page = doc.getPageIterator().next();
217
218 Obj annots = page.getAnnots();
219
220 if (annots == null) {
221 // If there are no annotations, create a new annotation
222 // array for the page.
223 annots = doc.createIndirectArray();
224 page.getSDFObj().put("Annots", annots);
225 }
226
227 // Create a Text annotation
228 Obj annot = doc.createIndirectDict();
229 annot.putName("Subtype", "Text");
230 annot.putBool("Open", true);
231 annot.putString("Contents", "The quick brown fox ate the lazy mouse.");
232 annot.putRect("Rect", 266, 116, 430, 204);
233
234 // Insert the annotation in the page annotation array
235 annots.pushBack(annot);
236
237 // Create a Link annotation
238 Obj link1 = doc.createIndirectDict();
239 link1.putName("Subtype", "Link");
240 Destination dest = Destination.createFit(doc.getPage(2));
241 link1.put("Dest", dest.getSDFObj());
242 link1.putRect("Rect", 85, 705, 503, 661);
243 annots.pushBack(link1);
244
245 // Create another Link annotation
246 Obj link2 = doc.createIndirectDict();
247 link2.putName("Subtype", "Link");
248 Destination dest2 = Destination.createFit(doc.getPage(3));
249 link2.put("Dest", dest2.getSDFObj());
250 link2.putRect("Rect", 85, 638, 503, 594);
251 annots.pushBack(link2);
252
253 // Note that PDFNet APi can be used to modify existing annotations.
254 // In the following example we will modify the second link annotation
255 // (link2) so that it points to the 10th page. We also use a different
256 // destination page fit type.
257
258 // link2 = annots.GetAt(annots.Size()-1);
259 link2.put("Dest",
260 Destination.createXYZ(doc.getPage(10), 100, 792 - 70, 10).getSDFObj());
261
262 // Create a third link annotation with a hyperlink action (all other
263 // annotation types can be created in a similar way)
264 Obj link3 = doc.createIndirectDict();
265 link3.putName("Subtype", "Link");
266 link3.putRect("Rect", 85, 570, 503, 524);
267
268 // Create a URI action
269 Obj action = link3.putDict("A");
270 action.putName("S", "URI");
271 action.putString("URI", "http://www.pdftron.com");
272
273 annots.pushBack(link3);
274 }
275
276 static void CreateTestAnnots(PDFDoc doc) throws PDFNetException {
277 ElementWriter ew = new ElementWriter();
278 ElementBuilder eb = new ElementBuilder();
279 Element element;
280
281 Page first_page = doc.pageCreate(new Rect(0, 0, 600, 600));
282 doc.pagePushBack(first_page);
283 ew.begin(first_page, ElementWriter.e_overlay, false); // begin writing to this page
284 ew.end(); // save changes to the current page
285
286 //
287 // Test of a free text annotation.
288 //
289 {
290 FreeText txtannot = FreeText.create(doc, new Rect(10, 400, 160, 570));
291 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
292 "\n\nAha!\n\nAnd there was much rejoicing!");
293 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
294 txtannot.setQuaddingFormat(0);
295 first_page.annotPushBack(txtannot);
296 txtannot.refreshAppearance();
297 }
298 {
299 FreeText txtannot = FreeText.create(doc, new Rect(100, 100, 350, 500));
300 txtannot.setContentRect(new Rect(200, 200, 350, 500));
301 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
302 "\n\nAha!\n\nAnd there was much rejoicing!");
303 txtannot.setCalloutLinePoints(new Point(200, 300), new Point(150, 290), new Point(110, 110));
304 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
305 txtannot.setEndingStyle(Line.e_ClosedArrow);
306 txtannot.setColor(new ColorPt(0, 1, 0));
307 txtannot.setQuaddingFormat(1);
308 first_page.annotPushBack(txtannot);
309 txtannot.refreshAppearance();
310 }
311 {
312 FreeText txtannot = FreeText.create(doc, new Rect(400, 10, 550, 400));
313 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
314 "\n\nAha!\n\nAnd there was much rejoicing!");
315 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
316 txtannot.setColor(new ColorPt(0, 0, 1));
317 txtannot.setOpacity(0.2);
318 txtannot.setQuaddingFormat(2);
319 first_page.annotPushBack(txtannot);
320 txtannot.refreshAppearance();
321 }
322
323 Page page = doc.pageCreate(new Rect(0, 0, 600, 600));
324 doc.pagePushBack(page);
325 ew.begin(page, ElementWriter.e_overlay, false); // begin writing to this page
326 eb.reset(); // Reset the GState to default
327 ew.end(); // save changes to the current page
328
329 {
330 //Create a Line annotation...
331 Line line = Line.create(doc, new Rect(250, 250, 400, 400));
332 line.setStartPoint(new Point(350, 270));
333 line.setEndPoint(new Point(260, 370));
334 line.setStartStyle(Line.e_Square);
335 line.setEndStyle(Line.e_Circle);
336 line.setColor(new ColorPt(.3, .5, 0), 3);
337 line.setContents("Dashed Captioned");
338 line.setShowCaption(true);
339 line.setCaptionPosition(Line.e_Top);
340 double[] dash = {2, 2.0};
341 line.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 2, 0, 0, dash));
342 line.refreshAppearance();
343 page.annotPushBack(line);
344 }
345 {
346 Line line = Line.create(doc, new Rect(347, 377, 600, 600));
347 line.setStartPoint(new Point(385, 410));
348 line.setEndPoint(new Point(540, 555));
349 line.setStartStyle(Line.e_Circle);
350 line.setEndStyle(Line.e_OpenArrow);
351 line.setColor(new ColorPt(1, 0, 0), 3);
352 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
353 line.setContents("Inline Caption");
354 line.setShowCaption(true);
355 line.setCaptionPosition(Line.e_Inline);
356 line.setLeaderLineExtensionLength(4.);
357 line.setLeaderLineLength(-12.);
358 line.setLeaderLineOffset(2.);
359 line.refreshAppearance();
360 page.annotPushBack(line);
361 }
362 {
363 Line line = Line.create(doc, new Rect(10, 400, 200, 600));
364 line.setStartPoint(new Point(25, 426));
365 line.setEndPoint(new Point(180, 555));
366 line.setStartStyle(Line.e_Circle);
367 line.setEndStyle(Line.e_Square);
368 line.setColor(new ColorPt(0, 0, 1), 3);
369 line.setInteriorColor(new ColorPt(1, 0, 0), 3);
370 line.setContents("Offset Caption");
371 line.setShowCaption(true);
372 line.setCaptionPosition(Line.e_Top);
373 line.setTextHOffset(-60);
374 line.setTextVOffset(10);
375 line.refreshAppearance();
376 page.annotPushBack(line);
377 }
378 {
379 Line line = Line.create(doc, new Rect(200, 10, 400, 70));
380 line.setStartPoint(new Point(220, 25));
381 line.setEndPoint(new Point(370, 60));
382 line.setStartStyle(Line.e_Butt);
383 line.setEndStyle(Line.e_OpenArrow);
384 line.setColor(new ColorPt(0, 0, 1), 3);
385 line.setContents("Regular Caption");
386 line.setShowCaption(true);
387 line.setCaptionPosition(Line.e_Top);
388 line.refreshAppearance();
389 page.annotPushBack(line);
390 }
391 {
392 Line line = Line.create(doc, new Rect(200, 70, 400, 130));
393 line.setStartPoint(new Point(220, 111));
394 line.setEndPoint(new Point(370, 78));
395 line.setStartStyle(Line.e_Circle);
396 line.setEndStyle(Line.e_Diamond);
397 line.setContents("Circle to Diamond");
398 line.setColor(new ColorPt(0, 0, 1), 3);
399 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
400 line.setShowCaption(true);
401 line.setCaptionPosition(Line.e_Top);
402 line.refreshAppearance();
403 page.annotPushBack(line);
404 }
405 {
406 Line line = Line.create(doc, new Rect(10, 100, 160, 200));
407 line.setStartPoint(new Point(15, 110));
408 line.setEndPoint(new Point(150, 190));
409 line.setStartStyle(Line.e_Slash);
410 line.setEndStyle(Line.e_ClosedArrow);
411 line.setContents("Slash to CArrow");
412 line.setColor(new ColorPt(1, 0, 0), 3);
413 line.setInteriorColor(new ColorPt(0, 1, 1), 3);
414 line.setShowCaption(true);
415 line.setCaptionPosition(Line.e_Top);
416 line.refreshAppearance();
417 page.annotPushBack(line);
418 }
419 {
420 Line line = Line.create(doc, new Rect(270, 270, 570, 433));
421 line.setStartPoint(new Point(300, 400));
422 line.setEndPoint(new Point(550, 300));
423 line.setStartStyle(Line.e_RClosedArrow);
424 line.setEndStyle(Line.e_ROpenArrow);
425 line.setContents("ROpen & RClosed arrows");
426 line.setColor(new ColorPt(0, 0, 1), 3);
427 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
428 line.setShowCaption(true);
429 line.setCaptionPosition(Line.e_Top);
430 line.refreshAppearance();
431 page.annotPushBack(line);
432 }
433 {
434 Line line = Line.create(doc, new Rect(195, 395, 205, 505));
435 line.setStartPoint(new Point(200, 400));
436 line.setEndPoint(new Point(200, 500));
437 line.refreshAppearance();
438 page.annotPushBack(line);
439 }
440 {
441 Line line = Line.create(doc, new Rect(55, 299, 150, 301));
442 line.setStartPoint(new Point(55, 300));
443 line.setEndPoint(new Point(155, 300));
444 line.setStartStyle(Line.e_Circle);
445 line.setEndStyle(Line.e_Circle);
446 line.setContents("Caption that's longer than its line.");
447 line.setColor(new ColorPt(1, 0, 1), 3);
448 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
449 line.setShowCaption(true);
450 line.setCaptionPosition(Line.e_Top);
451 line.refreshAppearance();
452 page.annotPushBack(line);
453 }
454 {
455 Line line = Line.create(doc, new Rect(300, 200, 390, 234));
456 line.setStartPoint(new Point(310, 210));
457 line.setEndPoint(new Point(380, 220));
458 line.setColor(new ColorPt(0, 0, 0), 3);
459 line.refreshAppearance();
460 page.annotPushBack(line);
461 }
462
463 Page page3 = doc.pageCreate(new Rect(0, 0, 600, 600));
464 ew.begin(page3); // begin writing to the page
465 ew.end(); // save changes to the current page
466 doc.pagePushBack(page3);
467 {
468 Circle circle = Circle.create(doc, new Rect(300, 300, 390, 350));
469 circle.setColor(new ColorPt(0, 0, 0), 3);
470 circle.refreshAppearance();
471 page3.annotPushBack(circle);
472 }
473 {
474 Circle circle = Circle.create(doc, new Rect(100, 100, 200, 200));
475 circle.setColor(new ColorPt(0, 1, 0), 3);
476 circle.setInteriorColor(new ColorPt(0, 0, 1), 3);
477 double[] dash = {2, 4};
478 circle.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 3, 0, 0, dash));
479 circle.setPadding(2);
480 circle.refreshAppearance();
481 page3.annotPushBack(circle);
482 }
483 {
484 Square sq = Square.create(doc, new Rect(10, 200, 80, 300));
485 sq.setColor(new ColorPt(0, 0, 0), 3);
486 sq.refreshAppearance();
487 page3.annotPushBack(sq);
488 }
489 {
490 Square sq = Square.create(doc, new Rect(500, 200, 580, 300));
491 sq.setColor(new ColorPt(1, 0, 0), 3);
492 sq.setInteriorColor(new ColorPt(0, 1, 1), 3);
493 double[] dash = {4, 2};
494 sq.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 6, 0, 0, dash));
495 sq.setPadding(4);
496 sq.refreshAppearance();
497 page3.annotPushBack(sq);
498 }
499 {
500 Polygon poly = Polygon.create(doc, new Rect(5, 500, 125, 590));
501 poly.setColor(new ColorPt(1, 0, 0), 3);
502 poly.setInteriorColor(new ColorPt(1, 1, 0), 3);
503 poly.setVertex(0, new Point(12, 510));
504 poly.setVertex(1, new Point(100, 510));
505 poly.setVertex(2, new Point(100, 555));
506 poly.setVertex(3, new Point(35, 544));
507 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 4, 0, 0));
508 poly.setPadding(4);
509 poly.refreshAppearance();
510 page3.annotPushBack(poly);
511 }
512 {
513 PolyLine poly = PolyLine.create(doc, new Rect(400, 10, 500, 90));
514 poly.setColor(new ColorPt(1, 0, 0), 3);
515 poly.setInteriorColor(new ColorPt(0, 1, 0), 3);
516 poly.setVertex(0, new Point(405, 20));
517 poly.setVertex(1, new Point(440, 40));
518 poly.setVertex(2, new Point(410, 60));
519 poly.setVertex(3, new Point(470, 80));
520 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 2, 0, 0));
521 poly.setPadding(4);
522 poly.setStartStyle(Line.e_RClosedArrow);
523 poly.setEndStyle(Line.e_ClosedArrow);
524 poly.refreshAppearance();
525 page3.annotPushBack(poly);
526 }
527 {
528 Link lk = Link.create(doc, new Rect(5, 5, 55, 24));
529 lk.refreshAppearance();
530 page3.annotPushBack(lk);
531 }
532
533 Page page4 = doc.pageCreate(new Rect(0, 0, 600, 600));
534 ew.begin(page4); // begin writing to the page
535 ew.end(); // save changes to the current page
536 doc.pagePushBack(page4);
537
538 {
539 ew.begin(page4);
540 Font font = Font.create(doc, Font.e_helvetica);
541 element = eb.createTextBegin(font, 16);
542 element.setPathFill(true);
543 ew.writeElement(element);
544 element = eb.createTextRun("Some random text on the page", font, 16);
545 element.setTextMatrix(1, 0, 0, 1, 100, 500);
546 ew.writeElement(element);
547 ew.writeElement(eb.createTextEnd());
548 ew.end();
549 }
550 {
551 Highlight hl = Highlight.create(doc, new Rect(100, 490, 150, 515));
552 hl.setColor(new ColorPt(0, 1, 0), 3);
553 hl.refreshAppearance();
554 page4.annotPushBack(hl);
555 }
556 {
557 Squiggly sq = Squiggly.create(doc, new Rect(100, 450, 250, 600));
558 sq.setQuadPoint(0, new QuadPoint(new Point(122, 455), new Point(240, 545), new Point(230, 595), new Point(101, 500)));
559 sq.refreshAppearance();
560 page4.annotPushBack(sq);
561 }
562 {
563 Caret cr = Caret.create(doc, new Rect(100, 40, 129, 69));
564 cr.setColor(new ColorPt(0, 0, 1), 3);
565 cr.setSymbol("P");
566 cr.refreshAppearance();
567 page4.annotPushBack(cr);
568 }
569
570 Page page5 = doc.pageCreate(new Rect(0, 0, 600, 600));
571 ew.begin(page5); // begin writing to the page
572 ew.end(); // save changes to the current page
573 doc.pagePushBack(page5);
574 FileSpec fs = FileSpec.create(doc, Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath(), false);
575 Page page6 = doc.pageCreate(new Rect(0, 0, 600, 600));
576 ew.begin(page6); // begin writing to the page
577 ew.end(); // save changes to the current page
578 doc.pagePushBack(page6);
579
580 {
581 Text txt = Text.create(doc, new Point(10, 20));
582 txt.setIcon("UserIcon");
583 txt.setContents("User defined icon, unrecognized by appearance generator");
584 txt.setColor(new ColorPt(0, 1, 0));
585 txt.refreshAppearance();
586 page6.annotPushBack(txt);
587 }
588 {
589 Ink ink = Ink.create(doc, new Rect(100, 400, 200, 550));
590 ink.setColor(new ColorPt(0, 0, 1));
591 ink.setPoint(1, 3, new Point(220, 505));
592 ink.setPoint(1, 0, new Point(100, 490));
593 ink.setPoint(0, 1, new Point(120, 410));
594 ink.setPoint(0, 0, new Point(100, 400));
595 ink.setPoint(1, 2, new Point(180, 490));
596 ink.setPoint(1, 1, new Point(140, 440));
597 ink.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 3, 0, 0));
598 ink.refreshAppearance();
599 page6.annotPushBack(ink);
600 }
601
602 Page page7 = doc.pageCreate(new Rect(0, 0, 600, 600));
603 ew.begin(page7); // begin writing to the page
604 ew.end(); // save changes to the current page
605 doc.pagePushBack(page7);
606
607 {
608 Sound snd = Sound.create(doc, new Rect(100, 500, 120, 520));
609 snd.setColor(new ColorPt(1, 1, 0));
610 snd.setIcon(Sound.e_Speaker);
611 snd.refreshAppearance();
612 page7.annotPushBack(snd);
613 }
614 {
615 Sound snd = Sound.create(doc, new Rect(200, 500, 220, 520));
616 snd.setColor(new ColorPt(1, 1, 0));
617 snd.setIcon(Sound.e_Mic);
618 snd.refreshAppearance();
619 page7.annotPushBack(snd);
620 }
621
622 Page page8 = doc.pageCreate(new Rect(0, 0, 600, 600));
623 ew.begin(page8); // begin writing to the page
624 ew.end(); // save changes to the current page
625 doc.pagePushBack(page8);
626
627 for (int ipage = 0; ipage < 2; ++ipage) {
628 double px = 5, py = 520;
629 for (int istamp = 0; istamp <= RubberStamp.e_Draft; ++istamp) {
630 RubberStamp st = RubberStamp.create(doc, new Rect(1, 1, 100, 100));
631 st.SetIcon(istamp);
632 st.setContents(st.getIconName());
633 st.setRect(new Rect(px, py, px + 100, py + 25));
634 py -= 100;
635 if (py < 0) {
636 py = 520;
637 px += 200;
638 }
639 if (ipage == 0)
640 //page7.AnnotPushBack( st );
641 ;
642 else {
643 page8.annotPushBack(st);
644 st.refreshAppearance();
645 }
646 }
647 }
648 RubberStamp st = RubberStamp.create(doc, new Rect(400, 5, 550, 45));
649 st.setIcon("UserStamp");
650 st.setContents("User defined stamp");
651 page8.annotPushBack(st);
652 st.refreshAppearance();
653 }
654
655
656}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample
10import com.pdftron.android.pdfnetsdksamples.R
11import com.pdftron.android.pdfnetsdksamples.util.Utils
12import com.pdftron.common.PDFNetException
13import com.pdftron.pdf.Action
14import com.pdftron.pdf.Annot
15import com.pdftron.pdf.Annot.BorderStyle
16import com.pdftron.pdf.ColorPt
17import com.pdftron.pdf.Destination
18import com.pdftron.pdf.Element
19import com.pdftron.pdf.ElementBuilder
20import com.pdftron.pdf.ElementWriter
21import com.pdftron.pdf.FileSpec
22import com.pdftron.pdf.Font
23import com.pdftron.pdf.PDFDoc
24import com.pdftron.pdf.Page
25import com.pdftron.pdf.PageIterator
26import com.pdftron.pdf.Point
27import com.pdftron.pdf.QuadPoint
28import com.pdftron.pdf.Rect
29import com.pdftron.pdf.annots.Caret
30import com.pdftron.pdf.annots.Circle
31import com.pdftron.pdf.annots.FreeText
32import com.pdftron.pdf.annots.Highlight
33import com.pdftron.pdf.annots.Ink
34import com.pdftron.pdf.annots.Line
35import com.pdftron.pdf.annots.Link
36import com.pdftron.pdf.annots.PolyLine
37import com.pdftron.pdf.annots.Polygon
38import com.pdftron.pdf.annots.RubberStamp
39import com.pdftron.pdf.annots.Sound
40import com.pdftron.pdf.annots.Square
41import com.pdftron.pdf.annots.Squiggly
42import com.pdftron.pdf.annots.Text
43import com.pdftron.sdf.Obj
44import com.pdftron.sdf.SDFDoc
45
46import java.text.DecimalFormat
47import java.util.ArrayList
48
49class AnnotationTest : PDFNetSample() {
50 init {
51 setTitle(R.string.sample_annotation_title)
52 setDescription(R.string.sample_annotation_description)
53 }
54
55 override fun run(outputListener: OutputListener?) {
56 super.run(outputListener)
57 mOutputListener = outputListener
58 mFileList.clear()
59 printHeader(outputListener!!)
60
61 try {
62 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "numbered.pdf")!!.absolutePath).use { doc ->
63
64 doc.initSecurityHandler()
65
66 // An example of using SDF/Cos API to add any type of annotations.
67 AnnotationLowLevelAPI(doc)
68 doc.save(Utils.createExternalFile("annotation_test1.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
69 mOutputListener!!.println("Done. Results saved in annotation_test1.pdf")
70
71 // An example of using the high-level PDFNet API to read existing annotations,
72 // to edit existing annotations, and to create new annotation from scratch.
73 AnnotationHighLevelAPI(doc)
74 doc.save(Utils.createExternalFile("annotation_test2.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
75 mOutputListener!!.println("Done. Results saved in annotation_test2.pdf")
76 }
77 } catch (e: Exception) {
78 mOutputListener!!.printError(e.stackTrace)
79 }
80
81 try {
82 // an example of creating various annotations in a brand new document
83 PDFDoc().use { doc1 ->
84 CreateTestAnnots(doc1)
85 doc1.save(Utils.createExternalFile("new_annot_test_api.pdf", mFileList).absolutePath, SDFDoc.SaveMode.LINEARIZED, null)
86 mOutputListener!!.println("Saved new_annot_test_api.pdf")
87 }
88 } catch (e: Exception) {
89 mOutputListener!!.printError(e.stackTrace)
90 }
91
92 for (file in mFileList) {
93 addToFileList(file)
94 }
95 printFooter(outputListener)
96 }
97
98 companion object {
99
100 private var mOutputListener: OutputListener? = null
101
102 private val mFileList = ArrayList<String>()
103
104 val format = DecimalFormat("0.#")
105
106 @Throws(PDFNetException::class)
107 internal fun AnnotationHighLevelAPI(doc: PDFDoc) {
108 // The following code snippet traverses all annotations in the document
109 mOutputListener!!.println("Traversing all annotations in the document...")
110
111 var page_num = 1
112 val itr = doc.pageIterator
113 while (itr.hasNext()) {
114 mOutputListener!!.println("Page " + page_num++ + ": ")
115
116 val page = itr.next()
117 val num_annots = page!!.getNumAnnots()
118 loop@ for (i in 0 until num_annots) {
119 val annot = page.getAnnot(i)
120 if (annot.isValid == false) continue
121 mOutputListener!!.println("Annot Type: " + annot.sdfObj.get("Subtype").value().name)
122
123 val bbox = annot.rect.get()
124 mOutputListener!!.println(" Position: " + format.format(bbox[0])
125 + ", " + format.format(bbox[1])
126 + ", " + format.format(bbox[2])
127 + ", " + format.format(bbox[3]))
128
129 when (annot.type) {
130 Annot.e_Link -> {
131 val link = com.pdftron.pdf.annots.Link(annot)
132 val action = link.action
133 if (action.isValid == false) continue@loop
134 if (action.type == Action.e_GoTo) {
135 val dest = action.dest
136 if (dest.isValid == false) {
137 mOutputListener!!.println(" Destination is not valid.")
138 } else {
139 val page_link = dest.page.index
140 mOutputListener!!.println(" Links to: page number $page_link in this document")
141 }
142 } else if (action.type == Action.e_URI) {
143 val uri = action.sdfObj.get("URI").value().asPDFText
144 mOutputListener!!.println(" Links to: $uri")
145 }
146 // ...
147 }
148 Annot.e_Widget -> {
149 }
150 Annot.e_FileAttachment -> {
151 }
152 // ...
153 else -> {
154 }
155 }
156 }
157 }
158
159 // Use the high-level API to create new annotations.
160 val first_page = doc.getPage(1)
161
162 // Create a hyperlink...
163 val hyperlink = com.pdftron.pdf.annots.Link.create(doc, Rect(85.0, 570.0, 503.0, 524.0), Action.createURI(doc, "http://www.pdftron.com"))
164 first_page.annotPushBack(hyperlink)
165
166 // Create an intra-document link...
167 val goto_page_3 = Action.createGoto(Destination.createFitH(doc.getPage(3), 0.0))
168 val link = com.pdftron.pdf.annots.Link.create(doc.sdfDoc,
169 Rect(85.0, 458.0, 503.0, 502.0),
170 goto_page_3)
171 link.setColor(ColorPt(1.0, 0.0, 0.0), 3)
172
173 // Add the new annotation to the first page
174 first_page.annotPushBack(link)
175
176 // Create a stamp annotation ...
177 val stamp = com.pdftron.pdf.annots.RubberStamp.create(doc, Rect(30.0, 30.0, 300.0, 200.0))
178 stamp.setIcon("Draft")
179 first_page.annotPushBack(stamp)
180
181 // Create a file attachment annotation (embed the 'peppers.jpg').
182 val file_attach = com.pdftron.pdf.annots.FileAttachment.create(doc, Rect(80.0, 280.0, 108.0, 320.0), Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "peppers.jpg")!!.absolutePath)
183 first_page.annotPushBack(file_attach)
184
185 val ink = com.pdftron.pdf.annots.Ink.create(doc, Rect(110.0, 10.0, 300.0, 200.0))
186 val pt3 = Point(110.0, 10.0)
187 //pt3.x = 110; pt3.y = 10;
188 ink.setPoint(0, 0, pt3)
189 pt3.x = 150.0
190 pt3.y = 50.0
191 ink.setPoint(0, 1, pt3)
192 pt3.x = 190.0
193 pt3.y = 60.0
194 ink.setPoint(0, 2, pt3)
195 pt3.x = 180.0
196 pt3.y = 90.0
197 ink.setPoint(1, 0, pt3)
198 pt3.x = 190.0
199 pt3.y = 95.0
200 ink.setPoint(1, 1, pt3)
201 pt3.x = 200.0
202 pt3.y = 100.0
203 ink.setPoint(1, 2, pt3)
204 pt3.x = 166.0
205 pt3.y = 86.0
206 ink.setPoint(2, 0, pt3)
207 pt3.x = 196.0
208 pt3.y = 96.0
209 ink.setPoint(2, 1, pt3)
210 pt3.x = 221.0
211 pt3.y = 121.0
212 ink.setPoint(2, 2, pt3)
213 pt3.x = 288.0
214 pt3.y = 188.0
215 ink.setPoint(2, 3, pt3)
216 ink.setColor(ColorPt(0.0, 1.0, 1.0), 3)
217 first_page.annotPushBack(ink)
218
219 }
220
221 @Throws(PDFNetException::class)
222 internal fun AnnotationLowLevelAPI(doc: PDFDoc) {
223 val page = doc.pageIterator.next()
224
225 var annots: Obj? = page!!.getAnnots()
226
227 if (annots == null) {
228 // If there are no annotations, create a new annotation
229 // array for the page.
230 annots = doc.createIndirectArray()
231 page.getSDFObj().put("Annots", annots!!)
232 }
233
234 // Create a Text annotation
235 val annot = doc.createIndirectDict()
236 annot.putName("Subtype", "Text")
237 annot.putBool("Open", true)
238 annot.putString("Contents", "The quick brown fox ate the lazy mouse.")
239 annot.putRect("Rect", 266.0, 116.0, 430.0, 204.0)
240
241 // Insert the annotation in the page annotation array
242 annots.pushBack(annot)
243
244 // Create a Link annotation
245 val link1 = doc.createIndirectDict()
246 link1.putName("Subtype", "Link")
247 val dest = Destination.createFit(doc.getPage(2))
248 link1.put("Dest", dest.sdfObj)
249 link1.putRect("Rect", 85.0, 705.0, 503.0, 661.0)
250 annots.pushBack(link1)
251
252 // Create another Link annotation
253 val link2 = doc.createIndirectDict()
254 link2.putName("Subtype", "Link")
255 val dest2 = Destination.createFit(doc.getPage(3))
256 link2.put("Dest", dest2.sdfObj)
257 link2.putRect("Rect", 85.0, 638.0, 503.0, 594.0)
258 annots.pushBack(link2)
259
260 // Note that PDFNet APi can be used to modify existing annotations.
261 // In the following example we will modify the second link annotation
262 // (link2) so that it points to the 10th page. We also use a different
263 // destination page fit type.
264
265 // link2 = annots.GetAt(annots.Size()-1);
266 link2.put("Dest",
267 Destination.createXYZ(doc.getPage(10), 100.0, (792 - 70).toDouble(), 10.0).sdfObj)
268
269 // Create a third link annotation with a hyperlink action (all other
270 // annotation types can be created in a similar way)
271 val link3 = doc.createIndirectDict()
272 link3.putName("Subtype", "Link")
273 link3.putRect("Rect", 85.0, 570.0, 503.0, 524.0)
274
275 // Create a URI action
276 val action = link3.putDict("A")
277 action.putName("S", "URI")
278 action.putString("URI", "http://www.pdftron.com")
279
280 annots.pushBack(link3)
281 }
282
283 @Throws(PDFNetException::class)
284 internal fun CreateTestAnnots(doc: PDFDoc) {
285 val ew = ElementWriter()
286 val eb = ElementBuilder()
287 var element: Element
288
289 val first_page = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
290 doc.pagePushBack(first_page)
291 ew.begin(first_page, ElementWriter.e_overlay, false) // begin writing to this page
292 ew.end() // save changes to the current page
293
294 //
295 // Test of a free text annotation.
296 //
297 run {
298 val txtannot = FreeText.create(doc, Rect(10.0, 400.0, 160.0, 570.0))
299 txtannot.contents = "\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!"
300 txtannot.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20)
301 txtannot.quaddingFormat = 0
302 first_page.annotPushBack(txtannot)
303 txtannot.refreshAppearance()
304 }
305 run {
306 val txtannot = FreeText.create(doc, Rect(100.0, 100.0, 350.0, 500.0))
307 txtannot.contentRect = Rect(200.0, 200.0, 350.0, 500.0)
308 txtannot.contents = "\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!"
309 txtannot.setCalloutLinePoints(Point(200.0, 300.0), Point(150.0, 290.0), Point(110.0, 110.0))
310 txtannot.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20)
311 txtannot.endingStyle = Line.e_ClosedArrow
312 txtannot.setColor(ColorPt(0.0, 1.0, 0.0))
313 txtannot.quaddingFormat = 1
314 first_page.annotPushBack(txtannot)
315 txtannot.refreshAppearance()
316 }
317 run {
318 val txtannot = FreeText.create(doc, Rect(400.0, 10.0, 550.0, 400.0))
319 txtannot.contents = "\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!"
320 txtannot.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20)
321 txtannot.setColor(ColorPt(0.0, 0.0, 1.0))
322 txtannot.opacity = 0.2
323 txtannot.quaddingFormat = 2
324 first_page.annotPushBack(txtannot)
325 txtannot.refreshAppearance()
326 }
327
328 val page = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
329 doc.pagePushBack(page)
330 ew.begin(page, ElementWriter.e_overlay, false) // begin writing to this page
331 eb.reset() // Reset the GState to default
332 ew.end() // save changes to the current page
333
334 run {
335 //Create a Line annotation...
336 val line = Line.create(doc, Rect(250.0, 250.0, 400.0, 400.0))
337 line.startPoint = Point(350.0, 270.0)
338 line.endPoint = Point(260.0, 370.0)
339 line.startStyle = Line.e_Square
340 line.endStyle = Line.e_Circle
341 line.setColor(ColorPt(.3, .5, 0.0), 3)
342 line.contents = "Dashed Captioned"
343 line.showCaption = true
344 line.captionPosition = Line.e_Top
345 val dash = doubleArrayOf(2.0, 2.0)
346 line.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_dashed, 2, 0, 0, dash)
347 line.refreshAppearance()
348 page.annotPushBack(line)
349 }
350 run {
351 val line = Line.create(doc, Rect(347.0, 377.0, 600.0, 600.0))
352 line.startPoint = Point(385.0, 410.0)
353 line.endPoint = Point(540.0, 555.0)
354 line.startStyle = Line.e_Circle
355 line.endStyle = Line.e_OpenArrow
356 line.setColor(ColorPt(1.0, 0.0, 0.0), 3)
357 line.setInteriorColor(ColorPt(0.0, 1.0, 0.0), 3)
358 line.contents = "Inline Caption"
359 line.showCaption = true
360 line.captionPosition = Line.e_Inline
361 line.leaderLineExtensionLength = 4.0
362 line.leaderLineLength = -12.0
363 line.leaderLineOffset = 2.0
364 line.refreshAppearance()
365 page.annotPushBack(line)
366 }
367 run {
368 val line = Line.create(doc, Rect(10.0, 400.0, 200.0, 600.0))
369 line.startPoint = Point(25.0, 426.0)
370 line.endPoint = Point(180.0, 555.0)
371 line.startStyle = Line.e_Circle
372 line.endStyle = Line.e_Square
373 line.setColor(ColorPt(0.0, 0.0, 1.0), 3)
374 line.setInteriorColor(ColorPt(1.0, 0.0, 0.0), 3)
375 line.contents = "Offset Caption"
376 line.showCaption = true
377 line.captionPosition = Line.e_Top
378 line.textHOffset = -60.0
379 line.textVOffset = 10.0
380 line.refreshAppearance()
381 page.annotPushBack(line)
382 }
383 run {
384 val line = Line.create(doc, Rect(200.0, 10.0, 400.0, 70.0))
385 line.startPoint = Point(220.0, 25.0)
386 line.endPoint = Point(370.0, 60.0)
387 line.startStyle = Line.e_Butt
388 line.endStyle = Line.e_OpenArrow
389 line.setColor(ColorPt(0.0, 0.0, 1.0), 3)
390 line.contents = "Regular Caption"
391 line.showCaption = true
392 line.captionPosition = Line.e_Top
393 line.refreshAppearance()
394 page.annotPushBack(line)
395 }
396 run {
397 val line = Line.create(doc, Rect(200.0, 70.0, 400.0, 130.0))
398 line.startPoint = Point(220.0, 111.0)
399 line.endPoint = Point(370.0, 78.0)
400 line.startStyle = Line.e_Circle
401 line.endStyle = Line.e_Diamond
402 line.contents = "Circle to Diamond"
403 line.setColor(ColorPt(0.0, 0.0, 1.0), 3)
404 line.setInteriorColor(ColorPt(0.0, 1.0, 0.0), 3)
405 line.showCaption = true
406 line.captionPosition = Line.e_Top
407 line.refreshAppearance()
408 page.annotPushBack(line)
409 }
410 run {
411 val line = Line.create(doc, Rect(10.0, 100.0, 160.0, 200.0))
412 line.startPoint = Point(15.0, 110.0)
413 line.endPoint = Point(150.0, 190.0)
414 line.startStyle = Line.e_Slash
415 line.endStyle = Line.e_ClosedArrow
416 line.contents = "Slash to CArrow"
417 line.setColor(ColorPt(1.0, 0.0, 0.0), 3)
418 line.setInteriorColor(ColorPt(0.0, 1.0, 1.0), 3)
419 line.showCaption = true
420 line.captionPosition = Line.e_Top
421 line.refreshAppearance()
422 page.annotPushBack(line)
423 }
424 run {
425 val line = Line.create(doc, Rect(270.0, 270.0, 570.0, 433.0))
426 line.startPoint = Point(300.0, 400.0)
427 line.endPoint = Point(550.0, 300.0)
428 line.startStyle = Line.e_RClosedArrow
429 line.endStyle = Line.e_ROpenArrow
430 line.contents = "ROpen & RClosed arrows"
431 line.setColor(ColorPt(0.0, 0.0, 1.0), 3)
432 line.setInteriorColor(ColorPt(0.0, 1.0, 0.0), 3)
433 line.showCaption = true
434 line.captionPosition = Line.e_Top
435 line.refreshAppearance()
436 page.annotPushBack(line)
437 }
438 run {
439 val line = Line.create(doc, Rect(195.0, 395.0, 205.0, 505.0))
440 line.startPoint = Point(200.0, 400.0)
441 line.endPoint = Point(200.0, 500.0)
442 line.refreshAppearance()
443 page.annotPushBack(line)
444 }
445 run {
446 val line = Line.create(doc, Rect(55.0, 299.0, 150.0, 301.0))
447 line.startPoint = Point(55.0, 300.0)
448 line.endPoint = Point(155.0, 300.0)
449 line.startStyle = Line.e_Circle
450 line.endStyle = Line.e_Circle
451 line.contents = "Caption that's longer than its line."
452 line.setColor(ColorPt(1.0, 0.0, 1.0), 3)
453 line.setInteriorColor(ColorPt(0.0, 1.0, 0.0), 3)
454 line.showCaption = true
455 line.captionPosition = Line.e_Top
456 line.refreshAppearance()
457 page.annotPushBack(line)
458 }
459 run {
460 val line = Line.create(doc, Rect(300.0, 200.0, 390.0, 234.0))
461 line.startPoint = Point(310.0, 210.0)
462 line.endPoint = Point(380.0, 220.0)
463 line.setColor(ColorPt(0.0, 0.0, 0.0), 3)
464 line.refreshAppearance()
465 page.annotPushBack(line)
466 }
467
468 val page3 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
469 ew.begin(page3) // begin writing to the page
470 ew.end() // save changes to the current page
471 doc.pagePushBack(page3)
472 run {
473 val circle = Circle.create(doc, Rect(300.0, 300.0, 390.0, 350.0))
474 circle.setColor(ColorPt(0.0, 0.0, 0.0), 3)
475 circle.refreshAppearance()
476 page3.annotPushBack(circle)
477 }
478 run {
479 val circle = Circle.create(doc, Rect(100.0, 100.0, 200.0, 200.0))
480 circle.setColor(ColorPt(0.0, 1.0, 0.0), 3)
481 circle.setInteriorColor(ColorPt(0.0, 0.0, 1.0), 3)
482 val dash = doubleArrayOf(2.0, 4.0)
483 circle.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_dashed, 3, 0, 0, dash)
484 circle.setPadding(2.0)
485 circle.refreshAppearance()
486 page3.annotPushBack(circle)
487 }
488 run {
489 val sq = Square.create(doc, Rect(10.0, 200.0, 80.0, 300.0))
490 sq.setColor(ColorPt(0.0, 0.0, 0.0), 3)
491 sq.refreshAppearance()
492 page3.annotPushBack(sq)
493 }
494 run {
495 val sq = Square.create(doc, Rect(500.0, 200.0, 580.0, 300.0))
496 sq.setColor(ColorPt(1.0, 0.0, 0.0), 3)
497 sq.setInteriorColor(ColorPt(0.0, 1.0, 1.0), 3)
498 val dash = doubleArrayOf(4.0, 2.0)
499 sq.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_dashed, 6, 0, 0, dash)
500 sq.setPadding(4.0)
501 sq.refreshAppearance()
502 page3.annotPushBack(sq)
503 }
504 run {
505 val poly = Polygon.create(doc, Rect(5.0, 500.0, 125.0, 590.0))
506 poly.setColor(ColorPt(1.0, 0.0, 0.0), 3)
507 poly.setInteriorColor(ColorPt(1.0, 1.0, 0.0), 3)
508 poly.setVertex(0, Point(12.0, 510.0))
509 poly.setVertex(1, Point(100.0, 510.0))
510 poly.setVertex(2, Point(100.0, 555.0))
511 poly.setVertex(3, Point(35.0, 544.0))
512 poly.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 4, 0, 0)
513 poly.setPadding(4.0)
514 poly.refreshAppearance()
515 page3.annotPushBack(poly)
516 }
517 run {
518 val poly = PolyLine.create(doc, Rect(400.0, 10.0, 500.0, 90.0))
519 poly.setColor(ColorPt(1.0, 0.0, 0.0), 3)
520 poly.setInteriorColor(ColorPt(0.0, 1.0, 0.0), 3)
521 poly.setVertex(0, Point(405.0, 20.0))
522 poly.setVertex(1, Point(440.0, 40.0))
523 poly.setVertex(2, Point(410.0, 60.0))
524 poly.setVertex(3, Point(470.0, 80.0))
525 poly.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 2, 0, 0)
526 poly.setPadding(4.0)
527 poly.startStyle = Line.e_RClosedArrow
528 poly.endStyle = Line.e_ClosedArrow
529 poly.refreshAppearance()
530 page3.annotPushBack(poly)
531 }
532 run {
533 val lk = Link.create(doc, Rect(5.0, 5.0, 55.0, 24.0))
534 lk.refreshAppearance()
535 page3.annotPushBack(lk)
536 }
537
538 val page4 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
539 ew.begin(page4) // begin writing to the page
540 ew.end() // save changes to the current page
541 doc.pagePushBack(page4)
542
543 run {
544 ew.begin(page4)
545 val font = Font.create(doc, Font.e_helvetica)
546 element = eb.createTextBegin(font, 16.0)
547 element.setPathFill(true)
548 ew.writeElement(element)
549 element = eb.createTextRun("Some random text on the page", font, 16.0)
550 element.setTextMatrix(1.0, 0.0, 0.0, 1.0, 100.0, 500.0)
551 ew.writeElement(element)
552 ew.writeElement(eb.createTextEnd())
553 ew.end()
554 }
555 run {
556 val hl = Highlight.create(doc, Rect(100.0, 490.0, 150.0, 515.0))
557 hl.setColor(ColorPt(0.0, 1.0, 0.0), 3)
558 hl.refreshAppearance()
559 page4.annotPushBack(hl)
560 }
561 run {
562 val sq = Squiggly.create(doc, Rect(100.0, 450.0, 250.0, 600.0))
563 sq.setQuadPoint(0, QuadPoint(Point(122.0, 455.0), Point(240.0, 545.0), Point(230.0, 595.0), Point(101.0, 500.0)))
564 sq.refreshAppearance()
565 page4.annotPushBack(sq)
566 }
567 run {
568 val cr = Caret.create(doc, Rect(100.0, 40.0, 129.0, 69.0))
569 cr.setColor(ColorPt(0.0, 0.0, 1.0), 3)
570 cr.symbol = "P"
571 cr.refreshAppearance()
572 page4.annotPushBack(cr)
573 }
574
575 val page5 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
576 ew.begin(page5) // begin writing to the page
577 ew.end() // save changes to the current page
578 doc.pagePushBack(page5)
579 val fs = FileSpec.create(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath, false)
580 val page6 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
581 ew.begin(page6) // begin writing to the page
582 ew.end() // save changes to the current page
583 doc.pagePushBack(page6)
584
585 run {
586 val txt = Text.create(doc, Point(10.0, 20.0))
587 txt.setIcon("UserIcon")
588 txt.contents = "User defined icon, unrecognized by appearance generator"
589 txt.setColor(ColorPt(0.0, 1.0, 0.0))
590 txt.refreshAppearance()
591 page6.annotPushBack(txt)
592 }
593 run {
594 val ink = Ink.create(doc, Rect(100.0, 400.0, 200.0, 550.0))
595 ink.setColor(ColorPt(0.0, 0.0, 1.0))
596 ink.setPoint(1, 3, Point(220.0, 505.0))
597 ink.setPoint(1, 0, Point(100.0, 490.0))
598 ink.setPoint(0, 1, Point(120.0, 410.0))
599 ink.setPoint(0, 0, Point(100.0, 400.0))
600 ink.setPoint(1, 2, Point(180.0, 490.0))
601 ink.setPoint(1, 1, Point(140.0, 440.0))
602 ink.borderStyle = Annot.BorderStyle(Annot.BorderStyle.e_solid, 3, 0, 0)
603 ink.refreshAppearance()
604 page6.annotPushBack(ink)
605 }
606
607 val page7 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
608 ew.begin(page7) // begin writing to the page
609 ew.end() // save changes to the current page
610 doc.pagePushBack(page7)
611
612 run {
613 val snd = Sound.create(doc, Rect(100.0, 500.0, 120.0, 520.0))
614 snd.setColor(ColorPt(1.0, 1.0, 0.0))
615 snd.icon = Sound.e_Speaker
616 snd.refreshAppearance()
617 page7.annotPushBack(snd)
618 }
619 run {
620 val snd = Sound.create(doc, Rect(200.0, 500.0, 220.0, 520.0))
621 snd.setColor(ColorPt(1.0, 1.0, 0.0))
622 snd.icon = Sound.e_Mic
623 snd.refreshAppearance()
624 page7.annotPushBack(snd)
625 }
626
627 val page8 = doc.pageCreate(Rect(0.0, 0.0, 600.0, 600.0))
628 ew.begin(page8) // begin writing to the page
629 ew.end() // save changes to the current page
630 doc.pagePushBack(page8)
631
632 for (ipage in 0..1) {
633 var px = 5.0
634 var py = 520.0
635 for (istamp in 0..RubberStamp.e_Draft) {
636 val st = RubberStamp.create(doc, Rect(1.0, 1.0, 100.0, 100.0))
637 st.SetIcon(istamp)
638 st.contents = st.iconName
639 st.rect = Rect(px, py, px + 100, py + 25)
640 py -= 100.0
641 if (py < 0) {
642 py = 520.0
643 px += 200.0
644 }
645 if (ipage == 0)
646 else {
647 page8.annotPushBack(st)
648 st.refreshAppearance()
649 }//page7.AnnotPushBack( st );
650 }
651 }
652 val st = RubberStamp.create(doc, Rect(400.0, 5.0, 550.0, 45.0))
653 st.setIcon("UserStamp")
654 st.contents = "User defined stamp"
655 page8.annotPushBack(st)
656 st.refreshAppearance()
657 }
658 }
659
660}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales