Replacing text & images in PDFs on Server/Desktop

To find text or images and replace it in a PDF.

1doc = PDFDoc.new(filename)
2replacer = ContentReplacer.new()
3page = doc.GetPage(1)
4
5# replace an image on the page
6target_region = page.GetMediaBox()
7img = Image.Create(doc.GetSDFDoc(), imagename)
8replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
9
10# replace a text placeholder
11replacer.AddString("NAME", "John Smith")
12
13# replace text in a given region
14text = "hello world"
15replacer.AddText(target_region, "hello world")
16replacer.Process(page)

Replace PDF text or images
Full code sample which shows how to use pdftron.PDF.ContentReplacer to search and replace text strings and images in existing PDF (e.g. business cards and other PDF templates).

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales