Search & Replace PDF Text and Images - Java 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
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.*;
8
9public class ContentReplacerTest {
10
11 public static void main(String[] args) {
12 String input_path = "../../TestFiles/";
13 String output_path = input_path + "Output/";
14
15 // The first step in every application using PDFNet is to initialize the
16 // library and set the path to common PDF resources. The library is usually
17 // initialized only once, but calling Initialize() multiple times is also fine.
18 PDFNet.initialize(PDFTronLicense.Key());
19
20 //--------------------------------------------------------------------------------
21 // Example 1) Update a business card template with personalized info
22
23 try (PDFDoc doc = new PDFDoc(input_path + "BusinessCardTemplate.pdf")) {
24 doc.initSecurityHandler();
25
26 ContentReplacer replacer = new ContentReplacer();
27 Page page = doc.getPage(1);
28 // first, replace the image on the first page
29 Image img = Image.create(doc, input_path + "peppers.jpg");
30 replacer.addImage(page.getMediaBox(), img.getSDFObj());
31 // next, replace the text place holders on the second page
32 replacer.addString("NAME", "John Smith");
33 replacer.addString("QUALIFICATIONS", "Philosophy Doctor");
34 replacer.addString("JOB_TITLE", "Software Developer");
35 replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd");
36 replacer.addString("ADDRESS_LINE2", "Vancouver, BC");
37 replacer.addString("PHONE_OFFICE", "604-730-8989");
38 replacer.addString("PHONE_MOBILE", "604-765-4321");
39 replacer.addString("EMAIL", "info@pdftron.com");
40 replacer.addString("WEBSITE_URL", "http://www.pdftron.com");
41 // finally, apply
42 replacer.process(page);
43
44 doc.save(output_path + "BusinessCard.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
45 System.out.println("Done. Result saved in BusinessCard.pdf");
46 } catch (Exception e) {
47 e.printStackTrace();
48 return;
49 }
50
51 //--------------------------------------------------------------------------------
52 // Example 2) Replace text in a region with new text
53
54 try (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) {
55 doc.initSecurityHandler();
56
57 ContentReplacer replacer = new ContentReplacer();
58 Page page = doc.getPage(1);
59 Rect target_region = page.getMediaBox();
60 String replacement_text = "hello hello hello hello hello hello hello hello hello hello";
61 replacer.addText(target_region, replacement_text);
62 replacer.process(page);
63
64 doc.save(output_path + "ContentReplaced.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
65 System.out.println("Done. Result saved in ContentReplaced.pdf");
66 } catch (Exception e) {
67 e.printStackTrace();
68 return;
69 }
70
71 System.out.println("Done.");
72
73 PDFNet.terminate();
74 }
75}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales