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 UWP SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11
12using PDFNetUniversalSamples.ViewModels;
13
14namespace PDFNetSamples
15{
16 public sealed class ContentReplacerTest : Sample
17 {
18 public ContentReplacerTest() :
19 base("ContentReplacer", "This sample 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).")
20 {
21 }
22
23 public override IAsyncAction RunAsync()
24 {
25 return Task.Run(new System.Action(async () => {
26 WriteLine("--------------------------------");
27 WriteLine("Starting ContentReplacer Test...");
28 WriteLine("--------------------------------\n");
29 // The following example illustrates how to replace an image in a certain region,
30 // and how to change template text.
31 try
32 {
33 String input_file_path = Path.Combine(InputPath, "BusinessCardTemplate.pdf");
34 WriteLine("Opening input file " + input_file_path );
35 PDFDoc doc = new PDFDoc(input_file_path);
36 doc.InitSecurityHandler();
37
38 // first, replace the image on the first page
39 ContentReplacer replacer = new ContentReplacer();
40 pdftron.PDF.Page page = doc.GetPage(1);
41 //pdftron.SDF.SDFDoc sdoc = new pdftron.SDF.SDFDoc(doc);
42 pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "peppers.jpg"));
43 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
44 // next, replace the text place holders on the second page
45 replacer.AddString("NAME", "John Smith");
46 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor");
47 replacer.AddString("JOB_TITLE", "Software Developer");
48 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd");
49 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC");
50 replacer.AddString("PHONE_OFFICE", "604-730-8989");
51 replacer.AddString("PHONE_MOBILE", "604-765-4321");
52 replacer.AddString("EMAIL", "info@pdftron.com");
53 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com");
54 // finally, apply
55 replacer.Process(page);
56
57 String output_file_path = Path.Combine(OutputPath, "BusinessCard.pdf");
58 await doc.SaveAsync(output_file_path, 0);
59 doc.Destroy();
60 WriteLine("Done. Result saved in " + output_file_path);
61 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
62 }
63 catch (Exception e)
64 {
65 WriteLine(GetExceptionMessage(e));
66 }
67
68 // The following example illustrates how to replace text in a given region
69 try {
70 String input_file_path = Path.Combine(InputPath, "newsletter.pdf");
71 PDFDoc doc = new PDFDoc(input_file_path);
72 doc.InitSecurityHandler();
73
74 ContentReplacer replacer = new ContentReplacer();
75 pdftron.PDF.Page page = doc.GetPage(1);
76 pdftron.PDF.Rect target_region = page.GetMediaBox();
77 string replacement_text = "hello hello hello hello hello hello hello hello hello hello";
78 replacer.AddText(target_region, replacement_text);
79 replacer.Process(page);
80
81 String output_file_path = Path.Combine(OutputPath, "ContentReplaced.pdf");
82 await doc.SaveAsync(output_file_path, 0);
83 doc.Destroy();
84 WriteLine("Done. Result saved in " + output_file_path);
85 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
86 }
87 catch (Exception e)
88 {
89 WriteLine(GetExceptionMessage(e));
90 }
91 WriteLine("\n--------------------------------");
92 WriteLine("Done ContentReplacer Test.");
93 WriteLine("--------------------------------\n");
94 })).AsAsyncAction();
95 }
96 }
97}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales