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
8 exports.runContentReplacer = () => {
9 const PDFNet = exports.Core.PDFNet;
10
11 const main = async () => {
12 try {
13 const inputUrl = '../TestFiles/';
14 const inputFilename = 'BusinessCardTemplate.pdf';
15 const outputFilename = 'BusinessCard.pdf';
16
17 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + inputFilename);
18 doc.initSecurityHandler();
19 doc.lock();
20
21 const replacer = await PDFNet.ContentReplacer.create();
22 const page = await doc.getPage(1);
23 const img = await PDFNet.Image.createFromURL(doc, inputUrl + 'peppers.jpg');
24
25 const region = await page.getMediaBox();
26 const replace = await img.getSDFObj();
27 await replacer.addImage(region, replace);
28 await replacer.addString('NAME', 'John Smith');
29 await replacer.addString('QUALIFICATIONS', 'Philosophy Doctor');
30 await replacer.addString('JOB_TITLE', 'Software Developer');
31 await replacer.addString('ADDRESS_LINE1', '#100 123 Software Rd');
32 await replacer.addString('ADDRESS_LINE2', 'Vancouver, BC');
33 await replacer.addString('PHONE_OFFICE', '604-730-8989');
34 await replacer.addString('PHONE_MOBILE', '604-765-4321');
35 await replacer.addString('EMAIL', 'info@pdftron.com');
36 await replacer.addString('WEBSITE_URL', 'http://www.pdftron.com');
37 await replacer.process(page);
38
39 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_remove_unused);
40 saveBufferAsPDFDoc(docbuf, outputFilename);
41
42 console.log('Done. Result saved in ' + outputFilename);
43 } catch (err) {
44 console.log(err);
45 }
46 try {
47 const inputUrl = '../TestFiles/';
48 const inputFilename = 'newsletter.pdf';
49 const outputFilename = 'ContentReplaced.pdf';
50
51 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + inputFilename);
52 doc.initSecurityHandler();
53 doc.lock();
54
55 const replacer = await PDFNet.ContentReplacer.create();
56 const page = await doc.getPage(1);
57 const region = await page.getMediaBox();
58 await replacer.addText(region, 'The quick onyx goblin jumps over the lazy dwarf');
59 await replacer.process(page);
60
61 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_remove_unused);
62 saveBufferAsPDFDoc(docbuf, outputFilename);
63
64 console.log('Done. Result saved in ' + outputFilename);
65 console.log('Done.');
66 } catch (err) {
67 console.log(err);
68 }
69 };
70 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
71 PDFNet.runWithCleanup(main);
72 };
73})(window);
74// eslint-disable-next-line spaced-comment
75//# sourceURL=ContentReplacerTest.js