ContentReplacer

Sample Java 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 Android SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.ContentReplacer;
13import com.pdftron.pdf.Image;
14import com.pdftron.pdf.PDFDoc;
15import com.pdftron.pdf.Page;
16import com.pdftron.pdf.Rect;
17import com.pdftron.sdf.SDFDoc;
18
19import java.util.ArrayList;
20
21public class ContentReplacerTest extends PDFNetSample {
22
23 private static OutputListener mOutputListener;
24
25 private static ArrayList<String> mFileList = new ArrayList<>();
26
27 public ContentReplacerTest() {
28 setTitle(R.string.sample_contentreplacer_title);
29 setDescription(R.string.sample_contentreplacer_description);
30 }
31
32 @Override
33 public void run(OutputListener outputListener) {
34 super.run(outputListener);
35 mOutputListener = outputListener;
36 mFileList.clear();
37 printHeader(outputListener);
38
39 // The first step in every application using PDFNet is to initialize the
40 // library and set the path to common PDF resources. The library is usually
41 // initialized only once, but calling Initialize() multiple times is also fine.
42
43 //--------------------------------------------------------------------------------
44 // Example 1) Update a business card template with personalized info
45
46 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "BusinessCardTemplate.pdf").getAbsolutePath())) {
47 doc.initSecurityHandler();
48
49 ContentReplacer replacer = new ContentReplacer();
50 Page page = doc.getPage(1);
51 // first, replace the image on the first page
52 Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
53 replacer.addImage(page.getMediaBox(), img.getSDFObj());
54 // next, replace the text place holders on the second page
55 replacer.addString("NAME", "John Smith");
56 replacer.addString("QUALIFICATIONS", "Philosophy Doctor");
57 replacer.addString("JOB_TITLE", "Software Developer");
58 replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd");
59 replacer.addString("ADDRESS_LINE2", "Vancouver, BC");
60 replacer.addString("PHONE_OFFICE", "604-730-8989");
61 replacer.addString("PHONE_MOBILE", "604-765-4321");
62 replacer.addString("EMAIL", "info@pdftron.com");
63 replacer.addString("WEBSITE_URL", "http://www.pdftron.com");
64 // finally, apply
65 replacer.process(page);
66
67 doc.save(Utils.createExternalFile("BusinessCard.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
68 mOutputListener.println("Done. Result saved in BusinessCard.pdf");
69 } catch (Exception e) {
70 mOutputListener.printError(e.getStackTrace());
71 return;
72 }
73
74 //--------------------------------------------------------------------------------
75 // Example 2) Replace text in a region with new text
76
77 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
78 doc.initSecurityHandler();
79
80 ContentReplacer replacer = new ContentReplacer();
81 Page page = doc.getPage(1);
82 Rect target_region = page.getMediaBox();
83 String replacement_text = "hello hello hello hello hello hello hello hello hello hello";
84 replacer.addText(target_region, replacement_text);
85 replacer.process(page);
86
87 doc.save(Utils.createExternalFile("ContentReplaced.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
88 mOutputListener.println("Done. Result saved in ContentReplaced.pdf");
89 } catch (Exception e) {
90 mOutputListener.printError(e.getStackTrace());
91 return;
92 }
93
94 mOutputListener.println("Done.");
95
96 for (String file : mFileList) {
97 addToFileList(file);
98 }
99 printFooter(outputListener);
100 }
101
102}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales