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