Search & Replace PDF Text and Images - Ruby 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-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#-----------------------------------------------------------------------------------------
13# The sample code illustrates how to read and edit existing outline items and create
14# new bookmarks using the high-level API.
15#-----------------------------------------------------------------------------------------
16
17# Relative path to the folder containing the test files.
18input_path = "../../TestFiles/"
19output_path = "../../TestFiles/Output/"
20
21#-----------------------------------------------------------------------------------------
22# The sample code illustrates how to use the ContentReplacer class to make using
23# 'template' pdf documents easier.
24#-----------------------------------------------------------------------------------------
25 PDFNet.Initialize(PDFTronLicense.Key)
26
27 # Example 1) Update a business card template with personalized info
28
29 doc = PDFDoc.new(input_path + "BusinessCardTemplate.pdf")
30 doc.InitSecurityHandler()
31
32 # first, replace the image on the first page
33 replacer = ContentReplacer.new()
34 page = doc.GetPage(1)
35 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
36 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
37 # next, replace the text place holders on the second page
38 replacer.AddString("NAME", "John Smith")
39 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
40 replacer.AddString("JOB_TITLE", "Software Developer")
41 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
42 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
43 replacer.AddString("PHONE_OFFICE", "604-730-8989")
44 replacer.AddString("PHONE_MOBILE", "604-765-4321")
45 replacer.AddString("EMAIL", "info@pdftron.com")
46 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
47 # finally, apply
48 replacer.Process(page)
49
50 doc.Save(output_path + "BusinessCard.pdf", 0)
51 doc.Close()
52 puts "Done. Result saved in BusinessCard.pdf"
53
54 # Example 2) Replace text in a region with new text
55
56 doc = PDFDoc.new(input_path + "newsletter.pdf")
57 doc.InitSecurityHandler()
58
59 replacer = ContentReplacer.new()
60 page = doc.GetPage(1)
61 replacer.AddText(page.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
62 replacer.Process(page)
63
64 doc.Save(output_path + "ContentReplaced.pdf", SDFDoc::E_linearized)
65 doc.Close()
66 puts "Done. Result saved in ContentReplaced.pdf"
67 PDFNet.Terminate
68 puts "Done."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales