1let imageName = 'path to your image file';
2let fileName = 'path to your PDF file';
3
4const { documentViewer, PDFNet } = instance.Core;
5
6document.getElementById('myBtn').addEventListener('click', async () => {
7 await PDFNet.initialize();
8 const doc = await documentViewer.getDocument().getPDFDoc();
9
10 // Run PDFNet methods with memory management
11 await PDFNet.runWithCleanup(async () => {
12
13 // lock the document before a write operation
14 // runWithCleanup will auto unlock when complete
15 doc.lock();
16 const s = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
17
18 // Specifies if the stamp is to be stamped as an annotation.
19 // note that stamps created with this setting do not work with SetAsBackground, HasStamps, and DeleteStamps, if annotation is true.
20 //s.setAsAnnotation(true);
21
22 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
23 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_courier);
24 await s.setFont(font);
25 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
26 await s.setFontColor(redColorPt);
27 await s.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_right);
28 await s.setAsBackground(false);
29 const pgSet = await PDFNet.PageSet.createRange(1, 2);
30 await s.stampText(doc, 'This is a title!', pgSet);
31
32 const img = await PDFNet.Image.createFromURL(doc, imageName);
33 s.setAsBackground(false);
34 const pgSetImage = await PDFNet.PageSet.createRange(1, 1);
35 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
36 await s.stampImage(doc, img, pgSetImage);
37
38 const srcDoc = await PDFNet.PDFDoc.createFromURL(fileName);
39 const srcPage = await srcDoc.getPage(1);
40 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
41 await s.stampPage(doc, srcPage, pgSet);
42 });
43
44 // clear the cache (rendered) data with the newly updated document
45 documentViewer.refreshAll();
46
47 // Update viewer to render with the new document
48 documentViewer.updateView();
49
50 // Refresh searchable and selectable text data with the new document
51 documentViewer.getDocument().refreshTextData();
52});