Some test text!

Loading...
Guides
Replace content

Replacing text & images in PDFs in Node.js

To find text or images and replace it in a PDF.

async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  const replacer = await PDFNet.ContentReplacer.create();
  const page = await doc.getPage(1);

  // replace an image on the page
  const target_region = await page.getMediaBox();
  const img = await PDFNet.Image.createFromURL(doc, imagename);
  await replacer.addImage(target_region, await img.getSDFObj());

  // replace a text placeholder
  await replacer.addString('NAME', 'John Smith');

  // replace text in a given region
  const text = "hello world";
  await replacer.addText(target_region, text);
  await replacer.process(page);
}
PDFNet.runWithCleanup(main);

Replace PDF text or images
Full code sample which shows how to use pdftron.PDF.ContentReplacer to search and replace text strings and images in existing PDF (e.g. business cards and other PDF templates).

Get the answers you need: Support

Upcoming Webinar: Live tech update & run-through: Redaction | March 31st at 11 am PT

PDFTron SDK

The Platform

NEW

© 2022 PDFTron Systems Inc. All rights reserved.

Privacy

Terms of Use