> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/edit-page-content/replace.md).

# Replacing text & images in PDFs using Apryse's Web SDK

Learn how to replace text or images in a PDF using pdftron.PDF.ContentReplacer. Find code samples for searching and replacing text strings and images in existing PDFs like business cards and templates

{% hint style="warning" %}
Make sure you have [Full API enabled in WebViewer.](/web/what-is-webviewer/full-api.md)
{% endhint %}

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

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  fullAPI: true,
  // other constructor options
}, viewerElement).then(instance => {
  const { documentViewer, PDFNet } = instance.Core;

  document.getElementById('myBtn').addEventListener('click', async () => {
    await PDFNet.initialize();
    const doc = await documentViewer.getDocument().getPDFDoc();

    // Run PDFNet methods with memory management
    await PDFNet.runWithCleanup(async () => {

      // lock the document before a write operation
      // runWithCleanup will auto unlock when complete
      doc.lock(); 

      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);
    });

    // clear the cache (rendered) data with the newly updated document
    documentViewer.refreshAll();

    // Update viewer to render with the new document
    documentViewer.updateView();

    // Refresh searchable and selectable text data with the new document
    documentViewer.getDocument().refreshTextData();
  });
})
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [PDFNet.PDFDoc.createFromUrl](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.PDFDoc.getPage](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getPage__anchor) [PDFNet.Page.getMediaBox](https://sdk.apryse.com/api/web/Core.PDFNet.Page.html#getMediaBox__anchor) [PDFNet.Image.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.Image.html#.createFromURL__anchor) [PDFNet.ContentReplacer.create](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#.create__anchor) [PDFNet.ContentReplacer.addImage](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addImage__anchor) [PDFNet.ContentReplacer.addString](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addString__anchor) [PDFNet.ContentReplacer.addText](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addText__anchor) [PDFNet.ContentReplacer.process](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#process__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  fullAPI: true,
  // other constructor options
}, viewerElement).then(instance => {
  const { docViewer, PDFNet } = instance;

  document.getElementById('myBtn').addEventListener('click', async () => {
    await PDFNet.initialize();
    const doc = await docViewer.getDocument().getPDFDoc();

    // Run PDFNet methods with memory management
    await PDFNet.runWithCleanup(async () => {

      // lock the document before a write operation
      // runWithCleanup will auto unlock when complete
      doc.lock(); 

      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);
    });

    // clear the cache (rendered) data with the newly updated document
    docViewer.refreshAll();

    // Update viewer to render with the new document
    docViewer.updateView();

    // Refresh searchable and selectable text data with the new document
    docViewer.getDocument().refreshTextData();
  });
})
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [PDFNet.PDFDoc.createFromUrl](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.PDFDoc.getPage](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getPage__anchor) [PDFNet.Page.getMediaBox](https://sdk.apryse.com/api/web/Core.PDFNet.Page.html#getMediaBox__anchor) [PDFNet.Image.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.Image.html#.createFromURL__anchor) [PDFNet.ContentReplacer.create](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#.create__anchor) [PDFNet.ContentReplacer.addImage](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addImage__anchor) [PDFNet.ContentReplacer.addString](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addString__anchor) [PDFNet.ContentReplacer.addText](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#addText__anchor) [PDFNet.ContentReplacer.process](https://sdk.apryse.com/api/web/Core.PDFNet.ContentReplacer.html#process__anchor)
{% endtab %}
{% endtabs %}

[Replace PDF text or images](/web/get-started/samples.md#contentreplacer) 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).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/edit-page-content/replace.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
