Stamper

Sample C# 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 UWP SDK.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14
15namespace PDFNetSamples
16{
17 public sealed class StamperTest : Sample
18 {
19 public StamperTest() :
20 base("Stamper", "The sample shows how to use 'pdftron.PDF.Stamper' utility class to stamp PDF pages with text, images, or with other PDF pages. ElementBuilder and ElementWriter should be used for more complex PDF stamping operations.")
21 {
22 }
23
24 public override IAsyncAction RunAsync()
25 {
26 return Task.Run(new System.Action(async () => {
27 WriteLine("--------------------------------");
28 WriteLine("Starting Stamper Test...");
29 WriteLine("--------------------------------\n");
30
31 string input_filename = Path.Combine(InputPath, "newsletter.pdf");
32 //--------------------------------------------------------------------------------
33 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
34 try
35 {
36 WriteLine("Opening input file " + input_filename);
37
38 using (PDFDoc doc = new PDFDoc(input_filename))
39 {
40 doc.InitSecurityHandler();
41
42 Stamper s = new Stamper(StamperSizeType.e_relative_scale, 0.5, 0.5);
43 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);
44 s.SetFontColor(new ColorPt(1, 0, 0)); // set text color to red
45 s.StampText(doc, "If you are reading this\nthis is an even page", new PageSet(1, doc.GetPageCount()));
46 //delete all text stamps in odd pages
47 Stamper.DeleteStamps(doc, new PageSet(1, doc.GetPageCount(), PageSetFilter.e_odd));
48
49 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex1.pdf");
50 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
51 WriteLine("Done. Results saved in " + output_file_path);
52 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
53 }
54 }
55 catch (Exception e)
56 {
57 WriteLine(GetExceptionMessage(e));
58 }
59
60 //--------------------------------------------------------------------------------
61 // Example 2) Add Image stamp to first 2 pages.
62 try
63 {
64 using (PDFDoc doc = new PDFDoc(input_filename))
65 {
66 doc.InitSecurityHandler();
67
68 Stamper s = new Stamper(StamperSizeType.e_relative_scale, .05, .05);
69 pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "peppers.jpg"));
70 s.SetSize(StamperSizeType.e_relative_scale, 0.5, 0.5);
71 //set position of the image to the center, left of PDF pages
72 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_left, StamperVerticalAlignment.e_vertical_center);
73 s.SetFontColor(new ColorPt(0, 0, 0, 0));
74 s.SetRotation(180);
75 s.SetAsBackground(false);
76 //only stamp first 2 pages
77 s.StampImage(doc, img, new PageSet(1, 2));
78
79 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex2.pdf");
80 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
81 WriteLine("Done. Results saved in " + output_file_path);
82 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
83 }
84 }
85 catch (Exception e)
86 {
87 WriteLine(GetExceptionMessage(e));
88 }
89
90 //--------------------------------------------------------------------------------
91 // Example 3) Add Page stamp to all pages.
92 try
93 {
94 using (PDFDoc doc = new PDFDoc(input_filename))
95 {
96 doc.InitSecurityHandler();
97
98 String fishInputPath = Path.Combine(InputPath, "fish.pdf");
99 WriteLine("\nOpening input file " + fishInputPath);
100 PDFDoc fish_doc = new PDFDoc(fishInputPath);
101 fish_doc.InitSecurityHandler();
102
103 Stamper s = new Stamper(StamperSizeType.e_relative_scale, .5, .5);
104 pdftron.PDF.Page src_page = fish_doc.GetPage(1);
105 pdftron.PDF.Rect page_one_crop = src_page.GetCropBox();
106 // set size of the image to 10% of the original while keep the old aspect ratio
107 s.SetSize(StamperSizeType.e_absolute_size, page_one_crop.Width() * 0.1, -1);
108 s.SetOpacity(0.4);
109 s.SetRotation(-67);
110 //put the image at the bottom right hand corner
111 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_right, StamperVerticalAlignment.e_vertical_bottom);
112 s.StampPage(doc, src_page, new PageSet(1, doc.GetPageCount()));
113
114 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex3.pdf");
115 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
116 WriteLine("Done. Results saved in " + output_file_path);
117 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
118 }
119 }
120 catch (Exception e)
121 {
122 WriteLine(GetExceptionMessage(e));
123 }
124
125 //--------------------------------------------------------------------------------
126 // Example 4) Add Image stamp to first 20 odd pages.
127 try
128 {
129 using (PDFDoc doc = new PDFDoc(input_filename))
130 {
131 doc.InitSecurityHandler();
132
133 Stamper s = new Stamper(StamperSizeType.e_absolute_size, 20, 20);
134 s.SetOpacity(1);
135 s.SetRotation(45);
136 s.SetAsBackground(true);
137 s.SetPosition(30, 40);
138 pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "peppers.jpg"));
139 s.StampImage(doc, img, new PageSet(1, 20, PageSetFilter.e_odd));
140
141 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex4.pdf");
142 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
143 WriteLine("Done. Results saved in " + output_file_path);
144 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
145 }
146 }
147 catch (Exception e)
148 {
149 WriteLine(GetExceptionMessage(e));
150 }
151
152 //--------------------------------------------------------------------------------
153 // Example 5) Add text stamp to first 20 even pages
154 try
155 {
156 using (PDFDoc doc = new PDFDoc(input_filename))
157 {
158 doc.InitSecurityHandler();
159
160 Stamper s = new Stamper(StamperSizeType.e_relative_scale, .05, .05);
161 s.SetPosition(0, 0);
162 s.SetOpacity(0.7);
163 s.SetRotation(90);
164 s.SetSize(StamperSizeType.e_font_size, 80, -1);
165 s.SetTextAlignment(StamperTextAlignment.e_align_center);
166 s.StampText(doc, "Goodbye\nMoon", new PageSet(1, 20, PageSetFilter.e_even));
167
168 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex5.pdf");
169 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
170 WriteLine("Done. Results saved in " + output_file_path);
171 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
172 }
173 }
174 catch (Exception e)
175 {
176 WriteLine(GetExceptionMessage(e));
177 }
178
179 //--------------------------------------------------------------------------------
180 // Example 6) Add first page as stamp to all even pages
181 try
182 {
183 using (PDFDoc doc = new PDFDoc(input_filename))
184 {
185 doc.InitSecurityHandler();
186
187 PDFDoc fish_doc = new PDFDoc(Path.Combine(InputPath, "fish.pdf"));
188 fish_doc.InitSecurityHandler();
189
190 Stamper s = new Stamper(StamperSizeType.e_relative_scale, .3, .3);
191 s.SetOpacity(1);
192 s.SetRotation(270);
193 s.SetAsBackground(true);
194 s.SetPosition(0.5, 0.5, true);
195 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_left, StamperVerticalAlignment.e_vertical_bottom);
196 pdftron.PDF.Page page_one = fish_doc.GetPage(1);
197 s.StampPage(doc, page_one, new PageSet(1, doc.GetPageCount(), PageSetFilter.e_even));
198
199 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex6.pdf");
200 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
201 WriteLine("Done. Results saved in " + output_file_path);
202 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
203 }
204 }
205 catch (Exception e)
206 {
207 WriteLine(GetExceptionMessage(e));
208 }
209
210 //--------------------------------------------------------------------------------
211 // Example 7) Add image stamp at top right corner in every pages
212 try
213 {
214 using (PDFDoc doc = new PDFDoc(input_filename))
215 {
216 doc.InitSecurityHandler();
217
218 Stamper s = new Stamper(StamperSizeType.e_relative_scale, .1, .1);
219 s.SetOpacity(0.8);
220 s.SetRotation(135);
221 s.SetAsBackground(false);
222 s.ShowsOnPrint(false);
223 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_left, StamperVerticalAlignment.e_vertical_top);
224 s.SetPosition(10, 10);
225
226 pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "peppers.jpg"));
227 s.StampImage(doc, img, new PageSet(1, doc.GetPageCount(), PageSetFilter.e_all));
228
229 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex7.pdf");
230 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
231 WriteLine("Done. Results saved in " + output_file_path);
232 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
233 }
234 }
235 catch (Exception e)
236 {
237 WriteLine(GetExceptionMessage(e));
238 }
239
240 //--------------------------------------------------------------------------------
241 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
242 // Because text stamp is set as background, the image is top of the text
243 // stamp. Text stamp on the first page is not visible.
244 try
245 {
246 using (PDFDoc doc = new PDFDoc(input_filename))
247 {
248 doc.InitSecurityHandler();
249
250 Stamper s = new Stamper(StamperSizeType.e_relative_scale, 0.07, -0.1);
251 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_right, StamperVerticalAlignment.e_vertical_bottom);
252 s.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_top);
253 s.SetFont(Font.Create(doc.GetSDFDoc(), FontStandardType1Font.e_courier, true));
254 s.SetFontColor(new ColorPt(1, 0, 0, 0)); //set color to red
255 s.SetTextAlignment(StamperTextAlignment.e_align_right);
256 s.SetAsBackground(true); //set text stamp as background
257 s.StampText(doc, "This is a title!", new PageSet(1, 2));
258
259 pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc.GetSDFDoc(), Path.Combine(InputPath, "peppers.jpg"));
260 s.SetAsBackground(false); // set image stamp as foreground
261 s.StampImage(doc, img, new PageSet(1));
262
263 String output_file_path = Path.Combine(OutputPath, "newsletter.pdf.ex8.pdf");
264 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
265 WriteLine("Done. Results saved in " + output_file_path);
266 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
267 }
268 }
269 catch (Exception e)
270 {
271 WriteLine(GetExceptionMessage(e));
272 }
273
274 WriteLine("\n--------------------------------");
275 WriteLine("Done Stamper Test.");
276 WriteLine("--------------------------------\n");
277 })).AsAsyncAction();
278 }
279 }
280}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales