Search & Replace PDF Text and Images - C++ Sample Code

Sample 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. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. Learn more about our Server SDK and PDF Editing & Manipulation Library.

It's mandatory to use square brackets for target strings in the original PDF doc when using ContentReplacer methods like AddString(). Otherwise, the content replacer won't recognize it as a template to replace.

For example, in the PDF document, you add a template for recognition: [NAME]. In the code, you tie the tag specified within the square brackets to what you want it to be replaced with: replacer.AddString("NAME", "John Smith") . After processing, both the square brackets and the tag will be replaced with "John Smith".

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <iostream>
7#include <PDF/PDFNet.h>
8#include <PDF/PDFDoc.h>
9#include <PDF/Image.h>
10#include <PDF/ContentReplacer.h>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13using namespace std;
14using namespace pdftron;
15using namespace Common;
16using namespace SDF;
17using namespace PDF;
18
19//-----------------------------------------------------------------------------------------
20// The sample code illustrates how to use the ContentReplacer class to make using
21// 'template' pdf documents easier.
22//-----------------------------------------------------------------------------------------
23int main(int argc, char * argv[])
24{
25 int ret = 0;
26
27 string input_path = "../../TestFiles/";
28 string output_path = input_path + "Output/";
29
30 // The first step in every application using PDFNet is to initialize the
31 // library and set the path to common PDF resources. The library is usually
32 // initialized only once, but calling Initialize() multiple times is also fine.
33 PDFNet::Initialize(LicenseKey);
34
35 //--------------------------------------------------------------------------------
36 // Example 1) Update a business card template with personalized info
37 try
38 {
39 PDFDoc doc(input_path + "BusinessCardTemplate.pdf");
40 doc.InitSecurityHandler();
41
42 // first, replace the image on the first page
43 ContentReplacer replacer;
44 Page page = doc.GetPage(1);
45 Image img = Image::Create(doc, input_path + "peppers.jpg");
46 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
47 // next, replace the text place holders on the second page
48 replacer.AddString("NAME", "John Smith");
49 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor");
50 replacer.AddString("JOB_TITLE", "Software Developer");
51 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd");
52 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC");
53 replacer.AddString("PHONE_OFFICE", "604-730-8989");
54 replacer.AddString("PHONE_MOBILE", "604-765-4321");
55 replacer.AddString("EMAIL", "info@pdftron.com");
56 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com");
57 // finally, apply
58 replacer.Process(page);
59
60 doc.Save(output_path + "BusinessCard.pdf", SDFDoc::e_remove_unused, 0);
61 cout << "Done. Result saved in BusinessCard.pdf" << endl;
62 }
63 catch (Common::Exception& e)
64 {
65 cout << e << endl;
66 ret = 1;
67 }
68 catch (...)
69 {
70 cout << "Unknown Exception" << endl;
71 ret = 1;
72 }
73
74 //--------------------------------------------------------------------------------
75 // Example 2) Replace text in a region with new text
76 try
77 {
78 PDFDoc doc(input_path + "newsletter.pdf");
79 doc.InitSecurityHandler();
80
81 ContentReplacer replacer;
82 Page page = doc.GetPage(1);
83 Rect target_region = page.GetMediaBox();
84 UString replacement_text("hello hello hello hello hello hello hello hello hello hello");
85 replacer.AddText(target_region, replacement_text);
86 replacer.Process(page);
87
88 doc.Save(output_path + "ContentReplaced.pdf", SDFDoc::e_remove_unused, 0);
89 cout << "Done. Result saved in ContentReplaced.pdf" << endl;
90 }
91 catch (Common::Exception& e)
92 {
93 cout << e << endl;
94 ret = 1;
95 }
96 catch (...)
97 {
98 cout << "Unknown Exception" << endl;
99 ret = 1;
100 }
101
102 cout << "Done." << endl;
103
104 PDFNet::Terminate();
105 return ret;
106}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales