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

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems 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
15using NUnit.Framework;
16
17namespace MiscellaneousSamples
18{
19 class StamperSample
20 {
21 static StamperSample() {}
22
23 /// <summary>
24 // The following sample shows how to add new content (or watermark) PDF pages
25 // using 'pdftron.PDF.Stamper' utility class.
26 //
27 // Stamper can be used to PDF pages with text, images, or with other PDF content
28 // in only a few lines of code. Although Stamper is very simple to use compared
29 // to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
30 // need full control over PDF creation use ElementBuilder/ElementWriter to add
31 // new content to existing PDF pages as shown in the ElementBuilder sample project.
32 /// </summary>
33 [Test]
34 public static void Sample()
35 {
36
37 const string input_path = "TestFiles/";
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(Utils.GetAssetTempFile(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(Utils.CreateExternalFile(input_filename + ".ex1.pdf"), SDFDoc.SaveOptions.e_linearized);
56 }
57 }
58 catch (PDFNetException e)
59 {
60 Console.WriteLine(e.Message);
61 Assert.True(false);
62 }
63
64 //--------------------------------------------------------------------------------
65 // Example 2) Add Image stamp to first 2 pages.
66 try
67 {
68 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
69 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
70 {
71 doc.InitSecurityHandler();
72
73 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
74 s.SetSize(Stamper.SizeType.e_relative_scale, 0.5, 0.5);
75 //set position of the image to the center, left of PDF pages
76 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_center);
77 s.SetFontColor(new ColorPt(0, 0, 0, 0));
78 s.SetRotation(180);
79 s.SetAsBackground(false);
80 //only stamp first 2 pages
81 s.StampImage(doc, img, new PageSet(1, 2));
82
83 doc.Save(Utils.CreateExternalFile(input_filename + ".ex2.pdf"), SDFDoc.SaveOptions.e_linearized);
84 }
85 }
86 catch (PDFNetException e)
87 {
88 Console.WriteLine(e.Message);
89 Assert.True(false);
90 }
91
92 //--------------------------------------------------------------------------------
93 // Example 3) Add Page stamp to all pages.
94 try
95 {
96 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
97 using (PDFDoc fish_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "fish.pdf")))
98 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .5, .5))
99 {
100 doc.InitSecurityHandler();
101
102 fish_doc.InitSecurityHandler();
103
104 Page src_page = fish_doc.GetPage(1);
105 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(Stamper.SizeType.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(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
112 s.StampPage(doc, src_page, new PageSet(1, doc.GetPageCount()));
113
114 doc.Save(Utils.CreateExternalFile(input_filename + ".ex3.pdf"), SDFDoc.SaveOptions.e_linearized);
115 }
116 }
117 catch (PDFNetException e)
118 {
119 Console.WriteLine(e.Message);
120 Assert.True(false);
121 }
122
123 //--------------------------------------------------------------------------------
124 // Example 4) Add Image stamp to first 20 odd pages.
125 try
126 {
127 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
128 using (Stamper s = new Stamper(Stamper.SizeType.e_absolute_size, 20, 20))
129 {
130 doc.InitSecurityHandler();
131
132 s.SetOpacity(1);
133 s.SetRotation(45);
134 s.SetAsBackground(true);
135 s.SetPosition(30, 40);
136 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
137 s.StampImage(doc, img, new PageSet(1, 20, PageSet.Filter.e_odd));
138
139 doc.Save(Utils.CreateExternalFile(input_filename + ".ex4.pdf"), SDFDoc.SaveOptions.e_linearized);
140 }
141 }
142 catch (PDFNetException e)
143 {
144 Console.WriteLine(e.Message);
145 Assert.True(false);
146 }
147
148 //--------------------------------------------------------------------------------
149 // Example 5) Add text stamp to first 20 even pages
150 try
151 {
152 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
153 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .05, .05))
154 {
155 doc.InitSecurityHandler();
156
157 s.SetPosition(0, 0);
158 s.SetOpacity(0.7);
159 s.SetRotation(90);
160 s.SetSize(Stamper.SizeType.e_font_size, 80, -1);
161 s.SetTextAlignment(Stamper.TextAlignment.e_align_center);
162 s.StampText(doc, "Goodbye\nMoon", new PageSet(1, 20, PageSet.Filter.e_even));
163
164 doc.Save(Utils.CreateExternalFile(input_filename + ".ex5.pdf"), SDFDoc.SaveOptions.e_linearized);
165 }
166 }
167 catch (PDFNetException e)
168 {
169 Console.WriteLine(e.Message);
170 Assert.True(false);
171 }
172
173 //--------------------------------------------------------------------------------
174 // Example 6) Add first page as stamp to all even pages
175 try
176 {
177 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
178 using (PDFDoc fish_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "fish.pdf")))
179 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .3, .3))
180 {
181 doc.InitSecurityHandler();
182
183 fish_doc.InitSecurityHandler();
184
185 s.SetOpacity(1);
186 s.SetRotation(270);
187 s.SetAsBackground(true);
188 s.SetPosition(0.5, 0.5, true);
189 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom);
190 Page page_one = fish_doc.GetPage(1);
191 s.StampPage(doc, page_one, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_even));
192
193 doc.Save(Utils.CreateExternalFile(input_filename + ".ex6.pdf"), SDFDoc.SaveOptions.e_linearized);
194 }
195 }
196 catch (PDFNetException e)
197 {
198 Console.WriteLine(e.Message);
199 Assert.True(false);
200 }
201
202 //--------------------------------------------------------------------------------
203 // Example 7) Add image stamp at top right corner in every pages
204 try
205 {
206 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
207 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, .1, .1))
208 {
209 doc.InitSecurityHandler();
210
211 s.SetOpacity(0.8);
212 s.SetRotation(135);
213 s.SetAsBackground(false);
214 s.ShowsOnPrint(false);
215 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_top);
216 s.SetPosition(10, 10);
217
218 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
219 s.StampImage(doc, img, new PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all));
220
221 doc.Save(Utils.CreateExternalFile(input_filename + ".ex7.pdf"), SDFDoc.SaveOptions.e_linearized);
222 }
223 }
224 catch (PDFNetException e)
225 {
226 Console.WriteLine(e.Message);
227 Assert.True(false);
228 }
229
230 //--------------------------------------------------------------------------------
231 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
232 // Because text stamp is set as background, the image is top of the text
233 // stamp. Text stamp on the first page is not visible.
234 try
235 {
236 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + input_filename + ".pdf")))
237 using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.07, -0.1))
238 {
239 doc.InitSecurityHandler();
240
241 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
242 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_top);
243 s.SetFont(Font.Create(doc, Font.StandardType1Font.e_courier, true));
244 s.SetFontColor(new ColorPt(1, 0, 0, 0)); //set color to red
245 s.SetTextAlignment(Stamper.TextAlignment.e_align_right);
246 s.SetAsBackground(true); //set text stamp as background
247 s.StampText(doc, "This is a title!", new PageSet(1, 2));
248
249 Image img = Image.Create(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"));
250 s.SetAsBackground(false); // set image stamp as foreground
251 s.StampImage(doc, img, new PageSet(1));
252
253 doc.Save(Utils.CreateExternalFile(input_filename + ".ex8.pdf"), SDFDoc.SaveOptions.e_linearized);
254 }
255 }
256 catch (PDFNetException e)
257 {
258 Console.WriteLine(e.Message);
259 Assert.True(false);
260 }
261 }
262 }
263}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales