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 Xamarin SDK and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems 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
13using NUnit.Framework;
14
15namespace MiscellaneousSamples
16{
17 /// <summary>
18 //-----------------------------------------------------------------------------------------
19 // The sample code illustrates how to use the ContentReplacer class to make using
20 // 'template' pdf documents easier.
21 //-----------------------------------------------------------------------------------------
22 /// </summary>
23 [TestFixture]
24 public class ContentReplacerTest
25 {
26
27 /// <summary>
28 /// The main entry point for the application.
29 /// </summary>
30 [Test]
31 public static void Sample()
32 {
33
34 // Relative path to the folder containing test files.
35 const string input_path = "TestFiles/";
36
37
38 // The following example illustrates how to replace an image in a certain region,
39 // and how to change template text.
40 try
41 {
42 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "BusinessCardTemplate.pdf")))
43 using (ContentReplacer replacer = new ContentReplacer())
44 {
45 doc.InitSecurityHandler();
46
47 // first, replace the image on the first page
48 Page page = doc.GetPage(1);
49 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
50 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
51 // next, replace the text place holders on the second page
52 replacer.AddString("NAME", "John Smith");
53 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor");
54 replacer.AddString("JOB_TITLE", "Software Developer");
55 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd");
56 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC");
57 replacer.AddString("PHONE_OFFICE", "604-730-8989");
58 replacer.AddString("PHONE_MOBILE", "604-765-4321");
59 replacer.AddString("EMAIL", "info@pdftron.com");
60 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com");
61 // finally, apply
62 replacer.Process(page);
63
64 doc.Save(Utils.CreateExternalFile("BusinessCard.pdf"), 0);
65 Console.WriteLine("Done. Result saved in BusinessCard.pdf");
66 }
67 }
68 catch (PDFNetException e)
69 {
70 Console.WriteLine(e.Message);
71 Assert.True(false);
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(Utils.GetAssetTempFile(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(Utils.CreateExternalFile("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 Assert.True(false);
97 }
98 Console.WriteLine("Done.");
99 }
100 }
101}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales