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}
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.sdf.SDFDoc
16import java.util.*
17
18class ContentReplacerTest : PDFNetSample() {
19 init {
20 setTitle(R.string.sample_contentreplacer_title)
21 setDescription(R.string.sample_contentreplacer_description)
22 }
23
24 override fun run(outputListener: OutputListener?) {
25 super.run(outputListener)
26 mOutputListener = outputListener
27 mFileList.clear()
28 printHeader(outputListener!!)
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
34 //--------------------------------------------------------------------------------
35 // Example 1) Update a business card template with personalized info
36
37 try {
38 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "BusinessCardTemplate.pdf")!!.absolutePath).use {doc ->
39 doc.initSecurityHandler()
40
41 val replacer = ContentReplacer()
42 val page = doc.getPage(1)
43 // first, replace the image on the first page
44 val img = Image.create(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "peppers.jpg")!!.absolutePath)
45 replacer.addImage(page.mediaBox, img.sdfObj)
46 // next, replace the text place holders on the second page
47 replacer.addString("NAME", "John Smith")
48 replacer.addString("QUALIFICATIONS", "Philosophy Doctor")
49 replacer.addString("JOB_TITLE", "Software Developer")
50 replacer.addString("ADDRESS_LINE1", "#100 123 Software Rd")
51 replacer.addString("ADDRESS_LINE2", "Vancouver, BC")
52 replacer.addString("PHONE_OFFICE", "604-730-8989")
53 replacer.addString("PHONE_MOBILE", "604-765-4321")
54 replacer.addString("EMAIL", "info@pdftron.com")
55 replacer.addString("WEBSITE_URL", "http://www.pdftron.com")
56 // finally, apply
57 replacer.process(page)
58
59 doc.save(Utils.createExternalFile("BusinessCard.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
60 mOutputListener!!.println("Done. Result saved in BusinessCard.pdf")
61 }
62 } catch (e: Exception) {
63 mOutputListener!!.printError(e.stackTrace)
64 return
65 }
66
67 //--------------------------------------------------------------------------------
68 // Example 2) Replace text in a region with new text
69
70 try {
71 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { doc ->
72 doc.initSecurityHandler()
73
74 val replacer = ContentReplacer()
75 val page = doc.getPage(1)
76 val target_region = page.mediaBox
77 val 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 doc.save(Utils.createExternalFile("ContentReplaced.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
82 mOutputListener!!.println("Done. Result saved in ContentReplaced.pdf")
83 }
84 } catch (e: Exception) {
85 mOutputListener!!.printError(e.stackTrace)
86 return
87 }
88
89 mOutputListener!!.println("Done.")
90
91 for (file in mFileList) {
92 addToFileList(file)
93 }
94 printFooter(outputListener)
95 }
96
97 companion object {
98
99 private var mOutputListener: OutputListener? = null
100
101 private val mFileList = ArrayList<String>()
102 }
103
104}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales