PDF Annotation - Add/Edit

Sample code to use Apryse Server SDK for adding or editing PDF annotations. The annotation types included in this sample are: hyperlink, intra-document link, stamp, rubber stamp, file attachment, sound, text, free-text, line, circle, square, polygon, polyline, highlight, squiggly, caret, and ink. Learn more about our Server SDK and PDF Annotation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.pdf.Annot.BorderStyle;
9import com.pdftron.pdf.annots.*;
10import com.pdftron.sdf.SDFDoc;
11import com.pdftron.sdf.Obj;
12
13import java.text.DecimalFormat;
14
15
16public class AnnotationTest {
17 // Relative path to the folder containing test files.
18 public static final String input_path = "../../TestFiles/";
19
20 public static final DecimalFormat format = new DecimalFormat("0.#");
21
22 static void AnnotationHighLevelAPI(PDFDoc doc) throws PDFNetException {
23 // The following code snippet traverses all annotations in the document
24 System.out.println("Traversing all annotations in the document...");
25
26 int page_num = 1;
27 for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) {
28 System.out.println("Page " + (page_num++) + ": ");
29
30 Page page = itr.next();
31 int num_annots = page.getNumAnnots();
32 for (int i = 0; i < num_annots; ++i) {
33 Annot annot = page.getAnnot(i);
34 if (annot.isValid() == false) continue;
35 System.out.println("Annot Type: " + annot.getSDFObj().get("Subtype").value().getName());
36
37 double[] bbox = annot.getRect().get();
38 System.out.println(" Position: " + format.format(bbox[0])
39 + ", " + format.format(bbox[1])
40 + ", " + format.format(bbox[2])
41 + ", " + format.format(bbox[3]));
42
43 switch (annot.getType()) {
44 case Annot.e_Link: {
45 com.pdftron.pdf.annots.Link link = new com.pdftron.pdf.annots.Link(annot);
46 Action action = link.getAction();
47 if (action.isValid() == false) continue;
48 if (action.getType() == Action.e_GoTo) {
49 Destination dest = action.getDest();
50 if (dest.isValid() == false) {
51 System.out.println(" Destination is not valid.");
52 } else {
53 int page_link = dest.getPage().getIndex();
54 System.out.println(" Links to: page number " + page_link + " in this document");
55 }
56 } else if (action.getType() == Action.e_URI) {
57 String uri = action.getSDFObj().get("URI").value().getAsPDFText();
58 System.out.println(" Links to: " + uri);
59 }
60 // ...
61 }
62 break;
63 case Annot.e_Widget:
64 break;
65 case Annot.e_FileAttachment:
66 break;
67 // ...
68 default:
69 break;
70 }
71 }
72 }
73
74 // Use the high-level API to create new annotations.
75 Page first_page = doc.getPage(1);
76
77 // Create a hyperlink...
78 com.pdftron.pdf.annots.Link hyperlink = com.pdftron.pdf.annots.Link.create(doc, new Rect(85, 570, 503, 524), Action.createURI(doc, "http://www.pdftron.com"));
79 first_page.annotPushBack(hyperlink);
80
81 // Create an intra-document link...
82 Action goto_page_3 = Action.createGoto(Destination.createFitH(doc.getPage(3), 0));
83 com.pdftron.pdf.annots.Link link = com.pdftron.pdf.annots.Link.create(doc.getSDFDoc(),
84 new Rect(85, 458, 503, 502),
85 goto_page_3);
86 link.setColor(new ColorPt(0, 0, 1), 3);
87
88 // Add the new annotation to the first page
89 first_page.annotPushBack(link);
90
91 // Create a stamp annotation ...
92 com.pdftron.pdf.annots.RubberStamp stamp = com.pdftron.pdf.annots.RubberStamp.create(doc, new Rect(30, 30, 300, 200));
93 stamp.setIcon("Draft");
94 first_page.annotPushBack(stamp);
95
96 // Create a file attachment annotation (embed the 'peppers.jpg').
97 com.pdftron.pdf.annots.FileAttachment file_attach = com.pdftron.pdf.annots.FileAttachment.create(doc, new Rect(80, 280, 108, 320), (input_path + "peppers.jpg"));
98 first_page.annotPushBack(file_attach);
99
100
101 com.pdftron.pdf.annots.Ink ink = com.pdftron.pdf.annots.Ink.create(doc, new Rect(110, 10, 300, 200));
102 Point pt3 = new Point(110, 10);
103 //pt3.x = 110; pt3.y = 10;
104 ink.setPoint(0, 0, pt3);
105 pt3.x = 150;
106 pt3.y = 50;
107 ink.setPoint(0, 1, pt3);
108 pt3.x = 190;
109 pt3.y = 60;
110 ink.setPoint(0, 2, pt3);
111 pt3.x = 180;
112 pt3.y = 90;
113 ink.setPoint(1, 0, pt3);
114 pt3.x = 190;
115 pt3.y = 95;
116 ink.setPoint(1, 1, pt3);
117 pt3.x = 200;
118 pt3.y = 100;
119 ink.setPoint(1, 2, pt3);
120 pt3.x = 166;
121 pt3.y = 86;
122 ink.setPoint(2, 0, pt3);
123 pt3.x = 196;
124 pt3.y = 96;
125 ink.setPoint(2, 1, pt3);
126 pt3.x = 221;
127 pt3.y = 121;
128 ink.setPoint(2, 2, pt3);
129 pt3.x = 288;
130 pt3.y = 188;
131 ink.setPoint(2, 3, pt3);
132 ink.setColor(new ColorPt(0, 1, 1), 3);
133 first_page.annotPushBack(ink);
134
135 }
136
137 static void AnnotationLowLevelAPI(PDFDoc doc) throws PDFNetException {
138 Page page = doc.getPageIterator().next();
139
140 Obj annots = page.getAnnots();
141
142 if (annots == null) {
143 // If there are no annotations, create a new annotation
144 // array for the page.
145 annots = doc.createIndirectArray();
146 page.getSDFObj().put("Annots", annots);
147 }
148
149 // Create a Text annotation
150 Obj annot = doc.createIndirectDict();
151 annot.putName("Subtype", "Text");
152 annot.putBool("Open", true);
153 annot.putString("Contents", "The quick brown fox ate the lazy mouse.");
154 annot.putRect("Rect", 266, 116, 430, 204);
155
156 // Insert the annotation in the page annotation array
157 annots.pushBack(annot);
158
159 // Create a Link annotation
160 Obj link1 = doc.createIndirectDict();
161 link1.putName("Subtype", "Link");
162 Destination dest = Destination.createFit(doc.getPage(2));
163 link1.put("Dest", dest.getSDFObj());
164 link1.putRect("Rect", 85, 705, 503, 661);
165 annots.pushBack(link1);
166
167 // Create another Link annotation
168 Obj link2 = doc.createIndirectDict();
169 link2.putName("Subtype", "Link");
170 Destination dest2 = Destination.createFit(doc.getPage(3));
171 link2.put("Dest", dest2.getSDFObj());
172 link2.putRect("Rect", 85, 638, 503, 594);
173 annots.pushBack(link2);
174
175 // Note that PDFNet APi can be used to modify existing annotations.
176 // In the following example we will modify the second link annotation
177 // (link2) so that it points to the 10th page. We also use a different
178 // destination page fit type.
179
180 // link2 = annots.GetAt(annots.Size()-1);
181 link2.put("Dest",
182 Destination.createXYZ(doc.getPage(10), 100, 792 - 70, 10).getSDFObj());
183
184 // Create a third link annotation with a hyperlink action (all other
185 // annotation types can be created in a similar way)
186 Obj link3 = doc.createIndirectDict();
187 link3.putName("Subtype", "Link");
188 link3.putRect("Rect", 85, 570, 503, 524);
189
190 // Create a URI action
191 Obj action = link3.putDict("A");
192 action.putName("S", "URI");
193 action.putString("URI", "http://www.pdftron.com");
194
195 annots.pushBack(link3);
196 }
197
198
199 static void CreateTestAnnots(PDFDoc doc) throws PDFNetException {
200 ElementWriter ew = new ElementWriter();
201 ElementBuilder eb = new ElementBuilder();
202 Element element;
203
204 Page first_page = doc.pageCreate(new Rect(0, 0, 600, 600));
205 doc.pagePushBack(first_page);
206 ew.begin(first_page, ElementWriter.e_overlay, false); // begin writing to this page
207 ew.end(); // save changes to the current page
208
209 //
210 // Test of a free text annotation.
211 //
212 {
213 FreeText txtannot = FreeText.create(doc, new Rect(10, 400, 160, 570));
214 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
215 "\n\nAha!\n\nAnd there was much rejoicing!");
216 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
217 txtannot.setQuaddingFormat(0);
218 first_page.annotPushBack(txtannot);
219 txtannot.refreshAppearance();
220 }
221 {
222 FreeText txtannot = FreeText.create(doc, new Rect(100, 100, 350, 500));
223 txtannot.setContentRect(new Rect(200, 200, 350, 500));
224 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
225 "\n\nAha!\n\nAnd there was much rejoicing!");
226 txtannot.setCalloutLinePoints(new Point(200, 300), new Point(150, 290), new Point(110, 110));
227 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
228 txtannot.setEndingStyle(Line.e_ClosedArrow);
229 txtannot.setColor(new ColorPt(0, 1, 0));
230 txtannot.setQuaddingFormat(1);
231 first_page.annotPushBack(txtannot);
232 txtannot.refreshAppearance();
233 }
234 {
235 FreeText txtannot = FreeText.create(doc, new Rect(400, 10, 550, 400));
236 txtannot.setContents("\n\nSome swift brown fox snatched a gray hare out of the air by freezing it with an angry glare." +
237 "\n\nAha!\n\nAnd there was much rejoicing!");
238 txtannot.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 1, 10, 20));
239 txtannot.setColor(new ColorPt(0, 0, 1));
240 txtannot.setOpacity(0.2);
241 txtannot.setQuaddingFormat(2);
242 first_page.annotPushBack(txtannot);
243 txtannot.refreshAppearance();
244 }
245
246 Page page = doc.pageCreate(new Rect(0, 0, 600, 600));
247 doc.pagePushBack(page);
248 ew.begin(page, ElementWriter.e_overlay, false); // begin writing to this page
249 eb.reset(); // Reset the GState to default
250 ew.end(); // save changes to the current page
251
252 {
253 //Create a Line annotation...
254 Line line = Line.create(doc, new Rect(250, 250, 400, 400));
255 line.setStartPoint(new Point(350, 270));
256 line.setEndPoint(new Point(260, 370));
257 line.setStartStyle(Line.e_Square);
258 line.setEndStyle(Line.e_Circle);
259 line.setColor(new ColorPt(.3, .5, 0), 3);
260 line.setContents("Dashed Captioned");
261 line.setShowCaption(true);
262 line.setCaptionPosition(Line.e_Top);
263 double[] dash = {2, 2.0};
264 line.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 2, 0, 0, dash));
265 line.refreshAppearance();
266 page.annotPushBack(line);
267 }
268 {
269 Line line = Line.create(doc, new Rect(347, 377, 600, 600));
270 line.setStartPoint(new Point(385, 410));
271 line.setEndPoint(new Point(540, 555));
272 line.setStartStyle(Line.e_Circle);
273 line.setEndStyle(Line.e_OpenArrow);
274 line.setColor(new ColorPt(1, 0, 0), 3);
275 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
276 line.setContents("Inline Caption");
277 line.setShowCaption(true);
278 line.setCaptionPosition(Line.e_Inline);
279 line.setLeaderLineExtensionLength(4.);
280 line.setLeaderLineLength(-12.);
281 line.setLeaderLineOffset(2.);
282 line.refreshAppearance();
283 page.annotPushBack(line);
284 }
285 {
286 Line line = Line.create(doc, new Rect(10, 400, 200, 600));
287 line.setStartPoint(new Point(25, 426));
288 line.setEndPoint(new Point(180, 555));
289 line.setStartStyle(Line.e_Circle);
290 line.setEndStyle(Line.e_Square);
291 line.setColor(new ColorPt(0, 0, 1), 3);
292 line.setInteriorColor(new ColorPt(1, 0, 0), 3);
293 line.setContents("Offset Caption");
294 line.setShowCaption(true);
295 line.setCaptionPosition(Line.e_Top);
296 line.setTextHOffset(-60);
297 line.setTextVOffset(10);
298 line.refreshAppearance();
299 page.annotPushBack(line);
300 }
301 {
302 Line line = Line.create(doc, new Rect(200, 10, 400, 70));
303 line.setStartPoint(new Point(220, 25));
304 line.setEndPoint(new Point(370, 60));
305 line.setStartStyle(Line.e_Butt);
306 line.setEndStyle(Line.e_OpenArrow);
307 line.setColor(new ColorPt(0, 0, 1), 3);
308 line.setContents("Regular Caption");
309 line.setShowCaption(true);
310 line.setCaptionPosition(Line.e_Top);
311 line.refreshAppearance();
312 page.annotPushBack(line);
313 }
314 {
315 Line line = Line.create(doc, new Rect(200, 70, 400, 130));
316 line.setStartPoint(new Point(220, 111));
317 line.setEndPoint(new Point(370, 78));
318 line.setStartStyle(Line.e_Circle);
319 line.setEndStyle(Line.e_Diamond);
320 line.setContents("Circle to Diamond");
321 line.setColor(new ColorPt(0, 0, 1), 3);
322 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
323 line.setShowCaption(true);
324 line.setCaptionPosition(Line.e_Top);
325 line.refreshAppearance();
326 page.annotPushBack(line);
327 }
328 {
329 Line line = Line.create(doc, new Rect(10, 100, 160, 200));
330 line.setStartPoint(new Point(15, 110));
331 line.setEndPoint(new Point(150, 190));
332 line.setStartStyle(Line.e_Slash);
333 line.setEndStyle(Line.e_ClosedArrow);
334 line.setContents("Slash to CArrow");
335 line.setColor(new ColorPt(1, 0, 0), 3);
336 line.setInteriorColor(new ColorPt(0, 1, 1), 3);
337 line.setShowCaption(true);
338 line.setCaptionPosition(Line.e_Top);
339 line.refreshAppearance();
340 page.annotPushBack(line);
341 }
342 {
343 Line line = Line.create(doc, new Rect(270, 270, 570, 433));
344 line.setStartPoint(new Point(300, 400));
345 line.setEndPoint(new Point(550, 300));
346 line.setStartStyle(Line.e_RClosedArrow);
347 line.setEndStyle(Line.e_ROpenArrow);
348 line.setContents("ROpen & RClosed arrows");
349 line.setColor(new ColorPt(0, 0, 1), 3);
350 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
351 line.setShowCaption(true);
352 line.setCaptionPosition(Line.e_Top);
353 line.refreshAppearance();
354 page.annotPushBack(line);
355 }
356 {
357 Line line = Line.create(doc, new Rect(195, 395, 205, 505));
358 line.setStartPoint(new Point(200, 400));
359 line.setEndPoint(new Point(200, 500));
360 line.refreshAppearance();
361 page.annotPushBack(line);
362 }
363 {
364 Line line = Line.create(doc, new Rect(55, 299, 150, 301));
365 line.setStartPoint(new Point(55, 300));
366 line.setEndPoint(new Point(155, 300));
367 line.setStartStyle(Line.e_Circle);
368 line.setEndStyle(Line.e_Circle);
369 line.setContents("Caption that's longer than its line.");
370 line.setColor(new ColorPt(1, 0, 1), 3);
371 line.setInteriorColor(new ColorPt(0, 1, 0), 3);
372 line.setShowCaption(true);
373 line.setCaptionPosition(Line.e_Top);
374 line.refreshAppearance();
375 page.annotPushBack(line);
376 }
377 {
378 Line line = Line.create(doc, new Rect(300, 200, 390, 234));
379 line.setStartPoint(new Point(310, 210));
380 line.setEndPoint(new Point(380, 220));
381 line.setColor(new ColorPt(0, 0, 0), 3);
382 line.refreshAppearance();
383 page.annotPushBack(line);
384 }
385
386 Page page3 = doc.pageCreate(new Rect(0, 0, 600, 600));
387 ew.begin(page3); // begin writing to the page
388 ew.end(); // save changes to the current page
389 doc.pagePushBack(page3);
390 {
391 Circle circle = Circle.create(doc, new Rect(300, 300, 390, 350));
392 circle.setColor(new ColorPt(0, 0, 0), 3);
393 circle.refreshAppearance();
394 page3.annotPushBack(circle);
395 }
396 {
397 Circle circle = Circle.create(doc, new Rect(100, 100, 200, 200));
398 circle.setColor(new ColorPt(0, 1, 0), 3);
399 circle.setInteriorColor(new ColorPt(0, 0, 1), 3);
400 double[] dash = {2, 4};
401 circle.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 3, 0, 0, dash));
402 circle.setPadding(2);
403 circle.refreshAppearance();
404 page3.annotPushBack(circle);
405 }
406 {
407 Square sq = Square.create(doc, new Rect(10, 200, 80, 300));
408 sq.setColor(new ColorPt(0, 0, 0), 3);
409 sq.refreshAppearance();
410 page3.annotPushBack(sq);
411 }
412 {
413 Square sq = Square.create(doc, new Rect(500, 200, 580, 300));
414 sq.setColor(new ColorPt(1, 0, 0), 3);
415 sq.setInteriorColor(new ColorPt(0, 1, 1), 3);
416 double[] dash = {4, 2};
417 sq.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 6, 0, 0, dash));
418 sq.setPadding(4);
419 sq.refreshAppearance();
420 page3.annotPushBack(sq);
421 }
422 {
423 Polygon poly = Polygon.create(doc, new Rect(5, 500, 125, 590));
424 poly.setColor(new ColorPt(1, 0, 0), 3);
425 poly.setInteriorColor(new ColorPt(1, 1, 0), 3);
426 poly.setVertex(0, new Point(12, 510));
427 poly.setVertex(1, new Point(100, 510));
428 poly.setVertex(2, new Point(100, 555));
429 poly.setVertex(3, new Point(35, 544));
430 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 4, 0, 0));
431 poly.setPadding(4);
432 poly.refreshAppearance();
433 page3.annotPushBack(poly);
434 }
435 {
436 PolyLine poly = PolyLine.create(doc, new Rect(400, 10, 500, 90));
437 poly.setColor(new ColorPt(1, 0, 0), 3);
438 poly.setInteriorColor(new ColorPt(0, 1, 0), 3);
439 poly.setVertex(0, new Point(405, 20));
440 poly.setVertex(1, new Point(440, 40));
441 poly.setVertex(2, new Point(410, 60));
442 poly.setVertex(3, new Point(470, 80));
443 poly.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 2, 0, 0));
444 poly.setPadding(4);
445 poly.setStartStyle(Line.e_RClosedArrow);
446 poly.setEndStyle(Line.e_ClosedArrow);
447 poly.refreshAppearance();
448 page3.annotPushBack(poly);
449 }
450 {
451 Link lk = Link.create(doc, new Rect(5, 5, 55, 24));
452 lk.refreshAppearance();
453 page3.annotPushBack(lk);
454 }
455
456
457 Page page4 = doc.pageCreate(new Rect(0, 0, 600, 600));
458 ew.begin(page4); // begin writing to the page
459 ew.end(); // save changes to the current page
460 doc.pagePushBack(page4);
461
462 {
463 ew.begin(page4);
464 Font font = Font.create(doc, Font.e_helvetica);
465 element = eb.createTextBegin(font, 16);
466 element.setPathFill(true);
467 ew.writeElement(element);
468 element = eb.createTextRun("Some random text on the page", font, 16);
469 element.setTextMatrix(1, 0, 0, 1, 100, 500);
470 ew.writeElement(element);
471 ew.writeElement(eb.createTextEnd());
472 ew.end();
473 }
474 {
475 Highlight hl = Highlight.create(doc, new Rect(100, 490, 150, 515));
476 hl.setColor(new ColorPt(0, 1, 0), 3);
477 hl.refreshAppearance();
478 page4.annotPushBack(hl);
479 }
480 {
481 Squiggly sq = Squiggly.create(doc, new Rect(100, 450, 250, 600));
482 sq.setQuadPoint(0, new QuadPoint(new Point(122, 455), new Point(240, 545), new Point(230, 595), new Point(101, 500)));
483 sq.refreshAppearance();
484 page4.annotPushBack(sq);
485 }
486 {
487 Caret cr = Caret.create(doc, new Rect(100, 40, 129, 69));
488 cr.setColor(new ColorPt(0, 0, 1), 3);
489 cr.setSymbol("P");
490 cr.refreshAppearance();
491 page4.annotPushBack(cr);
492 }
493
494
495 Page page5 = doc.pageCreate(new Rect(0, 0, 600, 600));
496 ew.begin(page5); // begin writing to the page
497 ew.end(); // save changes to the current page
498 doc.pagePushBack(page5);
499 FileSpec fs = FileSpec.create(doc, input_path + "butterfly.png", false);
500 Page page6 = doc.pageCreate(new Rect(0, 0, 600, 600));
501 ew.begin(page6); // begin writing to the page
502 ew.end(); // save changes to the current page
503 doc.pagePushBack(page6);
504
505 {
506 Text txt = Text.create(doc, new Point(10, 20));
507 txt.setIcon("UserIcon");
508 txt.setContents("User defined icon, unrecognized by appearance generator");
509 txt.setColor(new ColorPt(0, 1, 0));
510 txt.refreshAppearance();
511 page6.annotPushBack(txt);
512 }
513 {
514 Ink ink = Ink.create(doc, new Rect(100, 400, 200, 550));
515 ink.setColor(new ColorPt(0, 0, 1));
516 ink.setPoint(1, 3, new Point(220, 505));
517 ink.setPoint(1, 0, new Point(100, 490));
518 ink.setPoint(0, 1, new Point(120, 410));
519 ink.setPoint(0, 0, new Point(100, 400));
520 ink.setPoint(1, 2, new Point(180, 490));
521 ink.setPoint(1, 1, new Point(140, 440));
522 ink.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_solid, 3, 0, 0));
523 ink.refreshAppearance();
524 page6.annotPushBack(ink);
525 }
526
527
528 Page page7 = doc.pageCreate(new Rect(0, 0, 600, 600));
529 ew.begin(page7); // begin writing to the page
530 ew.end(); // save changes to the current page
531 doc.pagePushBack(page7);
532
533 {
534 Sound snd = Sound.create(doc, new Rect(100, 500, 120, 520));
535 snd.setColor(new ColorPt(1, 1, 0));
536 snd.setIcon(Sound.e_Speaker);
537 snd.refreshAppearance();
538 page7.annotPushBack(snd);
539 }
540 {
541 Sound snd = Sound.create(doc, new Rect(200, 500, 220, 520));
542 snd.setColor(new ColorPt(1, 1, 0));
543 snd.setIcon(Sound.e_Mic);
544 snd.refreshAppearance();
545 page7.annotPushBack(snd);
546 }
547
548 Page page8 = doc.pageCreate(new Rect(0, 0, 600, 600));
549 ew.begin(page8); // begin writing to the page
550 ew.end(); // save changes to the current page
551 doc.pagePushBack(page8);
552
553 for (int ipage = 0; ipage < 2; ++ipage) {
554 double px = 5, py = 520;
555 for (int istamp = 0; istamp <= RubberStamp.e_Draft; ++istamp) {
556 RubberStamp st = RubberStamp.create(doc, new Rect(1, 1, 100, 100));
557 st.SetIcon(istamp);
558 st.setContents(st.getIconName());
559 st.setRect(new Rect(px, py, px + 100, py + 25));
560 py -= 100;
561 if (py < 0) {
562 py = 520;
563 px += 200;
564 }
565 if (ipage == 0)
566 //page7.AnnotPushBack( st );
567 ;
568 else {
569 page8.annotPushBack(st);
570 st.refreshAppearance();
571 }
572 }
573 }
574 RubberStamp st = RubberStamp.create(doc, new Rect(400, 5, 550, 45));
575 st.setIcon("UserStamp");
576 st.setContents("User defined stamp");
577 page8.annotPushBack(st);
578 st.refreshAppearance();
579 }
580
581 public static void main(String[] args) {
582 PDFNet.initialize(PDFTronLicense.Key());
583
584 String output_path = "../../TestFiles/Output/";
585
586 try (PDFDoc doc = new PDFDoc((input_path + "numbered.pdf"))) {
587 doc.initSecurityHandler();
588
589 // An example of using SDF/Cos API to add any type of annotations.
590 AnnotationLowLevelAPI(doc);
591 doc.save(output_path + "annotation_test1.pdf", SDFDoc.SaveMode.LINEARIZED, null);
592 // output PDF doc
593 System.out.println("Done. Results saved in annotation_test1.pdf");
594
595 // An example of using the high-level PDFNet API to read existing annotations,
596 // to edit existing annotations, and to create new annotation from scratch.
597 AnnotationHighLevelAPI(doc);
598 doc.save(output_path + "annotation_test2.pdf", SDFDoc.SaveMode.LINEARIZED, null);
599 System.out.println("Done. Results saved in annotation_test2.pdf");
600 } catch (Exception e) {
601 System.out.println(e);
602 }
603
604 // an example of creating various annotations in a brand new document
605 try (PDFDoc doc1 = new PDFDoc()) {
606 CreateTestAnnots(doc1);
607 doc1.save(output_path + "new_annot_test_api.pdf", SDFDoc.SaveMode.LINEARIZED, null);
608 System.out.println("Saved new_annot_test_api.pdf");
609 } catch (Exception e) {
610 System.out.println(e);
611 }
612
613 PDFNet.terminate();
614 }
615
616}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales