1(exports => {
2 const PDFNet = exports.Core.PDFNet;
3 const runScript = () => {
4 const ModAnnotations = async doc => {
5 const imagefile = '../../samples/full-apis/TestFiles/grayscale.tif';
6
7 await PDFNet.startDeallocateStack(); // start stack-based deallocation. All objects will be deallocated by end of function
8 // The following code snippet traverses all annotations in the document
9 console.log('Traversing all annotations in the document...');
10 const apWriter = await PDFNet.ElementWriter.create();
11 const apBuilder = await PDFNet.ElementBuilder.create();
12
13 const sigImg = await PDFNet.Image.createFromURL(doc, imagefile);
14
15 const itr = await doc.getPageIterator(1);
16 let numMod = 0;
17
18 for (itr; await itr.hasNext(); await itr.next()) {
19 const page = await itr.current();
20 const numAnnots = await page.getNumAnnots();
21
22 for (let i = 0; i < numAnnots; ++i) {
23 const annot = await page.getAnnot(i);
24 if (!(await annot.isValid())) {
25 continue;
26 }
27
28 const annotType = await annot.getType();
29 switch (annotType) {
30 case PDFNet.Annot.Type.e_Stamp: {
31 apWriter.begin(doc);
32 const w = await sigImg.getImageWidth();
33 const h = await sigImg.getImageHeight();
34 let apElement = await apBuilder.createImageScaled(sigImg, 0, 0, w, h);
35 apWriter.writePlacedElement(apElement);
36 let apObj = await apWriter.end();
37 apObj.putRect('BBox', 0, 0, w, h);
38 apObj.putName('Subtype', 'Form');
39 apObj.putName('Type', 'XObject');
40 apElement = await apBuilder.createFormFromStream(apObj);
41 apWriter.writePlacedElement(apElement);
42 apObj = await apWriter.end();
43 apObj.putRect('BBox', 0, 0, w, h);
44 apObj.putName('Subtype', 'Form');
45 apObj.putName('Type', 'XObject');
46 await annot.setAppearance(apObj);
47 numMod += 1;
48 break;
49 }
50 default:
51 break;
52 }
53 }
54 }
55
56 console.log('number of annotation modifications: ' + numMod);
57
58 await PDFNet.endDeallocateStack();
59 };
60
61 const main = async () => {
62 let doc = null;
63
64 try {
65 // todo load a document from url
66 const inputURL = '../../samples/full-apis/TestFiles/';
67 const inputFilename = 'fish_stamped.pdf';
68 const url = inputURL + inputFilename;
69 console.log('loading document from url: ' + url);
70 doc = await PDFNet.PDFDoc.createFromURL(url);
71 doc.initSecurityHandler();
72 doc.lock();
73 console.log('loaded document from url: ' + url);
74 // modify annotations
75 await ModAnnotations(doc);
76 // flatten annotations
77 doc.flattenAnnotations();
78 console.log('flattened document from url: ' + url);
79 return doc;
80 } catch (err) {
81 console.log(err.stack);
82 } finally {
83 if (doc) {
84 doc.unlock();
85 }
86 }
87 };
88 // use "runWithoutCleanup" to lock the document data and run callback, use "with out cleanup" so data isn't cleared afterwards
89 // add your own license key as the second parameter, e.g. PDFNet.runWithoutCleanup(main, 'YOUR_LICENSE_KEY')
90 return PDFNet.runWithoutCleanup(main);
91 };
92
93 window.addEventListener('viewerLoaded', () => {
94 PDFNet.initialize()
95 .then(() => runScript())
96 .then(async doc => {
97 instance.UI.loadDocument(doc);
98 console.log('finished script');
99 });
100 });
101})(window);
102// eslint-disable-next-line spaced-comment
103//# sourceURL=config.js