ContentReplacer

Sample C# 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. Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6using System;
7using pdftron;
8using pdftron.Common;
9using pdftron.Filters;
10using pdftron.SDF;
11using pdftron.PDF;
12
13namespace ContentReplacerTestCS
14{
15 /// <summary>
16 //-----------------------------------------------------------------------------------------
17 // The sample code illustrates how to use the ContentReplacer class to make using
18 // 'template' pdf documents easier.
19 //-----------------------------------------------------------------------------------------
20 /// </summary>
21 class Class1
22 {
23 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
24 static Class1() {}
25
26 /// <summary>
27 /// The main entry point for the application.
28 /// </summary>
29 [STAThread]
30 static void Main(string[] args)
31 {
32 PDFNet.Initialize(PDFTronLicense.Key);
33
34 // Relative path to the folder containing test files.
35 string input_path = "../../../../TestFiles/";
36 string output_path = "../../../../TestFiles/Output/";
37
38
39 // The following example illustrates how to replace an image in a certain region,
40 // and how to change template text.
41 try
42 {
43 using (PDFDoc doc = new PDFDoc(input_path + "BusinessCardTemplate.pdf"))
44 using (ContentReplacer replacer = new ContentReplacer())
45 {
46 doc.InitSecurityHandler();
47
48 // first, replace the image on the first page
49 Page page = doc.GetPage(1);
50 Image img = Image.Create(doc, input_path + "peppers.jpg");
51 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
52 // next, replace the text place holders on the second page
53 replacer.AddString("NAME", "John Smith");
54 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor");
55 replacer.AddString("JOB_TITLE", "Software Developer");
56 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd");
57 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC");
58 replacer.AddString("PHONE_OFFICE", "604-730-8989");
59 replacer.AddString("PHONE_MOBILE", "604-765-4321");
60 replacer.AddString("EMAIL", "info@pdftron.com");
61 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com");
62 // finally, apply
63 replacer.Process(page);
64
65 doc.Save(output_path + "BusinessCard.pdf", 0);
66 Console.WriteLine("Done. Result saved in BusinessCard.pdf");
67 }
68 }
69 catch (PDFNetException e)
70 {
71 Console.WriteLine(e.Message);
72 }
73
74
75 // The following example illustrates how to replace text in a given region
76 try
77 {
78 using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
79 using (ContentReplacer replacer = new ContentReplacer())
80 {
81 doc.InitSecurityHandler();
82
83 Page page = doc.GetPage(1);
84 Rect target_region = page.GetMediaBox();
85 string replacement_text = "hello hello hello hello hello hello hello hello hello hello";
86 replacer.AddText(target_region, replacement_text);
87 replacer.Process(page);
88
89 doc.Save(output_path + "ContentReplaced.pdf", 0);
90 Console.WriteLine("Done. Result saved in ContentReplaced.pdf");
91 }
92 }
93 catch (PDFNetException e)
94 {
95 Console.WriteLine(e.Message);
96 }
97 PDFNet.Terminate();
98 Console.WriteLine("Done.");
99 }
100 }
101}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales