Sample JavaScript code to use Apryse SDK for searching and replacing text strings and images inside existing PDF files (e.g. business cards and other PDF templates). Unlike PDF forms, the ContentReplacer works on actual PDF content and is not limited to static rectangular annotation regions. Learn more about our Web SDK and PDF Editing & Manipulation 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// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
7// Consult legal.txt regarding legal and license information.
8//---------------------------------------------------------------------------------------
9
10(exports => {
11
12
13
14
15
16
17
18 exports.runContentReplacer = () => {
19 const PDFNet = exports.Core.PDFNet;
20
21 const main = async () => {
22 try {
23 const inputUrl = '../TestFiles/';
24 const inputFilename = 'BusinessCardTemplate.pdf';
25 const outputFilename = 'BusinessCard.pdf';
26
27 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + inputFilename);
28 doc.initSecurityHandler();
29 doc.lock();
30
31 const replacer = await PDFNet.ContentReplacer.create();
32 const page = await doc.getPage(1);
33 const img = await PDFNet.Image.createFromURL(doc, inputUrl + 'peppers.jpg');
34
35 const region = await page.getMediaBox();
36 const replace = await img.getSDFObj();
37 await replacer.addImage(region, replace);
38 await replacer.addString('NAME', 'John Smith');
39 await replacer.addString('QUALIFICATIONS', 'Philosophy Doctor');
40 await replacer.addString('JOB_TITLE', 'Software Developer');
41 await replacer.addString('ADDRESS_LINE1', '#100 123 Software Rd');
42 await replacer.addString('ADDRESS_LINE2', 'Vancouver, BC');
43 await replacer.addString('PHONE_OFFICE', '604-730-8989');
44 await replacer.addString('PHONE_MOBILE', '604-765-4321');
45 await replacer.addString('EMAIL', 'info@pdftron.com');
46 await replacer.addString('WEBSITE_URL', 'http://www.pdftron.com');
47 await replacer.process(page);
48
49 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_remove_unused);
50 saveBufferAsPDFDoc(docbuf, outputFilename);
51
52 console.log('Done. Result saved in ' + outputFilename);
53 } catch (err) {
54 console.log(err);
55 }
56 try {
57 const inputUrl = '../TestFiles/';
58 const inputFilename = 'newsletter.pdf';
59 const outputFilename = 'ContentReplaced.pdf';
60
61 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + inputFilename);
62 doc.initSecurityHandler();
63 doc.lock();
64
65 const replacer = await PDFNet.ContentReplacer.create();
66 const page = await doc.getPage(1);
67 const region = await page.getMediaBox();
68 await replacer.addText(region, 'The quick onyx goblin jumps over the lazy dwarf');
69 await replacer.process(page);
70
71 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_remove_unused);
72 saveBufferAsPDFDoc(docbuf, outputFilename);
73
74 console.log('Done. Result saved in ' + outputFilename);
75 console.log('Done.');
76 } catch (err) {
77 console.log(err);
78 }
79 };
80 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
81 PDFNet.runWithCleanup(main);
82 };
83})(window);
84// eslint-disable-next-line spaced-comment
85//# sourceURL=ContentReplacerTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales