Stamper

Sample JavaScript 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 Web SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5(exports => {
6
7
8
9
10
11
12
13 exports.runStamperTest = () => {
14 const PDFNet = exports.Core.PDFNet;
15
16 const main = async () => {
17 let ret = 0;
18 // Relative path to the folder containing test files.
19 const inputURL = '../TestFiles/';
20 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
21 try {
22 // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
23 // all objects in memory that were initialized since the most recent startDeallocateStack call will be
24 // cleaned up. Doing this makes sure that memory growth does not get too high.
25 await PDFNet.startDeallocateStack();
26
27 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
28 doc.initSecurityHandler();
29 doc.lock();
30
31 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5); // Stamp size is relative to the size of the crop box of the destination page
32 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_center);
33
34 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
35 stamper.setFontColor(redColorPt);
36 const pgSet = await PDFNet.PageSet.createRange(1, await doc.getPageCount());
37 stamper.stampText(doc, 'If you are reading this\nthis is an even page', pgSet);
38 const oddPgSet = await PDFNet.PageSet.createFilteredRange(1, await doc.getPageCount(), PDFNet.PageSet.Filter.e_odd);
39 // delete all text stamps in odd pages
40 PDFNet.Stamper.deleteStamps(doc, oddPgSet);
41
42 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
43 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex1.pdf');
44
45 await PDFNet.endDeallocateStack();
46 } catch (err) {
47 console.log(err.stack);
48 ret = 1;
49 }
50
51 // Example 2) Add Image stamp to first two pages
52 try {
53 await PDFNet.startDeallocateStack();
54 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
55 doc.initSecurityHandler();
56 doc.lock();
57
58 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
59 const img = await PDFNet.Image.createFromURL(doc, inputURL + 'peppers.jpg');
60 stamper.setSize(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
61 // set position of the image to the center, left of PDF pages
62 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_center);
63
64 const blackColorPt = await PDFNet.ColorPt.init(0, 0, 0, 0);
65 stamper.setFontColor(blackColorPt);
66 stamper.setRotation(180);
67 stamper.setAsBackground(false);
68 // only stamp first 2 pages
69 const pgSet = await PDFNet.PageSet.createRange(1, 2);
70 stamper.stampImage(doc, img, pgSet);
71
72 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
73 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex2.pdf');
74 await PDFNet.endDeallocateStack();
75 } catch (err) {
76 console.log(err.stack);
77 ret = 1;
78 }
79
80 // Example 3) Add Page stamp to all pages
81 try {
82 await PDFNet.startDeallocateStack();
83 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
84 doc.initSecurityHandler();
85 doc.lock();
86 const fishDoc = await PDFNet.PDFDoc.createFromURL(inputURL + 'fish.pdf');
87 fishDoc.initSecurityHandler();
88 fishDoc.lock();
89
90 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
91 const srcPage = await fishDoc.getPage(1);
92 const pageOneCrop = await srcPage.getCropBox();
93 // set size of the image to 10% of the original while keep the old aspect ratio
94 stamper.setSize(PDFNet.Stamper.SizeType.e_absolute_size, (await pageOneCrop.width()) * 0.1, -1);
95 stamper.setOpacity(0.4);
96 stamper.setRotation(-67);
97
98 // put the image at the bottom right hand corner
99 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
100 const pgSet = await PDFNet.PageSet.createRange(1, await doc.getPageCount());
101 stamper.stampPage(doc, srcPage, pgSet);
102
103 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
104 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex3.pdf');
105 await PDFNet.endDeallocateStack();
106 } catch (err) {
107 console.log(err.stack);
108 ret = 1;
109 }
110
111 // Example 4) Add Image stamp to first 20 odd pages
112 try {
113 await PDFNet.startDeallocateStack();
114 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
115 doc.initSecurityHandler();
116 doc.lock();
117
118 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_absolute_size, 20, 20);
119 stamper.setOpacity(1);
120 stamper.setRotation(45);
121 stamper.setAsBackground(true);
122 stamper.setPosition(30, 40);
123 const img = await PDFNet.Image.createFromURL(doc, inputURL + 'peppers.jpg');
124
125 const pgSet = await PDFNet.PageSet.createFilteredRange(1, 20, PDFNet.PageSet.Filter.e_odd);
126 stamper.stampImage(doc, img, pgSet);
127
128 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
129 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex4.pdf');
130 await PDFNet.endDeallocateStack();
131 } catch (err) {
132 console.log(err.stack);
133 ret = 1;
134 }
135
136 // Example 5) Add Text stamp to first 20 even pages
137 try {
138 await PDFNet.startDeallocateStack();
139
140 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
141 doc.initSecurityHandler();
142 doc.lock();
143
144 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.05, 0.05);
145 stamper.setOpacity(0.7);
146 stamper.setRotation(90);
147 stamper.setPosition(0, 0);
148 stamper.setSize(PDFNet.Stamper.SizeType.e_font_size, 80, -1);
149 stamper.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_center);
150 const pgSet = await PDFNet.PageSet.createFilteredRange(1, 20, PDFNet.PageSet.Filter.e_even);
151 stamper.stampText(doc, 'Goodbye\nMoon', pgSet);
152
153 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
154 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex5.pdf');
155
156 await PDFNet.endDeallocateStack();
157 } catch (err) {
158 console.log(err.stack);
159 ret = 1;
160 }
161 // Example 6) Add first page as stamp to all even pages
162 try {
163 await PDFNet.startDeallocateStack();
164
165 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
166 doc.initSecurityHandler();
167 doc.lock();
168
169 const fishDoc = await PDFNet.PDFDoc.createFromURL(inputURL + 'fish.pdf');
170 fishDoc.initSecurityHandler();
171 fishDoc.lock();
172
173 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.3, 0.3);
174 stamper.setOpacity(1);
175 stamper.setRotation(270);
176 stamper.setAsBackground(true);
177 stamper.setPosition(0.5, 0.5, true);
178 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
179 const pgOne = await fishDoc.getPage(1);
180 const pgSet = await PDFNet.PageSet.createFilteredRange(1, await doc.getPageCount(), PDFNet.PageSet.Filter.e_even);
181 stamper.stampPage(doc, pgOne, pgSet);
182
183 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
184 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex6.pdf');
185
186 await PDFNet.endDeallocateStack();
187 } catch (err) {
188 console.log(err.stack);
189 ret = 1;
190 }
191
192 // Example 7) Add image stamp at top right corner in every page
193 try {
194 await PDFNet.startDeallocateStack();
195
196 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
197 doc.initSecurityHandler();
198 doc.lock();
199
200 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.1, 0.1);
201 stamper.setOpacity(0.8);
202 stamper.setRotation(135);
203 stamper.setAsBackground(false);
204 stamper.showsOnPrint(false);
205 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
206 stamper.setPosition(10, 10);
207
208 const img = await PDFNet.Image.createFromURL(doc, inputURL + 'peppers.jpg');
209 const pgSet = await PDFNet.PageSet.createFilteredRange(1, await doc.getPageCount(), PDFNet.PageSet.Filter.e_all);
210 stamper.stampImage(doc, img, pgSet);
211
212 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
213 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex7.pdf');
214
215 await PDFNet.endDeallocateStack();
216 } catch (err) {
217 console.log(err.stack);
218 ret = 1;
219 }
220
221 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
222 // Because text stamp is set as background, the image is on top of the text stamp
223 // Text stamp on the first page is not visible.
224 try {
225 await PDFNet.startDeallocateStack();
226
227 const doc = await PDFNet.PDFDoc.createFromURL(inputURL + 'newsletter.pdf');
228 doc.initSecurityHandler();
229 doc.lock();
230
231 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.07, -0.1);
232 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
233 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
234 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_courier);
235 stamper.setFont(font);
236 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
237 stamper.setFontColor(redColorPt);
238 stamper.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_right);
239 stamper.setAsBackground(true);
240
241 const pgSet = await PDFNet.PageSet.createRange(1, 2);
242 stamper.stampText(doc, 'This is a title!', pgSet);
243
244 const img = await PDFNet.Image.createFromURL(doc, inputURL + 'peppers.jpg');
245 stamper.setAsBackground(false);
246
247 const pgSetImage = await PDFNet.PageSet.createRange(1, 1);
248 stamper.stampImage(doc, img, pgSetImage);
249
250 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
251 saveBufferAsPDFDoc(docBuffer, 'newsletter.ex8.pdf');
252
253 await PDFNet.endDeallocateStack();
254 } catch (err) {
255 console.log(err.stack);
256 ret = 1;
257 }
258 return ret;
259 };
260 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
261 PDFNet.runWithCleanup(main);
262 };
263})(window);
264// eslint-disable-next-line spaced-comment
265//# sourceURL=StamperTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales