> 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/core/search/replace.md).

# Replacing text & images in PDFs with Server/Desktop

Learn how to replace text or images in a PDF with this full code sample using pdftron.PDF.ContentReplacer. Enhance your PDF editing skills today! The Apryse Server SDK streamlines secure document proc

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

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc doc = new PDFDoc(filename);
ContentReplacer replacer = new ContentReplacer();
Page page = doc.GetPage(1);

// replace an image on the page
Rect target_region = page.GetMediaBox();
Image img = Image.Create(doc, imagename);
replacer.AddImage(target_region, img.GetSDFObj());

// replace a text placeholder
replacer.AddString("NAME", "John Smith");

// replace text in a given region
string text = "hello world";
replacer.AddText(target_region, text);
replacer.Process(page);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);
ContentReplacer replacer;
Page page = doc.GetPage(1);

// replace an image on the page
Rect target_region = page.GetMediaBox();
Image img = Image::Create(doc, imagename);
replacer.AddImage(target_region, img.GetSDFObj());

// replace a text placeholder
replacer.AddString("NAME", "John Smith");

// replace text in a given region
UString text("hello world");
replacer.AddText(target_region, text);
replacer.Process(page);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewPDFDoc(filename)
replacer := NewContentReplacer()
page := doc.GetPage(1)

// replace an image on the page
targetRegion := page.GetMediaBox()
img := ImageCreate(doc, imagename)
replacer.AddImage(targetRegion, img.GetSDFObj())

// replace a text placeholder
replacer.AddString("NAME", "John Smith")

// replace text in a given region
replacer.AddText(targetRegion, "Hello World");
replacer.Process(page)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);
ContentReplacer replacer = new ContentReplacer();
Page page = doc.getPage(1);

// replace an image on the page
Rect target_region = page.GetMediaBox();
Image img = Image.create(doc, imagename);
replacer.addImage(target_region, img.getSDFObj());

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

// replace text in a given region
String text = "hello world";
replacer.addText(target_region, text);
replacer.process(page);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
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);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)
val replacer = ContentReplacer()
val page = doc.getPage(1)

// replace an image on the page
val target_region = page.mediaBox
val img = Image.create(doc, imagename)
replacer.addImage(target_region, img.sdfObj)

// replace a text placeholder
replacer.addString("NAME", "John Smith")

// replace text in a given region
val text = "hello world"
replacer.addText(target_region, text)
replacer.process(page)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTContentReplacer *replacer = [[PTContentReplacer alloc] init];
PTPage *page = [doc GetPage: 1];

// replace an image on the page
PTPDFRect * target_region = [page GetMediaBox];
PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: imagename];
[replacer AddImage: target_region replacement_image: [img GetSDFObj]];

// replace a text placeholder
[replacer AddString: @"NAME" replacement_text: @"John Smith"];

// replace text in a given region
NSString* text = @"hello world";
[replacer AddText: target_region replacement_text: text];
[replacer Process: page];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc! = PTPDFDoc(filepath: filename)
let replacer: PTContentReplacer = PTContentReplacer()
let page: PTPage = doc.getPage(1)

// replace an image on the page
let target_region: PTPDFRect = page.getMediaBox()
let img: PTImage = PTImage.create(doc.getSDFDoc(), filename: imagename)
replacer.addImage(target_region, replacement_image: img.getSDFObj())

// replace a text placeholder
replacer.add("NAME", replacement_text: "John Smith")

// replace text in a given region
let text: String = "hello world";
replacer.addText(target_region, replacement_text: text)
replacer.process(page)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new PDFDoc($filename);
$replacer = new ContentReplacer();
$page = $doc->GetPage(1);

// replace an image on the page
$target_region = $page->GetMediaBox();
$img = Image::Create($doc->GetSDFDoc(), $imagename);
$replacer->AddImage($target_region, $img->GetSDFObj());

// replace a text placeholder
$replacer->AddString("NAME", "John Smith");

// replace text in a given region
$text = "hello world";
$replacer->AddText($target_region, "hello world");
$replacer->Process($page);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)
replacer = ContentReplacer()
page = doc.GetPage(1)

# replace an image on the page
target_region = page.GetMediaBox()
img = Image.Create(doc.GetSDFDoc(), imagename)
replacer.AddImage(target_region, img.GetSDFObj())

# replace a text placeholder
replacer.AddString("NAME", "John Smith")

# replace text in a given region
text = "hello world"
replacer.AddText(target_region, "hello world")
replacer.Process(page)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new(filename)
replacer = ContentReplacer.new()
page = doc.GetPage(1)

# replace an image on the page
target_region = page.GetMediaBox()
img = Image.Create(doc.GetSDFDoc(), imagename)
replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())

# replace a text placeholder
replacer.AddString("NAME", "John Smith")

# replace text in a given region
text = "hello world"
replacer.AddText(target_region, "hello world")
replacer.Process(page)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
Dim replacer As ContentReplacer = New ContentReplacer()
Dim page As Page = doc.GetPage(1)

' replace an image on the page
Dim target_region As Rect = page.GetMediaBox()
Dim img As Image = Image.Create(doc.GetSDFDoc(), imagename)
replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())

' replace a text placeholder
replacer.AddString("NAME", "John Smith")

' replace text in a given region
Dim text As String = "hello world"
replacer.AddText(target_region, "hello world")
replacer.Process(page)
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Replace PDF text or images](/core/get-started/samples/contentreplacertest.md) 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). Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.


---

# 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/core/search/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.
