Sample JavaScript 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 Web SDK and PDF Annotation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6(exports => {
7 exports.runAnnotationTest = async () => {
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 const PDFNet = exports.Core.PDFNet;
44 const AnnotationLowLevelAPI = async doc => {
45 try {
46 await PDFNet.startDeallocateStack(); // start stack-based deallocation. All objects will be deallocated by end of function
47 const itr = await doc.getPageIterator(1);
48 const page = await itr.current();
49
50 let annots = await page.getAnnots();
51
52 if (annots == null) {
53 // If there are no annotations, create a new annotation
54 // array for the page.
55 annots = await doc.createIndirectArray();
56 const sdfDoc = await page.getSDFObj();
57 await sdfDoc.put('Annots', annots);
58 }
59
60 // Create a Text annotation
61 const annot = await doc.createIndirectDict();
62 await annot.putName('Subtype', 'Text');
63 await annot.putBool('Open', true);
64 await annot.putString('Contents', 'The quick brown fox ate the lazy mouse.');
65 await annot.putRect('Rect', 266, 116, 430, 204);
66
67 // Insert the annotation in the page annotation array
68 await annots.pushBack(annot);
69
70 // Create a Link annotation
71 const link1 = await doc.createIndirectDict();
72 await link1.putName('Subtype', 'Link');
73 const dest = await PDFNet.Destination.createFit(await doc.getPage(2));
74 await link1.put('Dest', await dest.getSDFObj());
75 await link1.putRect('Rect', 85, 705, 503, 661);
76 await annots.pushBack(link1);
77
78 // Create another Link annotation
79 const link2 = await doc.createIndirectDict();
80 await link2.putName('Subtype', 'Link');
81 const dest2 = await PDFNet.Destination.createFit(await doc.getPage(3));
82 await link2.put('Dest', await dest2.getSDFObj());
83 await link2.putRect('Rect', 85, 638, 503, 594);
84 await annots.pushBack(link2);
85
86 // link2 = annots.GetAt(annots.Size()-1);
87 const tenthPage = await doc.getPage(10);
88 // XYZ destination stands for 'left', 'top' and 'zoom' coordinates
89 const XYZDestination = await PDFNet.Destination.createXYZ(tenthPage, 100, 722, 10);
90 await link2.put('Dest', await XYZDestination.getSDFObj());
91
92 // Create a third link annotation with a hyperlink action (all other
93 // annotation types can be created in a similar way)
94 const link3 = await doc.createIndirectDict();
95 await link3.putName('Subtype', 'Link');
96 await link3.putRect('Rect', 85, 570, 503, 524);
97
98 // Create a URI action
99 const action = await link3.putDict('A');
100 await action.putName('S', 'URI');
101 await action.putString('URI', 'http://www.apryse.com');
102
103 await annots.pushBack(link3);
104 await PDFNet.endDeallocateStack();
105 } catch (err) {
106 console.log(err);
107 }
108 };
109
110 const AnnotationHighLevelAPI = async doc => {
111 await PDFNet.startDeallocateStack(); // start stack-based deallocation. All objects will be deallocated by end of function
112 let firstPage = await doc.getPage(1);
113
114 // The following code snippet traverses all annotations in the document
115 console.log('Traversing all annotations in the document...');
116
117 // let firstPage = await doc.getPage(1);
118
119 let pageNum = 0;
120 const itr = await doc.getPageIterator(1);
121 for (itr; await itr.hasNext(); await itr.next()) {
122 pageNum += 1;
123 console.log('Page ' + pageNum + ': ');
124 const page = await itr.current();
125 const numAnnots = await page.getNumAnnots();
126 for (let i = 0; i < numAnnots; ++i) {
127 const annot = await page.getAnnot(i);
128 if (!(await annot.isValid())) {
129 continue;
130 }
131
132 const annotSDF = await annot.getSDFObj();
133 const subType = await annotSDF.get('Subtype');
134 const subTypeVal = await subType.value();
135
136 let outputString = 'Annot Type: ' + (await subTypeVal.getName());
137 console.log(outputString);
138 const bbox = await annot.getRect();
139 outputString = ' Position: ' + bbox.x1 + ', ' + bbox.y1 + ', ' + bbox.x2 + ', ' + bbox.y2;
140 console.log(outputString);
141 const annotType = await annot.getType();
142 switch (annotType) {
143 case PDFNet.Annot.Type.e_Link:
144 {
145 const link = await PDFNet.LinkAnnot.createFromAnnot(annot);
146 const action = await link.getAction();
147 if (!(await action.isValid())) {
148 continue;
149 }
150
151 if ((await action.getType()) === PDFNet.Action.Type.e_GoTo) {
152 const dest = await action.getDest();
153 if (!(await dest.isValid())) {
154 console.log(' Destination is not valid');
155 } else {
156 const pageNumOut = await (await dest.getPage()).getIndex();
157 console.log(' Links to: page number ' + pageNumOut + ' in this document');
158 }
159 } else if ((await action.getType()) === PDFNet.Action.Type.e_URI) {
160 const SDFObj = await action.getSDFObj();
161 const URI = await SDFObj.get('URI');
162 const URIval = await URI.value();
163 const URIText = await URIval.getAsPDFText(); // An Exception is thrown if this is not a Obj::Type::e_string.
164 console.log(' Links to: ' + URIText); // Other get methods such as getNumber do not work either, although some do, so confusing.
165 // deallocate dictionary object on C side
166 URI.destroy();
167 }
168 }
169 break;
170 case PDFNet.Annot.Type.e_Widget:
171 break;
172 case PDFNet.Annot.Type.e_FileAttachment:
173 break;
174 default:
175 break;
176 }
177
178 await subType.destroy();
179 }
180 }
181 // create a hyperlink
182 firstPage = await doc.getPage(1);
183 const createURIAction = await PDFNet.Action.createURI(doc, 'http://www.apryse.com');
184 const linkRect = new PDFNet.Rect(85, 570, 503, 524);
185 const hyperlink = await PDFNet.LinkAnnot.create(doc, linkRect);
186 await hyperlink.setAction(createURIAction);
187 await firstPage.annotPushBack(hyperlink);
188
189 // Create an intra-document link...
190 const page3 = await doc.getPage(3);
191 const gotoPage3 = await PDFNet.Action.createGoto(await PDFNet.Destination.createFitH(page3, 0));
192 const link = await PDFNet.LinkAnnot.create(doc, new PDFNet.Rect(85, 458, 503, 502));
193 await link.setAction(gotoPage3);
194
195 // Set the annotation border width to 3 points...
196 const borderStyle = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 3, 0, 0);
197 link.setBorderStyle(borderStyle, false); // default false
198 const greenColorPt = await PDFNet.ColorPt.init(0, 0, 1, 0);
199 await link.setColorDefault(greenColorPt);
200 await firstPage.annotPushBack(link);
201
202 // Create a stamp annotation ...
203 const stamp = await PDFNet.RubberStampAnnot.create(doc, new PDFNet.Rect(30, 30, 300, 200));
204 await stamp.setIconName('Draft');
205 await firstPage.annotPushBack(stamp);
206
207 const ink = await PDFNet.InkAnnot.create(doc, new PDFNet.Rect(110, 10, 300, 200));
208 const pt3 = new PDFNet.Point(110, 10);
209 await ink.setPoint(0, 0, pt3);
210 pt3.x = 150;
211 pt3.y = 50;
212 await ink.setPoint(0, 1, pt3);
213 pt3.x = 190;
214 pt3.y = 60;
215 await ink.setPoint(0, 2, pt3);
216 pt3.x = 180;
217 pt3.y = 90;
218 await ink.setPoint(1, 0, pt3);
219 pt3.x = 190;
220 pt3.y = 95;
221 await ink.setPoint(1, 1, pt3);
222 pt3.x = 200;
223 pt3.y = 100;
224 await ink.setPoint(1, 2, pt3);
225 pt3.x = 166;
226 pt3.y = 86;
227 await ink.setPoint(2, 0, pt3);
228 pt3.x = 196;
229 pt3.y = 96;
230 await ink.setPoint(2, 1, pt3);
231 pt3.x = 221;
232 pt3.y = 121;
233 await ink.setPoint(2, 2, pt3);
234 pt3.x = 288;
235 pt3.y = 188;
236 await ink.setPoint(2, 3, pt3);
237 const cyanColorPt = await PDFNet.ColorPt.init(0, 1, 1, 0);
238 await ink.setColor(cyanColorPt, 3);
239 firstPage.annotPushBack(ink);
240
241 await PDFNet.endDeallocateStack();
242 };
243
244 const CreateTestAnnots = async doc => {
245 await PDFNet.startDeallocateStack();
246 const ew = await PDFNet.ElementWriter.create(); // elementWriter
247 const eb = await PDFNet.ElementBuilder.create(); // elementBuilder
248 let element;
249
250 const firstPage = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
251 doc.pagePushBack(firstPage);
252 ew.beginOnPage(firstPage, PDFNet.ElementWriter.WriteMode.e_overlay, false); // begin writing to this page
253 ew.end(); // save changes to the current page
254
255 // NOTE: The following code represents three different ways to create a text annotation.
256 {
257 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(10, 400, 160, 570));
258 await txtannot.setContents('\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!');
259 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
260 await txtannot.setBorderStyle(solidLine, true);
261 await txtannot.setQuaddingFormat(0);
262 await firstPage.annotPushBack(txtannot);
263 await txtannot.refreshAppearance();
264 }
265
266 {
267 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(100, 100, 350, 500));
268 await txtannot.setContentRect(new PDFNet.Rect(200, 200, 350, 500));
269 await txtannot.setContents('\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!');
270 await txtannot.setCalloutLinePoints(new PDFNet.Point(200, 300), new PDFNet.Point(150, 290), new PDFNet.Point(110, 110));
271 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
272 await txtannot.setBorderStyle(solidLine, true);
273 await txtannot.setEndingStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
274 const greenColorPt = await PDFNet.ColorPt.init(0, 1, 0, 0);
275 await txtannot.setColorDefault(greenColorPt); // default value of last param is 0
276 await txtannot.setQuaddingFormat(1);
277 await firstPage.annotPushBack(txtannot);
278 await txtannot.refreshAppearance();
279 }
280 {
281 const txtannot = await PDFNet.FreeTextAnnot.create(doc, new PDFNet.Rect(400, 10, 550, 400));
282 await txtannot.setContents('\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!');
283 const solidLine = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 1, 10, 20);
284 await txtannot.setBorderStyle(solidLine, true);
285 const redColorPt = await PDFNet.ColorPt.init(0, 0, 1, 0);
286 await txtannot.setColorDefault(redColorPt);
287 await txtannot.setOpacity(0.2);
288 await txtannot.setQuaddingFormat(2);
289 await firstPage.annotPushBack(txtannot);
290 await txtannot.refreshAppearance();
291 }
292 const page = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
293 doc.pagePushBack(page);
294 await ew.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_overlay, false);
295 await eb.reset();
296 await ew.end(); // save changes to the current page
297 {
298 // Create a Line annotation...
299 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(250, 250, 400, 400));
300 await line.setStartPoint(new PDFNet.Point(350, 270));
301 await line.setEndPoint(new PDFNet.Point(260, 370));
302 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Square);
303 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
304 const darkGreenColorPt = await PDFNet.ColorPt.init(0.3, 0.5, 0, 0);
305 await line.setColor(darkGreenColorPt, 3);
306 await line.setContents('Dashed Captioned');
307 await line.setShowCaption(true);
308 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
309 const dash = [2.0, 2.0];
310 const bStyle = await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 2, 0, 0, dash);
311 line.setBorderStyle(bStyle, false);
312 line.refreshAppearance();
313 page.annotPushBack(line);
314 }
315 {
316 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(347, 377, 600, 600));
317 await line.setStartPoint(new PDFNet.Point(385, 410));
318 await line.setEndPoint(new PDFNet.Point(540, 555));
319 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
320 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_OpenArrow);
321 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
322 await line.setColor(redColorPt, 3);
323 const greenColorPt = await PDFNet.ColorPt.init(0, 1, 0, 0);
324 await line.setInteriorColor(greenColorPt, 3);
325 await line.setContents('Inline Caption');
326 await line.setShowCaption(true);
327 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Inline);
328 await line.setLeaderLineExtensionLength(-4.0);
329 await line.setLeaderLineLength(-12);
330 await line.setLeaderLineOffset(2.0);
331 await line.refreshAppearance();
332 page.annotPushBack(line);
333 }
334 {
335 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(10, 400, 200, 600));
336 await line.setStartPoint(new PDFNet.Point(25, 426));
337 await line.setEndPoint(new PDFNet.Point(180, 555));
338 await line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
339 await line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Square);
340 const blueColorPt = await PDFNet.ColorPt.init(0, 0, 1, 0);
341 await line.setColor(blueColorPt, 3);
342 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
343 await line.setInteriorColor(redColorPt, 3);
344 await line.setContents('Offset Caption');
345 await line.setShowCaption(true);
346 await line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
347 await line.setTextHOffset(-60);
348 await line.setTextVOffset(10);
349 await line.refreshAppearance();
350 page.annotPushBack(line);
351 }
352 {
353 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(200, 10, 400, 70));
354 line.setStartPoint(new PDFNet.Point(220, 25));
355 line.setEndPoint(new PDFNet.Point(370, 60));
356 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Butt);
357 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_OpenArrow);
358 line.setColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
359 line.setContents('Regular Caption');
360 line.setShowCaption(true);
361 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
362 await line.refreshAppearance();
363 page.annotPushBack(line);
364 }
365 {
366 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(200, 70, 400, 130));
367 line.setStartPoint(new PDFNet.Point(220, 111));
368 line.setEndPoint(new PDFNet.Point(370, 78));
369 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
370 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Diamond);
371 line.setContents('Circle to Diamond');
372 line.setColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
373 line.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
374 line.setShowCaption(true);
375 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
376 line.refreshAppearance();
377 page.annotPushBack(line);
378 }
379 {
380 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(10, 100, 160, 200));
381 line.setStartPoint(new PDFNet.Point(15, 110));
382 line.setEndPoint(new PDFNet.Point(150, 190));
383 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Slash);
384 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
385 line.setContents('Slash to CArrow');
386 line.setColor(await PDFNet.ColorPt.init(1, 0, 0), 3);
387 line.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 1), 3);
388 line.setShowCaption(true);
389 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
390 line.refreshAppearance();
391 page.annotPushBack(line);
392 }
393 {
394 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(270, 270, 570, 433));
395 line.setStartPoint(new PDFNet.Point(300, 400));
396 line.setEndPoint(new PDFNet.Point(550, 300));
397 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_RClosedArrow);
398 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ROpenArrow);
399 line.setContents('ROpen & RClosed arrows');
400 line.setColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
401 line.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
402 line.setShowCaption(true);
403 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
404 line.refreshAppearance();
405 page.annotPushBack(line);
406 }
407 {
408 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(195, 395, 205, 505));
409 line.setStartPoint(new PDFNet.Point(200, 400));
410 line.setEndPoint(new PDFNet.Point(200, 500));
411 line.refreshAppearance();
412 page.annotPushBack(line);
413 }
414 {
415 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(55, 299, 150, 301));
416 line.setStartPoint(new PDFNet.Point(55, 300));
417 line.setEndPoint(new PDFNet.Point(155, 300));
418 line.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
419 line.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_Circle);
420 line.setContents("Caption that's longer than its line.");
421 line.setColor(await PDFNet.ColorPt.init(1, 0, 1), 3);
422 line.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
423 line.setShowCaption(true);
424 line.setCapPos(PDFNet.LineAnnot.CapPos.e_Top);
425 line.refreshAppearance();
426 page.annotPushBack(line);
427 }
428 {
429 const line = await PDFNet.LineAnnot.create(doc, new PDFNet.Rect(300, 200, 390, 234));
430 line.setStartPoint(new PDFNet.Point(310, 210));
431 line.setEndPoint(new PDFNet.Point(380, 220));
432 line.setColor(await PDFNet.ColorPt.init(0, 0, 0), 3);
433 line.refreshAppearance();
434 page.annotPushBack(line);
435 }
436 const page3 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
437 ew.beginOnPage(page3); // begin writing to the page
438 ew.end(); // save changes to the current page
439 doc.pagePushBack(page3);
440 {
441 const circle = await PDFNet.CircleAnnot.create(doc, new PDFNet.Rect(300, 300, 390, 350));
442 circle.setColor(await PDFNet.ColorPt.init(0, 0, 0), 3);
443 circle.refreshAppearance();
444 page3.annotPushBack(circle);
445 }
446 {
447 const circle = await PDFNet.CircleAnnot.create(doc, new PDFNet.Rect(100, 100, 200, 200));
448 circle.setColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
449 circle.setInteriorColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
450 const dash = [2, 4];
451 circle.setBorderStyle(await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 3, 0, 0, dash));
452 circle.setPadding(new PDFNet.Rect(2, 2, 2, 2));
453 circle.refreshAppearance();
454 page3.annotPushBack(circle);
455 }
456 {
457 const sq = await PDFNet.SquareAnnot.create(doc, new PDFNet.Rect(10, 200, 80, 300));
458 sq.setColor(await PDFNet.ColorPt.init(0, 0, 0), 3);
459 sq.refreshAppearance();
460 page3.annotPushBack(sq);
461 }
462
463 {
464 const sq = await PDFNet.SquareAnnot.create(doc, new PDFNet.Rect(500, 200, 580, 300));
465 sq.setColor(await PDFNet.ColorPt.init(1, 0, 0), 3);
466 sq.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 1), 3);
467 const dash = [4, 2];
468 sq.setBorderStyle(await PDFNet.AnnotBorderStyle.createWithDashPattern(PDFNet.AnnotBorderStyle.Style.e_dashed, 6, 0, 0, dash));
469 sq.setPadding(new PDFNet.Rect(4, 4, 4, 4));
470 sq.refreshAppearance();
471 page3.annotPushBack(sq);
472 }
473
474 {
475 const poly = await PDFNet.PolygonAnnot.create(doc, new PDFNet.Rect(5, 500, 125, 590));
476 poly.setColor(await PDFNet.ColorPt.init(1, 0, 0), 3);
477 poly.setInteriorColor(await PDFNet.ColorPt.init(1, 1, 0), 3);
478 poly.setVertex(0, new PDFNet.Point(12, 510));
479 poly.setVertex(1, new PDFNet.Point(100, 510));
480 poly.setVertex(2, new PDFNet.Point(100, 555));
481 poly.setVertex(3, new PDFNet.Point(35, 544));
482 const solidBorderStyle = await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 4, 0, 0);
483 poly.setBorderStyle(solidBorderStyle);
484 poly.setPadding(new PDFNet.Rect(4, 4, 4, 4));
485 poly.refreshAppearance();
486 page3.annotPushBack(poly);
487 }
488 {
489 const poly = await PDFNet.PolyLineAnnot.create(doc, new PDFNet.Rect(400, 10, 500, 90));
490 poly.setColor(await PDFNet.ColorPt.init(1, 0, 0), 3);
491 poly.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
492 poly.setVertex(0, new PDFNet.Point(405, 20));
493 poly.setVertex(1, new PDFNet.Point(440, 40));
494 poly.setVertex(2, new PDFNet.Point(410, 60));
495 poly.setVertex(3, new PDFNet.Point(470, 80));
496 poly.setBorderStyle(await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 2, 0, 0));
497 poly.setPadding(new PDFNet.Rect(4, 4, 4, 4));
498 poly.setStartStyle(PDFNet.LineAnnot.EndingStyle.e_RClosedArrow);
499 poly.setEndStyle(PDFNet.LineAnnot.EndingStyle.e_ClosedArrow);
500 poly.refreshAppearance();
501 page3.annotPushBack(poly);
502 }
503 {
504 const lk = await PDFNet.LinkAnnot.create(doc, new PDFNet.Rect(5, 5, 55, 24));
505 // lk.setColor(await PDFNet.ColorPt.init(0,1,0), 3 );
506 lk.refreshAppearance();
507 page3.annotPushBack(lk);
508 }
509
510 const page4 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
511 ew.beginOnPage(page4); // begin writing to the page
512 ew.end(); // save changes to the current page
513 doc.pagePushBack(page4);
514
515 {
516 ew.beginOnPage(page4);
517 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_helvetica);
518 element = await eb.createTextBeginWithFont(font, 16);
519 element.setPathFill(true);
520 ew.writeElement(element);
521 element = await eb.createTextRun('Some random text on the page', font, 16);
522 element.setTextMatrixEntries(1, 0, 0, 1, 100, 500);
523 ew.writeElement(element);
524 ew.writeElement(await eb.createTextEnd());
525 ew.end();
526 }
527 {
528 const hl = await PDFNet.HighlightAnnot.create(doc, new PDFNet.Rect(100, 490, 150, 515));
529 hl.setColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
530 hl.refreshAppearance();
531 page4.annotPushBack(hl);
532 }
533 {
534 const sq = await PDFNet.SquigglyAnnot.create(doc, new PDFNet.Rect(100, 450, 250, 600));
535 // sq.setColor(await PDFNet.ColorPt.init(1,0,0), 3 );
536 sq.setQuadPoint(0, new PDFNet.QuadPoint(122, 455, 240, 545, 230, 595, 101, 500));
537 sq.refreshAppearance();
538 page4.annotPushBack(sq);
539 }
540 {
541 const cr = await PDFNet.CaretAnnot.create(doc, new PDFNet.Rect(100, 40, 129, 69));
542 cr.setColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
543 cr.setSymbol('P');
544 cr.refreshAppearance();
545 page4.annotPushBack(cr);
546 }
547
548 const page5 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
549 ew.beginOnPage(page5); // begin writing to the page
550 ew.end(); // save changes to the current page
551 doc.pagePushBack(page5);
552 const fs = await PDFNet.FileSpec.create(doc, '../TestFiles/butterfly.png', false);
553 const page6 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
554 ew.beginOnPage(page6); // begin writing to the page
555 ew.end(); // save changes to the current page
556 doc.pagePushBack(page6);
557
558 for (let ipage = 0; ipage < 2; ++ipage) {
559 for (let iann = 0; iann < 100; iann++) {
560 if (!(iann > PDFNet.FileAttachmentAnnot.Icon.e_Tag)) {
561 const fa = await PDFNet.FileAttachmentAnnot.createWithFileSpec(doc, new PDFNet.Rect(50 + 50 * iann, 100, 70 + 50 * iann, 120), fs, iann);
562 if (ipage) {
563 fa.setColor(await PDFNet.ColorPt.init(1, 1, 0));
564 }
565 fa.refreshAppearance();
566 if (ipage === 0) {
567 page5.annotPushBack(fa);
568 } else {
569 page6.annotPushBack(fa);
570 }
571 }
572 if (iann > PDFNet.TextAnnot.Icon.e_Note) {
573 break;
574 }
575 const txt = await PDFNet.TextAnnot.create(doc, new PDFNet.Rect(10 + iann * 50, 200, 30 + iann * 50, 220));
576 txt.setIcon(iann);
577 txt.setContents(await txt.getIconName());
578 if (ipage) {
579 txt.setColor(await PDFNet.ColorPt.init(1, 1, 0));
580 }
581 txt.refreshAppearance();
582 if (ipage === 0) {
583 page5.annotPushBack(txt);
584 } else {
585 page6.annotPushBack(txt);
586 }
587 }
588 }
589 {
590 const txt = await PDFNet.TextAnnot.create(doc, new PDFNet.Rect(10, 20, 30, 40));
591 txt.setIconName('UserIcon');
592 txt.setContents('User defined icon, unrecognized by appearance generator');
593 txt.setColor(await PDFNet.ColorPt.init(0, 1, 0));
594 txt.refreshAppearance();
595 page6.annotPushBack(txt);
596 }
597 {
598 const ink = await PDFNet.InkAnnot.create(doc, new PDFNet.Rect(100, 400, 200, 550));
599 ink.setColor(await PDFNet.ColorPt.init(0, 0, 1));
600 ink.setPoint(1, 3, new PDFNet.Point(220, 505));
601 ink.setPoint(1, 0, new PDFNet.Point(100, 490));
602 ink.setPoint(0, 1, new PDFNet.Point(120, 410));
603 ink.setPoint(0, 0, new PDFNet.Point(100, 400));
604 ink.setPoint(1, 2, new PDFNet.Point(180, 490));
605 ink.setPoint(1, 1, new PDFNet.Point(140, 440));
606 ink.setBorderStyle(await PDFNet.AnnotBorderStyle.create(PDFNet.AnnotBorderStyle.Style.e_solid, 3, 0, 0));
607 ink.refreshAppearance();
608 page6.annotPushBack(ink);
609 }
610
611 const page7 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
612 ew.beginOnPage(page7); // begin writing to the page
613 ew.end(); // save changes to the current page
614 doc.pagePushBack(page7);
615
616 {
617 const snd = await PDFNet.SoundAnnot.create(doc, new PDFNet.Rect(100, 500, 120, 520));
618 snd.setColor(await PDFNet.ColorPt.init(1, 1, 0));
619 snd.setIcon(PDFNet.SoundAnnot.Icon.e_Speaker);
620 snd.refreshAppearance();
621 page7.annotPushBack(snd);
622 }
623 {
624 const snd = await PDFNet.SoundAnnot.create(doc, new PDFNet.Rect(200, 500, 220, 520));
625 snd.setColor(await PDFNet.ColorPt.init(1, 1, 0));
626 snd.setIcon(PDFNet.SoundAnnot.Icon.e_Mic);
627 snd.refreshAppearance();
628 page7.annotPushBack(snd);
629 }
630
631 const page8 = await doc.pageCreate(new PDFNet.Rect(0, 0, 600, 600));
632 ew.beginOnPage(page8); // begin writing to the page
633 ew.end(); // save changes to the current page
634 doc.pagePushBack(page8);
635
636 for (let ipage = 0; ipage < 2; ++ipage) {
637 let px = 5;
638 let py = 520;
639 for (let istamp = PDFNet.RubberStampAnnot.Icon.e_Approved; istamp <= PDFNet.RubberStampAnnot.Icon.e_Draft; istamp++) {
640 const st = await PDFNet.RubberStampAnnot.create(doc, new PDFNet.Rect(1, 1, 100, 100));
641 st.setIcon(istamp);
642 st.setContents(await st.getIconName());
643 st.setRect(new PDFNet.Rect(px, py, px + 100, py + 25));
644 py -= 100;
645 if (py < 0) {
646 py = 520;
647 px += 200;
648 }
649 if (ipage === 0) {
650 // page7.annotPushBack( st );
651 } else {
652 page8.annotPushBack(st);
653 st.refreshAppearance();
654 }
655 }
656 }
657 const st = await PDFNet.RubberStampAnnot.create(doc, new PDFNet.Rect(400, 5, 550, 45));
658 st.setIconName('UserStamp');
659 st.setContents('User defined stamp');
660 page8.annotPushBack(st);
661 st.refreshAppearance();
662
663 await PDFNet.endDeallocateStack();
664 };
665
666 const main = async () => {
667 try {
668 const ret = 0;
669
670 const inputPath = '../TestFiles/';
671 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + 'numbered.pdf');
672 doc.initSecurityHandler();
673 doc.lock();
674
675 await AnnotationLowLevelAPI(doc);
676 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
677 saveBufferAsPDFDoc(docbuf, 'annotation_test1.pdf');
678
679 console.log('Done. Results saved in annotation_test1.pdf');
680
681 // eslint-disable-next-line @typescript-eslint/no-unused-vars
682 const firstPage = await doc.getPage(1);
683
684 await AnnotationHighLevelAPI(doc);
685 const docbuf2 = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
686 saveBufferAsPDFDoc(docbuf2, 'annotation_test2.pdf');
687 console.log('Done. Results saved in annotation_test2.pdf');
688
689 // creating various annotations in a brand new document
690 const docnew = await PDFNet.PDFDoc.create();
691 docnew.lock();
692 await CreateTestAnnots(docnew);
693 const doc1buf = await docnew.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
694 saveBufferAsPDFDoc(doc1buf, 'new_annot_test_api.pdf');
695 console.log('Saved new_annot_test_api.pdf');
696 return ret;
697 } catch (err) {
698 console.log(err);
699 }
700 };
701 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
702 PDFNet.runWithCleanup(main);
703 };
704})(window);
705// eslint-disable-next-line spaced-comment
706//# sourceURL=AnnotationTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales