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 Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6using System;
7using System.IO;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.Filters;
12using pdftron.SDF;
13using pdftron.PDF;
14
15namespace PDFNetSamples
16{
17 class StamperSample
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static StamperSample() {}
21
22 /// <summary>
23 // The following sample shows how to add new content (or watermark) PDF pages
24 // using 'pdftron.PDF.Stamper' utility class.
25 //
26 // Stamper can be used to PDF pages with text, images, or with other PDF content
27 // in only a few lines of code. Although Stamper is very simple to use compared
28 // to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
29 // need full control over PDF creation use ElementBuilder/ElementWriter to add
30 // new content to existing PDF pages as shown in the ElementBuilder sample project.
31 /// </summary>
32 static void Main(string[] args)
33 {
34 PDFNet.Initialize(PDFTronLicense.Key);
35
36 string input_path = "../../../../TestFiles/";
37 string output_path = "../../../../TestFiles/Output/";
38 string input_filename = "newsletter";
39
40 //--------------------------------------------------------------------------------
41 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
42 try
43 {
44 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
45 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5))
46 {
47 doc.InitSecurityHandler();
48
49 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center);
50 s.SetFontColor(new ColorPt(1, 0, 0)); // set text color to red
51 s.StampText(doc, "If you are reading this\nthis is an even page", new PageSet(1, doc.GetPageCount()));
52 //delete all text stamps in odd pages
53 Stamper.DeleteStamps(doc, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_odd));
54
55 doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc.SaveOptions.e_linearized);
56 }
57 }
58 catch (PDFNetException e)
59 {
60 Console.WriteLine(e.Message);
61 }
62
63 //--------------------------------------------------------------------------------
64 // Example 2) Add Image stamp to first 2 pages.
65 try
66 {
67 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
68 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
69 {
70 doc.InitSecurityHandler();
71
72 Image img = Image.Create(doc, input_path + "peppers.jpg");
73 s.SetSize(Stamper.SizeType.e_relative_scale, 0.5, 0.5);
74 //set position of the image to the center, left of PDF pages
75 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_center);
76 s.SetFontColor(new ColorPt(0, 0, 0, 0));
77 s.SetRotation(180);
78 s.SetAsBackground(false);
79 //only stamp first 2 pages
80 s.StampImage(doc, img, new PageSet(1, 2));
81
82 doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc.SaveOptions.e_linearized);
83 }
84 }
85 catch (PDFNetException e)
86 {
87 Console.WriteLine(e.Message);
88 }
89
90 //--------------------------------------------------------------------------------
91 // Example 3) Add Page stamp to all pages.
92 try
93 {
94 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
95 using (PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf"))
96 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .5, .5))
97 {
98 doc.InitSecurityHandler();
99
100 fish_doc.InitSecurityHandler();
101
102 Page src_page = fish_doc.GetPage(1);
103 Rect page_one_crop = src_page.GetCropBox();
104 // set size of the image to 10% of the original while keep the old aspect ratio
105 s.SetSize(Stamper.SizeType.e_absolute_size, page_one_crop.Width() * 0.1, -1);
106 s.SetOpacity(0.4);
107 s.SetRotation(-67);
108 //put the image at the bottom right hand corner
109 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
110 s.StampPage(doc, src_page, new PageSet(1, doc.GetPageCount()));
111
112 doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc.SaveOptions.e_linearized);
113 }
114 }
115 catch (PDFNetException e)
116 {
117 Console.WriteLine(e.Message);
118 }
119
120 //--------------------------------------------------------------------------------
121 // Example 4) Add Image stamp to first 20 odd pages.
122 try
123 {
124 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
125 using (Stamper s = new Stamper(Stamper.SizeType.e_absolute_size, 20, 20))
126 {
127 doc.InitSecurityHandler();
128
129 s.SetOpacity(1);
130 s.SetRotation(45);
131 s.SetAsBackground(true);
132 s.SetPosition(30, 40);
133 Image img = Image.Create(doc, input_path + "peppers.jpg");
134 s.StampImage(doc, img, new PageSet(1, 20, PageSet.Filter.e_odd));
135
136 doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc.SaveOptions.e_linearized);
137 }
138 }
139 catch (PDFNetException e)
140 {
141 Console.WriteLine(e.Message);
142 }
143
144 //--------------------------------------------------------------------------------
145 // Example 5) Add text stamp to first 20 even pages
146 try
147 {
148 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
149 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
150 {
151 doc.InitSecurityHandler();
152
153 s.SetPosition(0, 0);
154 s.SetOpacity(0.7);
155 s.SetRotation(90);
156 s.SetSize(Stamper.SizeType.e_font_size, 80, -1);
157 s.SetTextAlignment(Stamper.TextAlignment.e_align_center);
158 s.StampText(doc, "Goodbye\nMoon", new PageSet(1, 20, PageSet.Filter.e_even));
159
160 doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc.SaveOptions.e_linearized);
161 }
162 }
163 catch (PDFNetException e)
164 {
165 Console.WriteLine(e.Message);
166 }
167
168 //--------------------------------------------------------------------------------
169 // Example 6) Add first page as stamp to all even pages
170 try
171 {
172 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
173 using (PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf"))
174 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .3, .3))
175 {
176 doc.InitSecurityHandler();
177
178 fish_doc.InitSecurityHandler();
179
180 s.SetOpacity(1);
181 s.SetRotation(270);
182 s.SetAsBackground(true);
183 s.SetPosition(0.5, 0.5, true);
184 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom);
185 Page page_one = fish_doc.GetPage(1);
186 s.StampPage(doc, page_one, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_even));
187
188 doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc.SaveOptions.e_linearized);
189 }
190 }
191 catch (PDFNetException e)
192 {
193 Console.WriteLine(e.Message);
194 }
195
196 //--------------------------------------------------------------------------------
197 // Example 7) Add image stamp at top right corner in every pages
198 try
199 {
200 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
201 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .1, .1))
202 {
203 doc.InitSecurityHandler();
204
205 s.SetOpacity(0.8);
206 s.SetRotation(135);
207 s.SetAsBackground(false);
208 s.ShowsOnPrint(false);
209 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_top);
210 s.SetPosition(10, 10);
211
212 Image img = Image.Create(doc, input_path + "peppers.jpg");
213 s.StampImage(doc, img, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all));
214
215 doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc.SaveOptions.e_linearized);
216 }
217 }
218 catch (PDFNetException e)
219 {
220 Console.WriteLine(e.Message);
221 }
222
223 //--------------------------------------------------------------------------------
224 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
225 // Because text stamp is set as background, the image is top of the text
226 // stamp. Text stamp on the first page is not visible.
227 try
228 {
229 using (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf"))
230 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.07, -0.1))
231 {
232 doc.InitSecurityHandler();
233
234 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
235 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_top);
236 s.SetFont(Font.Create(doc, Font.StandardType1Font.e_courier, true));
237 s.SetFontColor(new ColorPt(1, 0, 0, 0)); //set color to red
238 s.SetTextAlignment(Stamper.TextAlignment.e_align_right);
239 s.SetAsBackground(true); //set text stamp as background
240 s.StampText(doc, "This is a title!", new PageSet(1, 2));
241
242 Image img = Image.Create(doc, input_path + "peppers.jpg");
243 s.SetAsBackground(false); // set image stamp as foreground
244 s.StampImage(doc, img, new PageSet(1));
245
246 doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc.SaveOptions.e_linearized);
247 }
248 }
249 catch (PDFNetException e)
250 {
251 Console.WriteLine(e.Message);
252 }
253 PDFNet.Terminate();
254 }
255 }
256}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales