Search & Replace PDF Text and Images - PHP 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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7
8include("../../../PDFNetC/Lib/PDFNetPHP.php");
9include("../../LicenseKey/PHP/LicenseKey.php");
10
11//-----------------------------------------------------------------------------------------
12// The sample code illustrates how to use the ContentReplacer class to make using
13// 'template' pdf documents easier.
14//-----------------------------------------------------------------------------------------
15 PDFNet::Initialize($LicenseKey);
16 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
17
18 // Relative path to the folder containing the test files.
19 $input_path = getcwd()."/../../TestFiles/";
20 $output_path = $input_path."Output/";
21
22 //--------------------------------------------------------------------------------
23 // Example 1) Update a business card template with personalized info
24 $doc = new PDFDoc($input_path."BusinessCardTemplate.pdf");
25 $doc->InitSecurityHandler();
26
27 // first, replace the image on the first page
28 $replacer = new ContentReplacer();
29 $page = $doc->GetPage(1);
30 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
31 $replacer->AddImage($page->GetMediaBox(), $img->GetSDFObj());
32 // next, replace the text place holders on the second page
33 $replacer->AddString("NAME", "John Smith");
34 $replacer->AddString("QUALIFICATIONS", "Philosophy Doctor");
35 $replacer->AddString("JOB_TITLE", "Software Developer");
36 $replacer->AddString("ADDRESS_LINE1", "#100 123 Software Rd");
37 $replacer->AddString("ADDRESS_LINE2", "Vancouver, BC");
38 $replacer->AddString("PHONE_OFFICE", "604-730-8989");
39 $replacer->AddString("PHONE_MOBILE", "604-765-4321");
40 $replacer->AddString("EMAIL", "info@pdftron.com");
41 $replacer->AddString("WEBSITE_URL", "http://www.pdftron.com");
42 // finally, apply
43 $replacer->Process($page);
44
45 $doc->Save($output_path."BusinessCard.pdf", 0);
46 echo nl2br("Done. Result saved in BusinessCard.pdf\n");
47
48 //--------------------------------------------------------------------------------
49 // Example 2) Replace text in a region with new text
50 $doc = new PDFDoc($input_path."newsletter.pdf");
51 $doc->InitSecurityHandler();
52
53 $replacer = new ContentReplacer();
54 $page = $doc->GetPage(1);
55 $target_region = $page->GetMediaBox();
56 $replacer->AddText($target_region, "hello hello hello hello hello hello hello hello hello hello");
57 $replacer->Process($page);
58
59 $doc->Save($output_path."ContentReplaced.pdf", 0);
60 echo nl2br("Done. Result saved in ContentReplaced.pdf\n");
61 PDFNet::Terminate();
62 echo nl2br("Done.\n");
63?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales