> 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/get-started/samples/find-and-replace.md).

# Find and Replace in PDF

This sample shows how to use Find and Replace to replace text in a PDF Document

Sample code for using the Apryse SDK to perform in-place Find and Replace while preserving document layout and formatting. Sample code provided in Python, C++, C#, Java, and Objective-C.

Learn more about our [Server SDK](/core/get-started/get-started.md).

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

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/PageSet.h>
#include <Filters/MappedFile.h>
#include <Filters/FilterReader.h>
#include <Filters/FilterWriter.h>
#include <PDF/ElementWriter.h>
#include <PDF/ElementReader.h>
#include <PDF/FindReplace.h>
#include <PDF/FindReplaceOptions.h>

#include <iostream>
#include <fstream>
#include "../../LicenseKey/CPP/LicenseKey.h"

using namespace std;
using namespace pdftron;
using namespace SDF;
using namespace PDF;
using namespace Filters;

int main(int argc, char *argv[])
{
	int ret = 0;
	PDFNet::Initialize(LicenseKey);

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

	// The following sample illustrates how to find and replace text in a document
	try  
	{
		// Open a PDF document to edit
		PDFDoc doc(input_path + "find-replace-test.pdf");
		FindReplaceOptions options = FindReplaceOptions();

		// Set some find/replace options
		options.SetWholeWords(true);
		options.SetMatchCase(true);
		options.SetMatchMode(FindReplaceOptions::e_exact);
		options.SetReflowMode(FindReplaceOptions::e_para);
		options.SetAlignment(FindReplaceOptions::e_left);

		// Perform a Find/Replace finding "the" with "THE INCREDIBLE"
		FindReplace::FindReplaceText(doc, "the", "THE INCREDIBLE", options);

		// Save the edited PDF
		doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc::e_linearized);
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;
}
```

{% endcode %}
{% endtab %}

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

```csharp
//
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
//

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

// The following sample illustrates how to find and replace text in a document
namespace FindReplaceTestCS
{
	class Class1
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static Class1() {}
		
		[STAThread]
		static void Main(string[] args)
		{
			PDFNet.Initialize(PDFTronLicense.Key);

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

			try  
			{
				// Read a PDF document from a stream or pass-in a memory buffer...
				FileStream istm = new FileStream(input_path + "find-replace-test.pdf", FileMode.Open, FileAccess.Read);
				
				using (PDFDoc doc = new PDFDoc(istm))
				{
					FindReplaceOptions options = new FindReplaceOptions();

					// Set some find/replace options
					options.SetWholeWords(true);
					options.SetMatchCase(true);
					options.SetMatchMode(FindReplaceOptions.MatchType.e_exact);
					options.SetReflowMode(FindReplaceOptions.ReflowType.e_para);
					options.SetAlignment(FindReplaceOptions.HorizAlignment.e_left);

					// Perform a Find/Replace finding "the" with "THE INCREDIBLE"
					FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options);

					// Save the edited PDF
					doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc.SaveOptions.e_linearized);
					doc.Close();
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}
			PDFNet.Terminate();
		}
	}
}

```

{% endcode %}
{% endtab %}

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

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;

// The following sample illustrates how to find and replace text in a document
public class FindReplaceTest {

	public static void main(String[] args) 
	{
		PDFNet.initialize(PDFTronLicense.Key());

		// Relative path to the folder containing test files.
		String input_path = "../../TestFiles/";
		String output_path = "../../TestFiles/Output/";

		// Open a PDF document to edit
		try (PDFDoc doc = new PDFDoc(input_path + "find-replace-test.pdf"))
		{
			FindReplaceOptions options = new FindReplaceOptions();

			// Set some find/replace options
			options.SetWholeWords(true);
			options.SetMatchCase(true);
			options.SetMatchMode(FindReplaceOptions.MatchType.e_exact);
			options.SetReflowMode(FindReplaceOptions.ReflowType.e_para);
			options.SetAlignment(FindReplaceOptions.HorizAlignment.e_left);

			// Perform a Find/Replace finding "the" with "THE INCREDIBLE"
			FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options);

			// Save the edited PDF
			doc.Save(output_path + "find-replace-test-replaced.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
			doc.close();
		}
		catch (PDFNetException e)
		{
			e.printStackTrace();
			System.out.println(e);
		}

		PDFNet.terminate();
	}
}

```

{% endcode %}
{% endtab %}

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

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

// The following sample illustrates how to find and replace text in a document

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int ret = 0;
        [PTPDFNet Initialize: 0];

        NSString *inputPath = @"../../TestFiles/";
        NSString *outputPath = @"../../TestFiles/Output/";

        @try
        {
            // Open a PDF document to edit
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [inputPath stringByAppendingString:@"find-replace-test.pdf"]];

            PTFindReplaceOptions* options = [[PTFindReplaceOptions alloc] init];

            // Set some find/replace options
            [options SetWholeWords: true];
            [options SetMatchCase: true];
            [options SetMatchMode: e_exact];
            [options SetReflowMode: e_para];
            [options SetAlignment: e_left];

            // Perform a Find/Replace finding "the" with "THE INCREDIBLE"
            [PTFindReplace FindReplaceText : doc from: @"the" to: @"THE INCREDIBLE" options: options];

            // Save the edited PDF
            [doc SaveToFile : [outputPath stringByAppendingString:@"find-replace-test-replaced.pdf"] flags: e_ptlinearized];
            [doc Close];
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        [PTPDFNet Terminate: 0];
        return ret;
    }
}


```

{% endcode %}
{% endtab %}

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

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

import platform

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

# ---------------------------------------------------------------------------------------
# The following sample illustrates how to find and replace text in a PDF document.
# --------------------------------------------------------------------------------------

# Relative path to the folder containing the test files.
input_path = "../../TestFiles/"
output_path = "../../TestFiles/Output/"

def main():

    # 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.
    PDFNet.Initialize(LicenseKey)

    try:
        # Open a PDF document to edit
        doc = PDFDoc(input_path + "find-replace-test.pdf")
        options = FindReplaceOptions()

        # Set some find/replace options
        options.SetWholeWords(True)
        options.SetMatchCase(True)
        options.SetMatchMode(FindReplaceOptions.e_exact)
        options.SetReflowMode(FindReplaceOptions.e_para)
        options.SetAlignment(FindReplaceOptions.e_left)

        # Perform a Find/Replace finding "the" with "THE INCREDIBLE"
        FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options)

        # Save the edited PDF
        doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc.e_linearized)

    except Exception as e:
        print("Unable to perform Find and Replace, error: " + str(e))

    PDFNet.Terminate()
    print("Done.")


if __name__ == '__main__':
    main()


```

{% 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/core/get-started/samples/find-and-replace.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
