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.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5#include <PDF/PDFNet.h>
6#include <PDF/PDFDoc.h>
7#include <PDF/PageSet.h>
8#include <Filters/MappedFile.h>
9#include <Filters/FilterReader.h>
10#include <Filters/FilterWriter.h>
11#include <PDF/ElementWriter.h>
12#include <PDF/ElementReader.h>
13#include <PDF/FindReplace.h>
14#include <PDF/FindReplaceOptions.h>
15
16#include <iostream>
17#include <fstream>
18#include "../../LicenseKey/CPP/LicenseKey.h"
19
20using namespace std;
21using namespace pdftron;
22using namespace SDF;
23using namespace PDF;
24using namespace Filters;
25
26int main(int argc, char *argv[])
27{
28 int ret = 0;
29 PDFNet::Initialize(LicenseKey);
30
31 // Relative path to the folder containing test files.
32 string input_path = "../../TestFiles/";
33 string output_path = "../../TestFiles/Output/";
34
35 // The following sample illustrates how to find and replace text in a document
36 try
37 {
38 // Open a PDF document to edit
39 PDFDoc doc(input_path + "find-replace-test.pdf");
40 FindReplaceOptions options = FindReplaceOptions();
41
42 // Set some find/replace options
43 options.SetWholeWords(true);
44 options.SetMatchCase(true);
45 options.SetMatchMode(FindReplaceOptions::e_exact);
46 options.SetReflowMode(FindReplaceOptions::e_para);
47 options.SetAlignment(FindReplaceOptions::e_left);
48
49 // Perform a Find/Replace finding "the" with "THE INCREDIBLE"
50 FindReplace::FindReplaceText(doc, "the", "THE INCREDIBLE", options);
51
52 // Save the edited PDF
53 doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc::e_linearized);
54 }
55 catch(Common::Exception& e)
56 {
57 cout << e << endl;
58 ret = 1;
59 }
60 catch(...)
61 {
62 cout << "Unknown Exception" << endl;
63 ret = 1;
64 }
65
66 PDFNet::Terminate();
67 return ret;
68}
1//
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using pdftron;
8using pdftron.Common;
9using pdftron.Filters;
10using pdftron.SDF;
11using pdftron.PDF;
12using pdftron.FDF;
13
14// The following sample illustrates how to find and replace text in a document
15namespace FindReplaceTestCS
16{
17 class Class1
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static Class1() {}
21
22 [STAThread]
23 static void Main(string[] args)
24 {
25 PDFNet.Initialize(PDFTronLicense.Key);
26
27 // Relative path to the folder containing test files.
28 string input_path = "../../../../TestFiles/";
29 string output_path = "../../../../TestFiles/Output/";
30
31 try
32 {
33 // Read a PDF document from a stream or pass-in a memory buffer...
34 FileStream istm = new FileStream(input_path + "find-replace-test.pdf", FileMode.Open, FileAccess.Read);
35
36 using (PDFDoc doc = new PDFDoc(istm))
37 {
38 FindReplaceOptions options = new FindReplaceOptions();
39
40 // Set some find/replace options
41 options.SetWholeWords(true);
42 options.SetMatchCase(true);
43 options.SetMatchMode(FindReplaceOptions.MatchType.e_exact);
44 options.SetReflowMode(FindReplaceOptions.ReflowType.e_para);
45 options.SetAlignment(FindReplaceOptions.HorizAlignment.e_left);
46
47 // Perform a Find/Replace finding "the" with "THE INCREDIBLE"
48 FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options);
49
50 // Save the edited PDF
51 doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc.SaveOptions.e_linearized);
52 doc.Close();
53 }
54 }
55 catch (PDFNetException e)
56 {
57 Console.WriteLine(e.Message);
58 }
59 PDFNet.Terminate();
60 }
61 }
62}
63
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.io.File;
7import java.io.IOException;
8
9import javax.imageio.ImageIO;
10
11import com.pdftron.common.PDFNetException;
12import com.pdftron.pdf.*;
13import com.pdftron.sdf.SDFDoc;
14
15// The following sample illustrates how to find and replace text in a document
16public class FindReplaceTest {
17
18 public static void main(String[] args)
19 {
20 PDFNet.initialize(PDFTronLicense.Key());
21
22 // Relative path to the folder containing test files.
23 String input_path = "../../TestFiles/";
24 String output_path = "../../TestFiles/Output/";
25
26 // Open a PDF document to edit
27 try (PDFDoc doc = new PDFDoc(input_path + "find-replace-test.pdf"))
28 {
29 FindReplaceOptions options = new FindReplaceOptions();
30
31 // Set some find/replace options
32 options.SetWholeWords(true);
33 options.SetMatchCase(true);
34 options.SetMatchMode(FindReplaceOptions.MatchType.e_exact);
35 options.SetReflowMode(FindReplaceOptions.ReflowType.e_para);
36 options.SetAlignment(FindReplaceOptions.HorizAlignment.e_left);
37
38 // Perform a Find/Replace finding "the" with "THE INCREDIBLE"
39 FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options);
40
41 // Save the edited PDF
42 doc.Save(output_path + "find-replace-test-replaced.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
43 doc.close();
44 }
45 catch (PDFNetException e)
46 {
47 e.printStackTrace();
48 System.out.println(e);
49 }
50
51 PDFNet.terminate();
52 }
53}
54
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9// The following sample illustrates how to find and replace text in a document
10
11int main(int argc, char *argv[])
12{
13 @autoreleasepool {
14 int ret = 0;
15 [PTPDFNet Initialize: 0];
16
17 NSString *inputPath = @"../../TestFiles/";
18 NSString *outputPath = @"../../TestFiles/Output/";
19
20 @try
21 {
22 // Open a PDF document to edit
23 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [inputPath stringByAppendingString:@"find-replace-test.pdf"]];
24
25 PTFindReplaceOptions* options = [[PTFindReplaceOptions alloc] init];
26
27 // Set some find/replace options
28 [options SetWholeWords: true];
29 [options SetMatchCase: true];
30 [options SetMatchMode: e_exact];
31 [options SetReflowMode: e_para];
32 [options SetAlignment: e_left];
33
34 // Perform a Find/Replace finding "the" with "THE INCREDIBLE"
35 [PTFindReplace FindReplaceText : doc from: @"the" to: @"THE INCREDIBLE" options: options];
36
37 // Save the edited PDF
38 [doc SaveToFile : [outputPath stringByAppendingString:@"find-replace-test-replaced.pdf"] flags: e_ptlinearized];
39 [doc Close];
40 }
41 @catch(NSException *e)
42 {
43 NSLog(@"%@", e.reason);
44 ret = 1;
45 }
46
47 [PTPDFNet Terminate: 0];
48 return ret;
49 }
50}
51
52
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11import platform
12
13sys.path.append("../../LicenseKey/PYTHON")
14from LicenseKey import *
15
16# ---------------------------------------------------------------------------------------
17# The following sample illustrates how to find and replace text in a PDF document.
18# --------------------------------------------------------------------------------------
19
20# Relative path to the folder containing the test files.
21input_path = "../../TestFiles/"
22output_path = "../../TestFiles/Output/"
23
24def main():
25
26 # The first step in every application using PDFNet is to initialize the
27 # library and set the path to common PDF resources. The library is usually
28 # initialized only once, but calling Initialize() multiple times is also fine.
29 PDFNet.Initialize(LicenseKey)
30
31 try:
32 # Open a PDF document to edit
33 doc = PDFDoc(input_path + "find-replace-test.pdf")
34 options = FindReplaceOptions()
35
36 # Set some find/replace options
37 options.SetWholeWords(True)
38 options.SetMatchCase(True)
39 options.SetMatchMode(FindReplaceOptions.e_exact)
40 options.SetReflowMode(FindReplaceOptions.e_para)
41 options.SetAlignment(FindReplaceOptions.e_left)
42
43 # Perform a Find/Replace finding "the" with "THE INCREDIBLE"
44 FindReplace.FindReplaceText(doc, "the", "THE INCREDIBLE", options)
45
46 # Save the edited PDF
47 doc.Save(output_path + "find-replace-test-replaced.pdf", SDFDoc.e_linearized)
48
49 except Exception as e:
50 print("Unable to perform Find and Replace, error: " + str(e))
51
52 PDFNet.Terminate()
53 print("Done.")
54
55
56if __name__ == '__main__':
57 main()
58
59
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales