Stamper

Sample Java code for using Apryse SDK to programmatically stamp PDF pages with text, images, or with other PDF pages. ElementBuilder and ElementWriter should be used for more complex PDF stamping operations. Learn more about our Android SDK.

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.ColorPt;
13import com.pdftron.pdf.Font;
14import com.pdftron.pdf.Image;
15import com.pdftron.pdf.PDFDoc;
16import com.pdftron.pdf.Page;
17import com.pdftron.pdf.PageSet;
18import com.pdftron.pdf.Rect;
19import com.pdftron.pdf.Stamper;
20import com.pdftron.sdf.SDFDoc;
21
22import java.util.ArrayList;
23
24/**
25 * The following sample shows how to add new content (or watermark) PDF pages
26 * using 'com.pdftron.pdf.Stamper' utility class.
27 * <p>
28 * Stamper can be used to PDF pages with text, images, or with other PDF content
29 * in only a few lines of code. Although Stamper is very simple to use compared
30 * to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
31 * need full control over PDF creation use ElementBuilder/ElementWriter to add
32 * new content to existing PDF pages as shown in the ElementBuilder sample project.
33 */
34public class StamperTest extends PDFNetSample {
35
36 private static OutputListener mOutputListener;
37
38 private static ArrayList<String> mFileList = new ArrayList<>();
39
40 public StamperTest() {
41 setTitle(R.string.sample_stamper_title);
42 setDescription(R.string.sample_stamper_description);
43 }
44
45 @Override
46 public void run(OutputListener outputListener) {
47 super.run(outputListener);
48 mOutputListener = outputListener;
49 mFileList.clear();
50 printHeader(outputListener);
51 String input_filename = "newsletter";
52
53 //--------------------------------------------------------------------------------
54 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
55 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
56 doc.initSecurityHandler();
57 Stamper s = new Stamper(Stamper.e_relative_scale, 0.5, 0.5);
58 s.setAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_center);
59 ColorPt red = new ColorPt(1, 0, 0); // set text color to red
60 s.setFontColor(red);
61 String msg = "If you are reading this\nthis is an even page";
62 PageSet ps = new PageSet(1, doc.getPageCount());
63 s.stampText(doc, msg, ps);
64 //delete all text stamps in even pages
65 PageSet ps1 = new PageSet(1, doc.getPageCount(), PageSet.e_odd);
66 Stamper.deleteStamps(doc, ps1);
67
68 doc.save(Utils.createExternalFile(input_filename + ".ex1.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
69 } catch (Exception e) {
70 mOutputListener.printError(e.getStackTrace());
71 return;
72 }
73
74 //--------------------------------------------------------------------------------
75 // Example 2) Add Image stamp to first 2 pages.
76 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
77 doc.initSecurityHandler();
78
79 Stamper s = new Stamper(Stamper.e_relative_scale, .05, .05);
80 Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
81 s.setSize(Stamper.e_relative_scale, 0.5, 0.5);
82 //set position of the image to the center, left of PDF pages
83 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_center);
84 ColorPt pt = new ColorPt(0, 0, 0, 0);
85 s.setFontColor(pt);
86 s.setRotation(180);
87 s.setAsBackground(false);
88 //only stamp first 2 pages
89 PageSet ps = new PageSet(1, 2);
90 s.stampImage(doc, img, ps);
91
92 doc.save(Utils.createExternalFile(input_filename + ".ex2.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
93 } catch (Exception e) {
94 mOutputListener.printError(e.getStackTrace());
95 return;
96 }
97
98 //--------------------------------------------------------------------------------
99 // Example 3) Add Page stamp to all pages.
100 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
101 doc.initSecurityHandler();
102
103 try (PDFDoc fish_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath())) {
104 fish_doc.initSecurityHandler();
105
106 Stamper s = new Stamper(Stamper.e_relative_scale, 0.5, 0.5);
107 Page src_page = fish_doc.getPage(1);
108 Rect page_one_crop = src_page.getCropBox();
109 // set size of the image to 10% of the original while keep the old aspect ratio
110 s.setSize(Stamper.e_absolute_size, page_one_crop.getWidth() * 0.1, -1);
111 s.setOpacity(0.4);
112 s.setRotation(-67);
113 //put the image at the bottom right hand corner
114 s.setAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom);
115 PageSet ps = new PageSet(1, doc.getPageCount());
116 s.stampPage(doc, src_page, ps);
117
118 doc.save(Utils.createExternalFile(input_filename + ".ex3.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
119 }
120 } catch (Exception e) {
121 mOutputListener.printError(e.getStackTrace());
122 return;
123 }
124
125 //--------------------------------------------------------------------------------
126 // Example 4) Add Image stamp to first 20 odd pages.
127 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
128 doc.initSecurityHandler();
129
130 Stamper s = new Stamper(Stamper.e_absolute_size, 20, 20);
131 s.setOpacity(1);
132 s.setRotation(45);
133 s.setAsBackground(true);
134 s.setPosition(30, 40);
135 Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
136 PageSet ps = new PageSet(1, 20, PageSet.e_odd);
137 s.stampImage(doc, img, ps);
138
139 doc.save(Utils.createExternalFile(input_filename + ".ex4.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
140 } catch (Exception e) {
141 mOutputListener.printError(e.getStackTrace());
142 return;
143 }
144
145 //--------------------------------------------------------------------------------
146 // Example 5) Add text stamp to first 20 even pages
147 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
148 doc.initSecurityHandler();
149
150 Stamper s = new Stamper(Stamper.e_relative_scale, .05, .05);
151 s.setPosition(0, 0);
152 s.setOpacity(0.7);
153 s.setRotation(90);
154 s.setSize(Stamper.e_font_size, 80, -1);
155 s.setTextAlignment(Stamper.e_align_center);
156 PageSet ps = new PageSet(1, 20, PageSet.e_even);
157 s.stampText(doc, "Goodbye\nMoon", ps);
158
159 doc.save(Utils.createExternalFile(input_filename + ".ex5.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
160 } catch (Exception e) {
161 mOutputListener.printError(e.getStackTrace());
162 return;
163 }
164
165 //--------------------------------------------------------------------------------
166 // Example 6) Add first page as stamp to all even pages
167 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
168 doc.initSecurityHandler();
169
170 PDFDoc fish_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath());
171 fish_doc.initSecurityHandler();
172
173 Stamper s = new Stamper(Stamper.e_relative_scale, 0.3, 0.3);
174 s.setOpacity(1);
175 s.setRotation(270);
176 s.setAsBackground(true);
177 s.setPosition(0.5, 0.5, true);
178 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_bottom);
179 Page page_one = fish_doc.getPage(1);
180 PageSet ps = new PageSet(1, doc.getPageCount(), PageSet.e_even);
181 s.stampPage(doc, page_one, ps);
182
183 doc.save(Utils.createExternalFile(input_filename + ".ex6.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
184 } catch (Exception e) {
185 mOutputListener.printError(e.getStackTrace());
186 return;
187 }
188
189 //--------------------------------------------------------------------------------
190 // Example 7) Add image stamp at top right corner in every pages
191 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
192 doc.initSecurityHandler();
193
194 Stamper s = new Stamper(Stamper.e_relative_scale, .1, .1);
195 s.setOpacity(0.8);
196 s.setRotation(135);
197 s.setAsBackground(false);
198 s.showsOnPrint(false);
199 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_top);
200 s.setPosition(10, 10);
201
202 Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
203 PageSet ps = new PageSet(1, doc.getPageCount(), PageSet.e_all);
204 s.stampImage(doc, img, ps);
205
206 doc.save(Utils.createExternalFile(input_filename + ".ex7.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
207 } catch (Exception e) {
208 mOutputListener.printError(e.getStackTrace());
209 return;
210 }
211
212 //--------------------------------------------------------------------------------
213 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
214 // Because text stamp is set as background, the image is top of the text
215 // stamp. Text stamp on the first page is not visible.
216 try (PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + input_filename + ".pdf").getAbsolutePath())) {
217 doc.initSecurityHandler();
218
219 Stamper s = new Stamper(Stamper.e_relative_scale, 0.07, -0.1);
220 s.setAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom);
221 s.setAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_top);
222 s.setFont(Font.create(doc, Font.e_courier, true));
223 ColorPt red = new ColorPt(1, 0, 0, 0);
224 s.setFontColor(red); //set color to red
225 s.setTextAlignment(Stamper.e_align_right);
226 s.setAsBackground(true); //set text stamp as background
227 PageSet ps = new PageSet(1, 2);
228 s.stampText(doc, "This is a title!", ps);
229
230 Image img = Image.create(doc, Utils.getAssetTempFile(INPUT_PATH + "peppers.jpg").getAbsolutePath());
231 s.setAsBackground(false); // set image stamp as foreground
232 PageSet first_page_ps = new PageSet(1);
233 s.stampImage(doc, img, first_page_ps);
234
235 doc.save(Utils.createExternalFile(input_filename + ".ex8.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
236 } catch (Exception e) {
237 mOutputListener.printError(e.getStackTrace());
238 return;
239 }
240
241 for (String file : mFileList) {
242 addToFileList(file);
243 }
244 printFooter(outputListener);
245 }
246 //---------------------------------------------------------------------------------------
247 // The following sample shows how to add new content (or watermark) PDF pages
248 // using 'pdftron.PDF.Stamper' utility class.
249 //
250 // Stamper can be used to PDF pages with text, images, or with other PDF content
251 // in only a few lines of code. Although Stamper is very simple to use compared
252 // to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
253 // need full control over PDF creation use ElementBuilder/ElementWriter to add
254 // new content to existing PDF pages as shown in the ElementBuilder sample project.
255 //---------------------------------------------------------------------------------------
256
257}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales