> 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/android/get-started/samples/contentreplacertest.md).

# ContentReplacer

Sample Java 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 wo

Sample Java 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 [Android SDK](/core/get-started/languages/java.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

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

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples;

import com.pdftron.android.pdfnetsdksamples.OutputListener;
import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
import com.pdftron.android.pdfnetsdksamples.R;
import com.pdftron.android.pdfnetsdksamples.util.Utils;
import com.pdftron.pdf.ContentReplacer;
import com.pdftron.pdf.Image;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.Page;
import com.pdftron.pdf.Rect;
import com.pdftron.sdf.SDFDoc;

import java.util.ArrayList;

public class ContentReplacerTest extends PDFNetSample {

	private static OutputListener mOutputListener;

	private static ArrayList<String> mFileList = new ArrayList<>();

    public ContentReplacerTest() {
        setTitle(R.string.sample_contentreplacer_title);
        setDescription(R.string.sample_contentreplacer_description);
    }

	@Override
	public void run(OutputListener outputListener) {
		super.run(outputListener);
		mOutputListener = outputListener;
		mFileList.clear();
		printHeader(outputListener);

        // The first step in every application using PDFNet is to initialize the
        // library and set the path to common PDF resources. The library is usually
        // initialized only once, but calling Initialize() multiple times is also fine.

        //--------------------------------------------------------------------------------
        // Example 1) Update a business card template with personalized info

        try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "BusinessCardTemplate.pdf").getAbsolutePath())) {
            doc.initSecurityHandler();

            ContentReplacer replacer = new ContentReplacer();
            Page page = doc.getPage(1);
            // first, replace the image on the first page
            Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
            replacer.addImage(page.getMediaBox(), img.getSDFObj());
            // next, replace the text place holders on the second page
            replacer.addString("NAME", "John Smith");
            replacer.addString("QUALIFICATIONS", "Philosophy Doctor");
            replacer.addString("JOB_TITLE", "Software Developer");
            replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd");
            replacer.addString("ADDRESS_LINE2", "Vancouver, BC");
            replacer.addString("PHONE_OFFICE", "604-730-8989");
            replacer.addString("PHONE_MOBILE", "604-765-4321");
            replacer.addString("EMAIL", "info@pdftron.com");
            replacer.addString("WEBSITE_URL", "http://www.pdftron.com");
            // finally, apply
            replacer.process(page);

            doc.save(Utils.createExternalFile("BusinessCard.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
            mOutputListener.println("Done. Result saved in BusinessCard.pdf");
        } catch (Exception e) {
            mOutputListener.printError(e.getStackTrace());
            return;
        }

        //--------------------------------------------------------------------------------
        // Example 2) Replace text in a region with new text

        try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
            doc.initSecurityHandler();

            ContentReplacer replacer = new ContentReplacer();
            Page page = doc.getPage(1);
            Rect target_region = page.getMediaBox();
            String replacement_text = "hello hello hello hello hello hello hello hello hello hello";
            replacer.addText(target_region, replacement_text);
            replacer.process(page);

            doc.save(Utils.createExternalFile("ContentReplaced.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
            mOutputListener.println("Done. Result saved in ContentReplaced.pdf");
        } catch (Exception e) {
            mOutputListener.printError(e.getStackTrace());
            return;
        }

        mOutputListener.println("Done.");

		for (String file : mFileList) {
			addToFileList(file);
		}
		printFooter(outputListener);
	}

}
```

{% endcode %}
{% endtab %}

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

```kotlin
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.pdf.ContentReplacer
import com.pdftron.pdf.Image
import com.pdftron.pdf.PDFDoc
import com.pdftron.sdf.SDFDoc
import java.util.*

class ContentReplacerTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_contentreplacer_title)
        setDescription(R.string.sample_contentreplacer_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)

        // The first step in every application using PDFNet is to initialize the
        // library and set the path to common PDF resources. The library is usually
        // initialized only once, but calling Initialize() multiple times is also fine.

        //--------------------------------------------------------------------------------
        // Example 1) Update a business card template with personalized info

        try {
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "BusinessCardTemplate.pdf")!!.absolutePath).use {doc ->
                doc.initSecurityHandler()

                val replacer = ContentReplacer()
                val page = doc.getPage(1)
                // first, replace the image on the first page
                val img = Image.create(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "peppers.jpg")!!.absolutePath)
                replacer.addImage(page.mediaBox, img.sdfObj)
                // next, replace the text place holders on the second page
                replacer.addString("NAME", "John Smith")
                replacer.addString("QUALIFICATIONS", "Philosophy Doctor")
                replacer.addString("JOB_TITLE", "Software Developer")
                replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd")
                replacer.addString("ADDRESS_LINE2", "Vancouver, BC")
                replacer.addString("PHONE_OFFICE", "604-730-8989")
                replacer.addString("PHONE_MOBILE", "604-765-4321")
                replacer.addString("EMAIL", "info@pdftron.com")
                replacer.addString("WEBSITE_URL", "http://www.pdftron.com")
                // finally, apply
                replacer.process(page)

                doc.save(Utils.createExternalFile("BusinessCard.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
                mOutputListener!!.println("Done. Result saved in BusinessCard.pdf")
            }
        } catch (e: Exception) {
            mOutputListener!!.printError(e.stackTrace)
            return
        }

        //--------------------------------------------------------------------------------
        // Example 2) Replace text in a region with new text

        try {
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { doc ->
                doc.initSecurityHandler()

                val replacer = ContentReplacer()
                val page = doc.getPage(1)
                val target_region = page.mediaBox
                val replacement_text = "hello hello hello hello hello hello hello hello hello hello"
                replacer.addText(target_region, replacement_text)
                replacer.process(page)

                doc.save(Utils.createExternalFile("ContentReplaced.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
                mOutputListener!!.println("Done. Result saved in ContentReplaced.pdf")
            }
        } catch (e: Exception) {
            mOutputListener!!.printError(e.stackTrace)
            return
        }

        mOutputListener!!.println("Done.")

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
```

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


---

# 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/android/get-started/samples/contentreplacertest.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.
