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

# ContentReplacer

Sample C# 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 work

Sample C# 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 [Xamarin SDK](/core/get-started/frameworks/dotnet.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

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

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

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

using NUnit.Framework;

namespace MiscellaneousSamples
{
	/// <summary>
	//-----------------------------------------------------------------------------------------
	// The sample code illustrates how to use the ContentReplacer class to make using 
	// 'template' pdf documents easier.
	//-----------------------------------------------------------------------------------------
	/// </summary>
	[TestFixture]
	public class ContentReplacerTest
	{

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[Test]
		public static void Sample()
		{

			// Relative path to the folder containing test files.
			const string input_path =  "TestFiles/";


			// The following example illustrates how to replace an image in a certain region,
			// and how to change template text.
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "BusinessCardTemplate.pdf")))
				using (ContentReplacer replacer = new ContentReplacer())
				{
					doc.InitSecurityHandler();

					// first, replace the image on the first page
					Page page = doc.GetPage(1);
					Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
					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"), 0);
					Console.WriteLine("Done. Result saved in BusinessCard.pdf");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}


			// The following example illustrates how to replace text in a given region
			try
			{
				using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
				using (ContentReplacer replacer = new ContentReplacer())
				{
					doc.InitSecurityHandler();

					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"), 0);
					Console.WriteLine("Done. Result saved in ContentReplaced.pdf");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}
			Console.WriteLine("Done.");
		}
	}
}
```

{% 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/xamarin/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.
