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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14var inputPath = "../../TestFiles/"
15var outputPath = "../../TestFiles/Output/"
16
17//-----------------------------------------------------------------------------------------
18// The sample code illustrates how to use the ContentReplacer class to make using
19// 'template' pdf documents easier.
20//-----------------------------------------------------------------------------------------
21
22func main(){
23 PDFNetInitialize(PDFTronLicense.Key)
24
25 // Example 1) Update a business card template with personalized info
26
27 doc := NewPDFDoc(inputPath + "BusinessCardTemplate.pdf")
28 doc.InitSecurityHandler()
29
30 // first, replace the image on the first page
31 replacer := NewContentReplacer()
32 page := doc.GetPage(1)
33 img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
34 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
35 // next, replace the text place holders on the second page
36 replacer.AddString("NAME", "John Smith")
37 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
38 replacer.AddString("JOB_TITLE", "Software Developer")
39 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
40 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
41 replacer.AddString("PHONE_OFFICE", "604-730-8989")
42 replacer.AddString("PHONE_MOBILE", "604-765-4321")
43 replacer.AddString("EMAIL", "info@pdftron.com")
44 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
45 // finally, apply
46 replacer.Process(page)
47
48 doc.Save(outputPath + "BusinessCard.pdf", uint(SDFDocE_linearized))
49 doc.Close()
50
51 fmt.Println("Done. Result saved in BusinessCard.pdf")
52
53 // Example 2) Replace text in a region with new text
54
55 doc = NewPDFDoc(inputPath + "newsletter.pdf")
56 doc.InitSecurityHandler()
57
58 replacer = NewContentReplacer()
59 page = doc.GetPage(1)
60 replacer.AddText(page.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
61 replacer.Process(page)
62
63 doc.Save(outputPath + "ContentReplaced.pdf", uint(SDFDocE_linearized))
64 doc.Close()
65
66 fmt.Println("Done. Result saved in ContentReplaced.pdf")
67
68 PDFNetTerminate()
69 fmt.Println("Done.")
70}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <iostream>
7#include <PDF/PDFNet.h>
8#include <PDF/PDFDoc.h>
9#include <PDF/Image.h>
10#include <PDF/ContentReplacer.h>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13using namespace std;
14using namespace pdftron;
15using namespace Common;
16using namespace SDF;
17using namespace PDF;
18
19//-----------------------------------------------------------------------------------------
20// The sample code illustrates how to use the ContentReplacer class to make using
21// 'template' pdf documents easier.
22//-----------------------------------------------------------------------------------------
23int main(int argc, char * argv[])
24{
25 int ret = 0;
26
27 string input_path = "../../TestFiles/";
28 string output_path = input_path + "Output/";
29
30 // The first step in every application using PDFNet is to initialize the
31 // library and set the path to common PDF resources. The library is usually
32 // initialized only once, but calling Initialize() multiple times is also fine.
33 PDFNet::Initialize(LicenseKey);
34
35 //--------------------------------------------------------------------------------
36 // Example 1) Update a business card template with personalized info
37 try
38 {
39 PDFDoc doc(input_path + "BusinessCardTemplate.pdf");
40 doc.InitSecurityHandler();
41
42 // first, replace the image on the first page
43 ContentReplacer replacer;
44 Page page = doc.GetPage(1);
45 Image img = Image::Create(doc, input_path + "peppers.jpg");
46 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj());
47 // next, replace the text place holders on the second page
48 replacer.AddString("NAME", "John Smith");
49 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor");
50 replacer.AddString("JOB_TITLE", "Software Developer");
51 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd");
52 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC");
53 replacer.AddString("PHONE_OFFICE", "604-730-8989");
54 replacer.AddString("PHONE_MOBILE", "604-765-4321");
55 replacer.AddString("EMAIL", "info@pdftron.com");
56 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com");
57 // finally, apply
58 replacer.Process(page);
59
60 doc.Save(output_path + "BusinessCard.pdf", SDFDoc::e_remove_unused, 0);
61 cout << "Done. Result saved in BusinessCard.pdf" << endl;
62 }
63 catch (Common::Exception& e)
64 {
65 cout << e << endl;
66 ret = 1;
67 }
68 catch (...)
69 {
70 cout << "Unknown Exception" << endl;
71 ret = 1;
72 }
73
74 //--------------------------------------------------------------------------------
75 // Example 2) Replace text in a region with new text
76 try
77 {
78 PDFDoc doc(input_path + "newsletter.pdf");
79 doc.InitSecurityHandler();
80
81 ContentReplacer replacer;
82 Page page = doc.GetPage(1);
83 Rect target_region = page.GetMediaBox();
84 UString replacement_text("hello hello hello hello hello hello hello hello hello hello");
85 replacer.AddText(target_region, replacement_text);
86 replacer.Process(page);
87
88 doc.Save(output_path + "ContentReplaced.pdf", SDFDoc::e_remove_unused, 0);
89 cout << "Done. Result saved in ContentReplaced.pdf" << endl;
90 }
91 catch (Common::Exception& e)
92 {
93 cout << e << endl;
94 ret = 1;
95 }
96 catch (...)
97 {
98 cout << "Unknown Exception" << endl;
99 ret = 1;
100 }
101
102 cout << "Done." << endl;
103
104 PDFNet::Terminate();
105 return ret;
106}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.*;
8
9public class ContentReplacerTest {
10
11 public static void main(String[] args) {
12 String input_path = "../../TestFiles/";
13 String output_path = input_path + "Output/";
14
15 // The first step in every application using PDFNet is to initialize the
16 // library and set the path to common PDF resources. The library is usually
17 // initialized only once, but calling Initialize() multiple times is also fine.
18 PDFNet.initialize(PDFTronLicense.Key());
19
20 //--------------------------------------------------------------------------------
21 // Example 1) Update a business card template with personalized info
22
23 try (PDFDoc doc = new PDFDoc(input_path + "BusinessCardTemplate.pdf")) {
24 doc.initSecurityHandler();
25
26 ContentReplacer replacer = new ContentReplacer();
27 Page page = doc.getPage(1);
28 // first, replace the image on the first page
29 Image img = Image.create(doc, input_path + "peppers.jpg");
30 replacer.addImage(page.getMediaBox(), img.getSDFObj());
31 // next, replace the text place holders on the second page
32 replacer.addString("NAME", "John Smith");
33 replacer.addString("QUALIFICATIONS", "Philosophy Doctor");
34 replacer.addString("JOB_TITLE", "Software Developer");
35 replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd");
36 replacer.addString("ADDRESS_LINE2", "Vancouver, BC");
37 replacer.addString("PHONE_OFFICE", "604-730-8989");
38 replacer.addString("PHONE_MOBILE", "604-765-4321");
39 replacer.addString("EMAIL", "info@pdftron.com");
40 replacer.addString("WEBSITE_URL", "http://www.pdftron.com");
41 // finally, apply
42 replacer.process(page);
43
44 doc.save(output_path + "BusinessCard.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
45 System.out.println("Done. Result saved in BusinessCard.pdf");
46 } catch (Exception e) {
47 e.printStackTrace();
48 return;
49 }
50
51 //--------------------------------------------------------------------------------
52 // Example 2) Replace text in a region with new text
53
54 try (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) {
55 doc.initSecurityHandler();
56
57 ContentReplacer replacer = new ContentReplacer();
58 Page page = doc.getPage(1);
59 Rect target_region = page.getMediaBox();
60 String replacement_text = "hello hello hello hello hello hello hello hello hello hello";
61 replacer.addText(target_region, replacement_text);
62 replacer.process(page);
63
64 doc.save(output_path + "ContentReplaced.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
65 System.out.println("Done. Result saved in ContentReplaced.pdf");
66 } catch (Exception e) {
67 e.printStackTrace();
68 return;
69 }
70
71 System.out.println("Done.");
72
73 PDFNet.terminate();
74 }
75}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6
7const { PDFNet } = require('@pdftron/pdfnet-node');
8const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
10((exports) => {
11
12 exports.runContentReplacer = () => {
13
14 const main = async() => {
15 const inputPath = '../TestFiles/';
16 const outputPath = inputPath + 'Output/';
17
18 try {
19 const inputFilename = 'BusinessCardTemplate.pdf';
20 const outputFilename = 'BusinessCard.pdf';
21
22 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + inputFilename);
23 doc.initSecurityHandler();
24
25 const replacer = await PDFNet.ContentReplacer.create();
26 const page = await doc.getPage(1);
27 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
28
29 const region = await page.getMediaBox();
30 const replace = await img.getSDFObj();
31 await replacer.addImage(region, replace);
32 await replacer.addString('NAME', 'John Smith');
33 await replacer.addString('QUALIFICATIONS', 'Philosophy Doctor');
34 await replacer.addString('JOB_TITLE', 'Software Developer');
35 await replacer.addString('ADDRESS_LINE1', '#100 123 Software Rd');
36 await replacer.addString('ADDRESS_LINE2', 'Vancouver, BC');
37 await replacer.addString('PHONE_OFFICE', '604-730-8989');
38 await replacer.addString('PHONE_MOBILE', '604-765-4321');
39 await replacer.addString('EMAIL', 'info@pdftron.com');
40 await replacer.addString('WEBSITE_URL', 'http://www.pdftron.com');
41 await replacer.process(page);
42
43 await doc.save(outputPath + outputFilename, PDFNet.SDFDoc.SaveOptions.e_remove_unused);
44
45 console.log('Done. Result saved in ' + outputFilename);
46 } catch (err) {
47 console.log(err);
48 }
49 try {
50 const inputFilename = 'newsletter.pdf';
51 const outputFilename = 'ContentReplaced.pdf';
52
53 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + inputFilename);
54 doc.initSecurityHandler();
55
56 const replacer = await PDFNet.ContentReplacer.create();
57 const page = await doc.getPage(1);
58 const region = await page.getMediaBox();
59 await replacer.addText(region, 'hello hello hello hello hello hello hello hello hello hello');
60 await replacer.process(page);
61
62 await doc.save(outputPath + outputFilename, PDFNet.SDFDoc.SaveOptions.e_remove_unused);
63
64 console.log('Done. Result saved in ' + outputFilename);
65 } catch (err) {
66 console.log(err);
67 }
68 console.log('Done.');
69 };
70 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
71 };
72 exports.runContentReplacer();
73})(exports);
74// eslint-disable-next-line spaced-comment
75//# sourceURL=ContentReplacerTest.js
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# Relattive path to the folder containing the test files.
15input_path = "../../TestFiles/"
16output_path = "../../TestFiles/Output/"
17
18#-----------------------------------------------------------------------------------------
19# The sample code illustrates how to use the ContentReplacer class to make using
20# 'template' pdf documents easier.
21#-----------------------------------------------------------------------------------------
22def main():
23 PDFNet.Initialize(LicenseKey)
24
25 # Example 1) Update a business card template with personalized info
26
27 doc = PDFDoc(input_path + "BusinessCardTemplate.pdf")
28 doc.InitSecurityHandler()
29
30 # first, replace the image on the first page
31 replacer = ContentReplacer()
32 page = doc.GetPage(1)
33 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
34 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
35 # next, replace the text place holders on the second page
36 replacer.AddString("NAME", "John Smith")
37 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
38 replacer.AddString("JOB_TITLE", "Software Developer")
39 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
40 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
41 replacer.AddString("PHONE_OFFICE", "604-730-8989")
42 replacer.AddString("PHONE_MOBILE", "604-765-4321")
43 replacer.AddString("EMAIL", "info@pdftron.com")
44 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
45 # finally, apply
46 replacer.Process(page)
47
48 doc.Save(output_path + "BusinessCard.pdf", SDFDoc.e_linearized)
49 doc.Close()
50
51 print("Done. Result saved in BusinessCard.pdf")
52
53 # Example 2) Replace text in a region with new text
54
55 doc = PDFDoc(input_path + "newsletter.pdf")
56 doc.InitSecurityHandler()
57
58 replacer = ContentReplacer()
59 page = doc.GetPage(1)
60 replacer.AddText(page.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
61 replacer.Process(page)
62
63 doc.Save(output_path + "ContentReplaced.pdf", SDFDoc.e_linearized)
64 doc.Close()
65
66 print("Done. Result saved in ContentReplaced.pdf")
67 PDFNet.Terminate()
68 print("Done.")
69
70if __name__ == '__main__':
71 main()
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?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#-----------------------------------------------------------------------------------------
13# The sample code illustrates how to read and edit existing outline items and create
14# new bookmarks using the high-level API.
15#-----------------------------------------------------------------------------------------
16
17# Relative path to the folder containing the test files.
18input_path = "../../TestFiles/"
19output_path = "../../TestFiles/Output/"
20
21#-----------------------------------------------------------------------------------------
22# The sample code illustrates how to use the ContentReplacer class to make using
23# 'template' pdf documents easier.
24#-----------------------------------------------------------------------------------------
25 PDFNet.Initialize(PDFTronLicense.Key)
26
27 # Example 1) Update a business card template with personalized info
28
29 doc = PDFDoc.new(input_path + "BusinessCardTemplate.pdf")
30 doc.InitSecurityHandler()
31
32 # first, replace the image on the first page
33 replacer = ContentReplacer.new()
34 page = doc.GetPage(1)
35 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
36 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
37 # next, replace the text place holders on the second page
38 replacer.AddString("NAME", "John Smith")
39 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
40 replacer.AddString("JOB_TITLE", "Software Developer")
41 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
42 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
43 replacer.AddString("PHONE_OFFICE", "604-730-8989")
44 replacer.AddString("PHONE_MOBILE", "604-765-4321")
45 replacer.AddString("EMAIL", "info@pdftron.com")
46 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
47 # finally, apply
48 replacer.Process(page)
49
50 doc.Save(output_path + "BusinessCard.pdf", 0)
51 doc.Close()
52 puts "Done. Result saved in BusinessCard.pdf"
53
54 # Example 2) Replace text in a region with new text
55
56 doc = PDFDoc.new(input_path + "newsletter.pdf")
57 doc.InitSecurityHandler()
58
59 replacer = ContentReplacer.new()
60 page = doc.GetPage(1)
61 replacer.AddText(page.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
62 replacer.Process(page)
63
64 doc.Save(output_path + "ContentReplaced.pdf", SDFDoc::E_linearized)
65 doc.Close()
66 puts "Done. Result saved in ContentReplaced.pdf"
67 PDFNet.Terminate
68 puts "Done."
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12
13Module ContentReplacerTestVB
14 Dim pdfNetLoader As PDFNetLoader
15 Sub New()
16 pdfNetLoader = pdftron.PDFNetLoader.Instance()
17 End Sub
18'-----------------------------------------------------------------------------------------
19' The sample code illustrates how to use the ContentReplacer class to make using
20' 'template' pdf documents easier.
21'-----------------------------------------------------------------------------------------
22
23 Sub Main()
24
25
26 PDFNet.Initialize(PDFTronLicense.Key)
27
28 ' Relative path to the folder containing test files.
29 Dim input_path As String = "../../../../TestFiles/"
30 Dim output_path As String = "../../../../TestFiles/Output/"
31
32
33 ' Example 1) Update a business card template with personalized info
34 Try
35 Using doc As PDFDoc = New PDFDoc(input_path + "BusinessCardTemplate.pdf")
36 doc.InitSecurityHandler()
37
38 ' first, replace the image on the first page
39 Using replacer As ContentReplacer = New ContentReplacer()
40 Dim page As Page = doc.GetPage(1)
41 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
42 replacer.AddImage(page.GetMediaBox(), img.GetSDFObj())
43 ' next, replace the text place holders on the second page
44 replacer.AddString("NAME", "John Smith")
45 replacer.AddString("QUALIFICATIONS", "Philosophy Doctor")
46 replacer.AddString("JOB_TITLE", "Software Developer")
47 replacer.AddString("ADDRESS_LINE1", "#100 123 Software Rd")
48 replacer.AddString("ADDRESS_LINE2", "Vancouver, BC")
49 replacer.AddString("PHONE_OFFICE", "604-730-8989")
50 replacer.AddString("PHONE_MOBILE", "604-765-4321")
51 replacer.AddString("EMAIL", "info@pdftron.com")
52 replacer.AddString("WEBSITE_URL", "http://www.pdftron.com")
53 ' finally, apply
54 replacer.Process(page)
55 End Using
56
57 doc.Save(output_path + "BusinessCard.pdf", 0)
58 End Using
59 Console.WriteLine("Done. Result saved in BusinessCard.pdf")
60 Catch e As PDFNetException
61 Console.WriteLine(e.Message)
62 End Try
63
64 ' Example 2) Replace text in a region with new text
65 Try
66 Using doc1 As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
67 doc1.InitSecurityHandler()
68
69 Dim replacer1 As ContentReplacer = New ContentReplacer()
70 Dim page1 As Page = doc1.GetPage(1)
71 replacer1.AddText(page1.GetMediaBox(), "hello hello hello hello hello hello hello hello hello hello")
72 replacer1.Process(page1)
73
74 doc1.Save(output_path + "ContentReplaced.pdf", SDFDoc.SaveOptions.e_linearized)
75 End Using
76 Console.WriteLine("Done. Result saved in ContentReplaced.pdf")
77 Catch e As PDFNetException
78 Console.WriteLine(e.Message)
79 End Try
80 PDFNet.Terminate()
81 Console.WriteLine("Done.")
82 End Sub
83
84End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales