Some test text!

menu

Replacing text & images in PDFs with JavaScript

Only available with the Full API

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

WebViewer({ fullAPI: true })  
    .then(instance => {
        const { PDFNet } = instance;
        await PDFNet.initialize();
        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);
    })

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

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close