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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/PageSet.h>
9#include <PDF/Stamper.h>
10#include <PDF/Image.h>
11#include <iostream>
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14using namespace std;
15using namespace pdftron;
16using namespace Common;
17using namespace SDF;
18using namespace PDF;
19
20//---------------------------------------------------------------------------------------
21// The following sample shows how to add new content (or watermark) PDF pages
22// using 'pdftron.PDF.Stamper' utility class.
23//
24// Stamper can be used to PDF pages with text, images, or with other PDF content
25// in only a few lines of code. Although Stamper is very simple to use compared
26// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
27// need full control over PDF creation use ElementBuilder/ElementWriter to add
28// new content to existing PDF pages as shown in the ElementBuilder sample project.
29//---------------------------------------------------------------------------------------
30int main(int argc, char * argv[])
31{
32 int ret = 0;
33 std::string input_path = "../../TestFiles/";
34 std::string output_path = "../../TestFiles/Output/";
35 std::string input_filename = "newsletter";
36
37 PDFNet::Initialize(LicenseKey);
38
39 //--------------------------------------------------------------------------------
40 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
41 try
42 {
43 pdftron::PDF::PDFDoc doc((input_path + input_filename + ".pdf").c_str());
44 doc.InitSecurityHandler();
45 Stamper s(pdftron::PDF::Stamper::e_relative_scale, 0.5, 0.5);
46
47 s.SetAlignment(Stamper::e_horizontal_center, Stamper::e_vertical_center);
48 ColorPt red(1, 0, 0); // set text color to red
49 s.SetFontColor(red);
50 s.StampText(doc, "If you are reading this\nthis is an even page", PageSet(1, doc.GetPageCount()));
51 //delete all text stamps in even pages
52 Stamper::DeleteStamps(doc, PageSet(1, doc.GetPageCount(), PageSet::e_odd));
53
54 doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc::e_linearized, NULL);
55 }
56 catch (Common::Exception& e)
57 {
58 std::cout << e << endl;
59 ret = 1;
60 }
61 catch (...)
62 {
63 cout << "Unknown Exception" << endl;
64 ret = 1;
65 }
66
67 //--------------------------------------------------------------------------------
68 // Example 2) Add Image stamp to first 2 pages.
69 try
70 {
71 PDFDoc doc(input_path + input_filename + ".pdf");
72 doc.InitSecurityHandler();
73
74 Stamper s(Stamper::e_relative_scale, .05, .05);
75 Image img = Image::Create(doc, input_path + "peppers.jpg");
76 s.SetSize(Stamper::e_relative_scale, 0.5, 0.5);
77 //set position of the image to the center, left of PDF pages
78 s.SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_center);
79 ColorPt pt(0, 0, 0, 0);
80 s.SetFontColor(pt);
81 s.SetRotation(180);
82 s.SetAsBackground(false);
83 //only stamp first 2 pages
84 PageSet ps(1, 2);
85 s.StampImage(doc, img, ps);
86
87 doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc::e_linearized, NULL);
88 }
89 catch (Common::Exception& e)
90 {
91 std::cout << e << endl;
92 ret = 1;
93 }
94 catch (...)
95 {
96 cout << "Unknown Exception" << endl;
97 ret = 1;
98 }
99
100 //--------------------------------------------------------------------------------
101 // Example 3) Add Page stamp to all pages.
102 try
103 {
104 PDFDoc doc(input_path + input_filename + ".pdf");
105 doc.InitSecurityHandler();
106
107 PDFDoc fish_doc(input_path + "fish.pdf");
108 fish_doc.InitSecurityHandler();
109
110 Stamper s(Stamper::e_relative_scale, 0.5, 0.5);
111 Page src_page = fish_doc.GetPage(1);
112 Rect page_one_crop = src_page.GetCropBox();
113 // set size of the image to 10% of the original while keep the old aspect ratio
114 s.SetSize(Stamper::e_absolute_size, page_one_crop.Width() * 0.1, -1);
115 s.SetOpacity(0.4);
116 s.SetRotation(-67);
117 //put the image at the bottom right hand corner
118 s.SetAlignment(Stamper::e_horizontal_right, Stamper::e_vertical_bottom);
119 PageSet ps(1, doc.GetPageCount());
120 s.StampPage(doc, src_page, ps);
121
122 doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc::e_linearized, NULL);
123 }
124 catch (Common::Exception& e)
125 {
126 std::cout << e << endl;
127 ret = 1;
128 }
129 catch (...)
130 {
131 cout << "Unknown Exception" << endl;
132 ret = 1;
133 }
134
135 //--------------------------------------------------------------------------------
136 // Example 4) Add Image stamp to first 20 odd pages.
137 try
138 {
139 PDFDoc doc(input_path + input_filename + ".pdf");
140 doc.InitSecurityHandler();
141
142 Stamper s(Stamper::e_absolute_size, 20, 20);
143 s.SetOpacity(1);
144 s.SetRotation(45);
145 s.SetAsBackground(true);
146 s.SetPosition(30, 40);
147 Image img = Image::Create(doc, input_path + "peppers.jpg");
148 PageSet ps(1, 20, PageSet::e_odd);
149 s.StampImage(doc, img, ps);
150
151 doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc::e_linearized, NULL);
152 }
153 catch (Common::Exception& e)
154 {
155 std::cout << e << endl;
156 ret = 1;
157 }
158 catch (...)
159 {
160 cout << "Unknown Exception" << endl;
161 ret = 1;
162 }
163
164 //--------------------------------------------------------------------------------
165 // Example 5) Add text stamp to first 20 even pages
166 try
167 {
168 PDFDoc doc(input_path + input_filename + ".pdf");
169 doc.InitSecurityHandler();
170
171 Stamper s(Stamper::e_relative_scale, .05, .05);
172 s.SetPosition(0, 0);
173 s.SetOpacity(0.7);
174 s.SetRotation(90);
175 s.SetSize(Stamper::e_font_size, 80, -1);
176 s.SetTextAlignment(Stamper::e_align_center);
177 PageSet ps(1, 20, PageSet::e_even);
178 s.StampText(doc, "Goodbye\nMoon", ps);
179
180 doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc::e_linearized, NULL);
181 }
182 catch (Common::Exception& e)
183 {
184 std::cout << e << endl;
185 ret = 1;
186 }
187 catch (...)
188 {
189 cout << "Unknown Exception" << endl;
190 ret = 1;
191 }
192
193 //--------------------------------------------------------------------------------
194 // Example 6) Add first page as stamp to all even pages
195 try
196 {
197 PDFDoc doc(input_path + input_filename + ".pdf");
198 doc.InitSecurityHandler();
199
200 PDFDoc fish_doc(input_path + "fish.pdf");
201 fish_doc.InitSecurityHandler();
202
203 Stamper s(Stamper::e_relative_scale, .3, .3);
204 s.SetOpacity(1);
205 s.SetRotation(270);
206 s.SetAsBackground(true);
207 s.SetPosition(0.5, 0.5, true);
208 s.SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_bottom);
209 Page page_one = fish_doc.GetPage(1);
210 PageSet ps(1, doc.GetPageCount(), PageSet::e_even);
211 s.StampPage(doc, page_one, ps);
212
213 doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc::e_linearized, NULL);
214 }
215 catch (Common::Exception& e)
216 {
217 std::cout << e << endl;
218 ret = 1;
219 }
220 catch (...)
221 {
222 cout << "Unknown Exception" << endl;
223 ret = 1;
224 }
225
226 //--------------------------------------------------------------------------------
227 // Example 7) Add image stamp at top right corner in every pages
228 try
229 {
230 PDFDoc doc(input_path + input_filename + ".pdf");
231 doc.InitSecurityHandler();
232
233 Stamper s(Stamper::e_relative_scale, .1, .1);
234 s.SetOpacity(0.8);
235 s.SetRotation(135);
236 s.SetAsBackground(false);
237 s.ShowsOnPrint(false);
238 s.SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_top);
239 s.SetPosition(10, 10);
240
241 Image img = Image::Create(doc, input_path + "peppers.jpg");
242 PageSet ps(1, doc.GetPageCount(), PageSet::e_all);
243 s.StampImage(doc, img, ps);
244
245 doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc::e_linearized, NULL);
246 }
247 catch (Common::Exception& e)
248 {
249 std::cout << e << endl;
250 ret = 1;
251 }
252 catch (...)
253 {
254 cout << "Unknown Exception" << endl;
255 ret = 1;
256 }
257
258 //--------------------------------------------------------------------------------
259 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
260 // Because text stamp is set as background, the image is top of the text
261 // stamp. Text stamp on the first page is not visible.
262 try
263 {
264 PDFDoc doc(input_path + input_filename + ".pdf");
265 doc.InitSecurityHandler();
266
267 Stamper s(Stamper::e_relative_scale, 0.07, -0.1);
268 s.SetAlignment(Stamper::e_horizontal_right, Stamper::e_vertical_bottom);
269 s.SetAlignment(Stamper::e_horizontal_center, Stamper::e_vertical_top);
270 s.SetFont(Font::Create(doc, Font::e_courier, true));
271 ColorPt red(1, 0, 0, 0);
272 s.SetFontColor(red); //set color to red
273 s.SetTextAlignment(Stamper::e_align_right);
274 s.SetAsBackground(true); //set text stamp as background
275 PageSet ps(1, 2);
276 s.StampText(doc, "This is a title!", ps);
277
278 Image img = Image::Create(doc, input_path + "peppers.jpg");
279 s.SetAsBackground(false); // set image stamp as foreground
280 PageSet first_page_ps(1);
281 s.StampImage(doc, img, first_page_ps);
282
283 doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc::e_linearized, NULL);
284 }
285 catch (Common::Exception& e)
286 {
287 std::cout << e << endl;
288 ret = 1;
289 }
290 catch (...)
291 {
292 cout << "Unknown Exception" << endl;
293 ret = 1;
294 }
295
296 PDFNet::Terminate();
297 return ret;
298}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 . "pdftron"
9)
10
11import "pdftron/Samples/LicenseKey/GO"
12//---------------------------------------------------------------------------------------
13// The following sample shows how to add new content (or watermark) PDF pages
14// using 'pdftron.PDF.Stamper' utility class.
15//
16// Stamper can be used to PDF pages with text, images, or with other PDF content
17// in only a few lines of code. Although Stamper is very simple to use compared
18// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
19// need full control over PDF creation use ElementBuilder/ElementWriter to add
20// new content to existing PDF pages as shown in the ElementBuilder sample project.
21//---------------------------------------------------------------------------------------
22
23// Relative path to the folder containing the test files.
24var inputPath = "../../TestFiles/"
25var outputPath = "../../TestFiles/Output/"
26var inputFilename = "newsletter"
27
28func main(){
29 // Initialize PDFNet
30 PDFNetInitialize(PDFTronLicense.Key)
31
32 //--------------------------------------------------------------------------------
33 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
34 doc := NewPDFDoc(inputPath + inputFilename + ".pdf")
35 doc.InitSecurityHandler()
36 s := NewStamper(StamperE_relative_scale, 0.5, 0.5)
37
38 s.SetAlignment(StamperE_horizontal_center, StamperE_vertical_center)
39 red := NewColorPt(1.0, 0.0, 0.0) // set text color to red
40 s.SetFontColor(red)
41 s.StampText(doc, "If you are reading this\nthis is an even page", NewPageSet(1, doc.GetPageCount()))
42 // delete all text stamps in odd pages
43 StamperDeleteStamps(doc, NewPageSet(1, doc.GetPageCount(), PageSetE_odd))
44
45 doc.Save(outputPath + inputFilename + "E_x1.pdf", uint(SDFDocE_linearized))
46 doc.Close()
47
48 //--------------------------------------------------------------------------------
49 // Example 2) Add Image stamp to first 2 pages.
50
51 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
52 doc.InitSecurityHandler()
53 s = NewStamper(StamperE_relative_scale, 0.05, 0.05)
54 img := ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
55 s.SetSize(StamperE_relative_scale, 0.5, 0.5)
56
57 // set position of the image to the center, left of PDF pages
58 s.SetAlignment(StamperE_horizontal_left, StamperE_vertical_center)
59 pt := NewColorPt(0.0, 0.0, 0.0, 0.0)
60 s.SetFontColor(pt)
61 s.SetRotation(180)
62 s.SetAsBackground(false)
63 // only stamp first 2 pages
64 ps := NewPageSet(1, 2)
65 s.StampImage(doc, img, ps)
66
67 doc.Save(outputPath + inputFilename + "E_x2.pdf", uint(SDFDocE_linearized))
68 doc.Close()
69
70 //--------------------------------------------------------------------------------
71 // Example 3) Add Page stamp to all pages.
72
73 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
74 doc.InitSecurityHandler()
75
76 fishDoc := NewPDFDoc(inputPath + "fish.pdf")
77 fishDoc.InitSecurityHandler()
78 s = NewStamper(StamperE_relative_scale, 0.5, 0.5)
79 srcPage := fishDoc.GetPage(1)
80 pageOneCrop := srcPage.GetCropBox()
81 // set size of the image to 10% of the original while keep the old aspect ratio
82 s.SetSize(StamperE_absolute_size, pageOneCrop.Width() * 0.1, -1)
83 s.SetOpacity(0.4)
84 s.SetRotation(-67)
85 // put the image at the bottom right hand corner
86 s.SetAlignment(StamperE_horizontal_right, StamperE_vertical_bottom)
87 ps = NewPageSet(1, doc.GetPageCount())
88 s.StampPage(doc, srcPage, ps)
89 doc.Save(outputPath + inputFilename + "E_x3.pdf", uint(SDFDocE_linearized))
90 doc.Close()
91
92 //--------------------------------------------------------------------------------
93 // Example 4) Add Image stamp to first 20 odd pages.
94
95 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
96 doc.InitSecurityHandler()
97
98 s = NewStamper(StamperE_absolute_size, 20.0, 20.0)
99 s.SetOpacity(1)
100 s.SetRotation(45)
101 s.SetAsBackground(true)
102 s.SetPosition(30.0, 40.0)
103 img = ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
104 ps = NewPageSet(1, 20, PageSetE_odd)
105 s.StampImage(doc, img, ps)
106
107 doc.Save(outputPath + inputFilename + "E_x4.pdf", uint(SDFDocE_linearized))
108 doc.Close()
109
110 //--------------------------------------------------------------------------------
111 // Example 5) Add text stamp to first 20 even pages
112
113 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
114 doc.InitSecurityHandler()
115 s = NewStamper(StamperE_relative_scale, 0.05, 0.05)
116 s.SetPosition(0.0, 0.0)
117 s.SetOpacity(0.7)
118 s.SetRotation(90)
119 s.SetSize(StamperE_font_size, 80, -1)
120 s.SetTextAlignment(StamperE_align_center)
121 ps = NewPageSet(1, 20, PageSetE_even)
122 s.StampText(doc, "Goodbye\nMoon", ps)
123
124 doc.Save(outputPath + inputFilename + "E_x5.pdf", uint(SDFDocE_linearized))
125 doc.Close()
126
127 //--------------------------------------------------------------------------------
128 // Example 6) Add first page as stamp to all even pages
129
130 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
131 doc.InitSecurityHandler()
132
133 fishDoc = NewPDFDoc(inputPath + "fish.pdf");
134 fishDoc.InitSecurityHandler()
135
136 s = NewStamper(StamperE_relative_scale, 0.3, 0.3)
137 s.SetOpacity(1)
138 s.SetRotation(270)
139 s.SetAsBackground(true)
140 s.SetPosition(0.5, 0.5, true)
141 s.SetAlignment(StamperE_horizontal_left, StamperE_vertical_bottom)
142 pageOne := fishDoc.GetPage(1)
143 ps = NewPageSet(1, doc.GetPageCount(), PageSetE_even)
144 s.StampPage(doc, pageOne, ps)
145
146 doc.Save(outputPath + inputFilename + "E_x6.pdf", uint(SDFDocE_linearized))
147 doc.Close()
148
149 //--------------------------------------------------------------------------------
150 // Example 7) Add image stamp at top left corner in every pages
151
152 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
153 doc.InitSecurityHandler()
154
155 s = NewStamper(StamperE_relative_scale, 0.1, 0.1)
156 s.SetOpacity(0.8)
157 s.SetRotation(135)
158 s.SetAsBackground(false)
159 s.ShowsOnPrint(false)
160 s.SetAlignment(StamperE_horizontal_left, StamperE_vertical_top)
161 s.SetPosition(10.0, 10.0)
162 img = ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
163 ps = NewPageSet(1, doc.GetPageCount(), PageSetE_all)
164 s.StampImage(doc, img, ps)
165 doc.Save(outputPath + inputFilename + "E_x7.pdf", uint(SDFDocE_linearized))
166 doc.Close()
167
168 //--------------------------------------------------------------------------------
169 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
170 // Because text stamp is set as background, the image is top of the text
171 // stamp. Text stamp on the first page is not visible.
172
173 doc = NewPDFDoc(inputPath + inputFilename + ".pdf")
174 doc.InitSecurityHandler()
175
176 s = NewStamper(StamperE_relative_scale, 0.07, -0.1)
177 s.SetAlignment(StamperE_horizontal_right, StamperE_vertical_bottom)
178 s.SetAlignment(StamperE_horizontal_center, StamperE_vertical_top)
179 s.SetFont(FontCreate(doc.GetSDFDoc(), FontE_courier, true))
180 red = NewColorPt(1.0, 0.0, 0.0)
181 s.SetFontColor(red) // set text color to red
182 s.SetTextAlignment(StamperE_align_right)
183 s.SetAsBackground(true) // set text stamp as background
184 ps = NewPageSet(1, 2)
185 s.StampText(doc, "This is a title!", ps)
186
187 img = ImageCreate(doc.GetSDFDoc(), inputPath + "peppers.jpg")
188 s.SetAsBackground(false) // set image stamp as foreground
189 firstPagePS := NewPageSet(1)
190 s.StampImage(doc, img, firstPagePS)
191
192 doc.Save(outputPath + inputFilename + "E_x8.pdf", uint(SDFDocE_linearized))
193 doc.Close()
194 PDFNetTerminate()
195}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8
9public class StamperTest {
10 //---------------------------------------------------------------------------------------
11 // The following sample shows how to add new content (or watermark) PDF pages
12 // using 'pdftron.PDF.Stamper' utility class.
13 //
14 // Stamper can be used to PDF pages with text, images, or with other PDF content
15 // in only a few lines of code. Although Stamper is very simple to use compared
16 // to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
17 // need full control over PDF creation use ElementBuilder/ElementWriter to add
18 // new content to existing PDF pages as shown in the ElementBuilder sample project.
19 //---------------------------------------------------------------------------------------
20 public static void main(String[] args) {
21 String input_path = "../../TestFiles/";
22 String output_path = "../../TestFiles/Output/";
23 String input_filename = "newsletter";
24
25 PDFNet.initialize(PDFTronLicense.Key());
26
27 //--------------------------------------------------------------------------------
28 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
29 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
30 doc.initSecurityHandler();
31 Stamper s = new Stamper(Stamper.e_relative_scale, 0.5, 0.5);
32 s.setAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_center);
33 ColorPt red = new ColorPt(1, 0, 0); // set text color to red
34 s.setFontColor(red);
35 String msg = "If you are reading this\nthis is an even page";
36 PageSet ps = new PageSet(1, doc.getPageCount());
37 s.stampText(doc, msg, ps);
38 //delete all text stamps in even pages
39 PageSet ps1 = new PageSet(1, doc.getPageCount(), PageSet.e_odd);
40 Stamper.deleteStamps(doc, ps1);
41
42 doc.save(output_path + input_filename + ".ex1.pdf", SDFDoc.SaveMode.LINEARIZED, null);
43 } catch (Exception e) {
44 e.printStackTrace();
45 return;
46 }
47
48 //--------------------------------------------------------------------------------
49 // Example 2) Add Image stamp to first 2 pages.
50 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
51 doc.initSecurityHandler();
52
53 Stamper s = new Stamper(Stamper.e_relative_scale, .05, .05);
54 Image img = Image.create(doc, input_path + "peppers.jpg");
55 s.setSize(Stamper.e_relative_scale, 0.5, 0.5);
56 //set position of the image to the center, left of PDF pages
57 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_center);
58 ColorPt pt = new ColorPt(0, 0, 0, 0);
59 s.setFontColor(pt);
60 s.setRotation(180);
61 s.setAsBackground(false);
62 //only stamp first 2 pages
63 PageSet ps = new PageSet(1, 2);
64 s.stampImage(doc, img, ps);
65
66 doc.save(output_path + input_filename + ".ex2.pdf", SDFDoc.SaveMode.LINEARIZED, null);
67 } catch (Exception e) {
68 e.printStackTrace();
69 return;
70 }
71
72 //--------------------------------------------------------------------------------
73 // Example 3) Add Page stamp to all pages.
74 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
75 doc.initSecurityHandler();
76
77 try (PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf")) {
78 fish_doc.initSecurityHandler();
79
80 Stamper s = new Stamper(Stamper.e_relative_scale, 0.5, 0.5);
81 Page src_page = fish_doc.getPage(1);
82 Rect page_one_crop = src_page.getCropBox();
83 // set size of the image to 10% of the original while keep the old aspect ratio
84 s.setSize(Stamper.e_absolute_size, page_one_crop.getWidth() * 0.1, -1);
85 s.setOpacity(0.4);
86 s.setRotation(-67);
87 //put the image at the bottom right hand corner
88 s.setAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom);
89 PageSet ps = new PageSet(1, doc.getPageCount());
90 s.stampPage(doc, src_page, ps);
91
92 doc.save(output_path + input_filename + ".ex3.pdf", SDFDoc.SaveMode.LINEARIZED, null);
93 }
94 } catch (Exception e) {
95 e.printStackTrace();
96 return;
97 }
98
99 //--------------------------------------------------------------------------------
100 // Example 4) Add Image stamp to first 20 odd pages.
101 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
102 doc.initSecurityHandler();
103
104 Stamper s = new Stamper(Stamper.e_absolute_size, 20, 20);
105 s.setOpacity(1);
106 s.setRotation(45);
107 s.setAsBackground(true);
108 s.setPosition(30, 40);
109 Image img = Image.create(doc, input_path + "peppers.jpg");
110 PageSet ps = new PageSet(1, 20, PageSet.e_odd);
111 s.stampImage(doc, img, ps);
112
113 doc.save(output_path + input_filename + ".ex4.pdf", SDFDoc.SaveMode.LINEARIZED, null);
114 } catch (Exception e) {
115 e.printStackTrace();
116 return;
117 }
118
119 //--------------------------------------------------------------------------------
120 // Example 5) Add text stamp to first 20 even pages
121 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
122 doc.initSecurityHandler();
123
124 Stamper s = new Stamper(Stamper.e_relative_scale, .05, .05);
125 s.setPosition(0, 0);
126 s.setOpacity(0.7);
127 s.setRotation(90);
128 s.setSize(Stamper.e_font_size, 80, -1);
129 s.setTextAlignment(Stamper.e_align_center);
130 PageSet ps = new PageSet(1, 20, PageSet.e_even);
131 s.stampText(doc, "Goodbye\nMoon", ps);
132
133 doc.save(output_path + input_filename + ".ex5.pdf", SDFDoc.SaveMode.LINEARIZED, null);
134 } catch (Exception e) {
135 e.printStackTrace();
136 return;
137 }
138
139 //--------------------------------------------------------------------------------
140 // Example 6) Add first page as stamp to all even pages
141 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
142 doc.initSecurityHandler();
143
144 PDFDoc fish_doc = new PDFDoc(input_path + "fish.pdf");
145 fish_doc.initSecurityHandler();
146
147 Stamper s = new Stamper(Stamper.e_relative_scale, 0.3, 0.3);
148 s.setOpacity(1);
149 s.setRotation(270);
150 s.setAsBackground(true);
151 s.setPosition(0.5, 0.5, true);
152 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_bottom);
153 Page page_one = fish_doc.getPage(1);
154 PageSet ps = new PageSet(1, doc.getPageCount(), PageSet.e_even);
155 s.stampPage(doc, page_one, ps);
156
157 doc.save(output_path + input_filename + ".ex6.pdf", SDFDoc.SaveMode.LINEARIZED, null);
158 } catch (Exception e) {
159 e.printStackTrace();
160 return;
161 }
162
163 //--------------------------------------------------------------------------------
164 // Example 7) Add image stamp at top right corner in every pages
165 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
166 doc.initSecurityHandler();
167
168 Stamper s = new Stamper(Stamper.e_relative_scale, .1, .1);
169 s.setOpacity(0.8);
170 s.setRotation(135);
171 s.setAsBackground(false);
172 s.showsOnPrint(false);
173 s.setAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_top);
174 s.setPosition(10, 10);
175
176 Image img = Image.create(doc, input_path + "peppers.jpg");
177 PageSet ps = new PageSet(1, doc.getPageCount(), PageSet.e_all);
178 s.stampImage(doc, img, ps);
179
180 doc.save(output_path + input_filename + ".ex7.pdf", SDFDoc.SaveMode.LINEARIZED, null);
181 } catch (Exception e) {
182 e.printStackTrace();
183 return;
184 }
185
186 //--------------------------------------------------------------------------------
187 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
188 // Because text stamp is set as background, the image is top of the text
189 // stamp. Text stamp on the first page is not visible.
190 try (PDFDoc doc = new PDFDoc(input_path + input_filename + ".pdf")) {
191 doc.initSecurityHandler();
192
193 Stamper s = new Stamper(Stamper.e_relative_scale, 0.07, -0.1);
194 s.setAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom);
195 s.setAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_top);
196 s.setFont(Font.create(doc, Font.e_courier, true));
197 ColorPt red = new ColorPt(1, 0, 0, 0);
198 s.setFontColor(red); //set color to red
199 s.setTextAlignment(Stamper.e_align_right);
200 s.setAsBackground(true); //set text stamp as background
201 PageSet ps = new PageSet(1, 2);
202 s.stampText(doc, "This is a title!", ps);
203
204 Image img = Image.create(doc, input_path + "peppers.jpg");
205 s.setAsBackground(false); // set image stamp as foreground
206 PageSet first_page_ps = new PageSet(1);
207 s.stampImage(doc, img, first_page_ps);
208
209 doc.save(output_path + input_filename + ".ex8.pdf", SDFDoc.SaveMode.LINEARIZED, null);
210 } catch (Exception e) {
211 e.printStackTrace();
212 return;
213 }
214
215 PDFNet.terminate();
216 }
217}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//---------------------------------------------------------------------------------------
7// The following sample shows how to add new content (or watermark) PDF pages
8// using 'pdftron.PDF.Stamper' utility class.
9//
10// Stamper can be used to PDF pages with text, images, or with other PDF content
11// in only a few lines of code. Although Stamper is very simple to use compared
12// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
13// need full control over PDF creation use ElementBuilder/ElementWriter to add
14// new content to existing PDF pages as shown in the ElementBuilder sample project.
15//---------------------------------------------------------------------------------------
16
17const { PDFNet } = require('@pdftron/pdfnet-node');
18const PDFTronLicense = require('../LicenseKey/LicenseKey');
19
20((exports) => {
21
22 exports.runStamperTest = () => {
23
24 const main = async () => {
25 // Relative path to the folder containing test files.
26 const inputPath = '../TestFiles/';
27 const outputPath = inputPath + 'Output/';
28
29 //--------------------------------------------------------------------------------
30 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
31 try {
32 // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
33 // all objects in memory that were initialized since the most recent startDeallocateStack call will be
34 // cleaned up. Doing this makes sure that memory growth does not get too high.
35 await PDFNet.startDeallocateStack();
36
37 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
38 doc.initSecurityHandler();
39
40 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
41 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_center);
42
43 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0);
44 stamper.setFontColor(redColorPt);
45 const pgSet = await PDFNet.PageSet.createRange(1, (await doc.getPageCount()));
46 stamper.stampText(doc, 'If you are reading this\nthis is an even page', pgSet);
47 const oddPgSet = await PDFNet.PageSet.createFilteredRange(1, (await doc.getPageCount()), PDFNet.PageSet.Filter.e_odd);
48 // delete all text stamps in odd pages
49 PDFNet.Stamper.deleteStamps(doc, oddPgSet);
50
51 await doc.save(outputPath + 'newsletter.ex1.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
52
53 await PDFNet.endDeallocateStack();
54 } catch (err) {
55 console.log(err.stack);
56 }
57
58 //--------------------------------------------------------------------------------
59 // Example 2) Add Image stamp to first 2 pages.
60 try {
61 await PDFNet.startDeallocateStack();
62 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
63 doc.initSecurityHandler();
64
65 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
66 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
67 stamper.setSize(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
68 // set position of the image to the center, left of PDF pages
69 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_center);
70
71 const blackColorPt = await PDFNet.ColorPt.init(0, 0, 0, 0);
72 stamper.setFontColor(blackColorPt);
73 stamper.setRotation(180);
74 stamper.setAsBackground(false);
75 // only stamp first 2 pages
76 const pgSet = await PDFNet.PageSet.createRange(1, 2);
77 stamper.stampImage(doc, img, pgSet);
78
79 await doc.save(outputPath + 'newsletter.ex2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
80 await PDFNet.endDeallocateStack();
81 } catch (err) {
82 console.log(err.stack);
83 }
84
85 //--------------------------------------------------------------------------------
86 // Example 3) Add Page stamp to all pages.
87 try {
88 await PDFNet.startDeallocateStack();
89 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
90 doc.initSecurityHandler();
91 const fishDoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
92 fishDoc.initSecurityHandler();
93
94 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
95 const srcPage = await fishDoc.getPage(1);
96 const pageOneCrop = await srcPage.getCropBox();
97 // set size of the image to 10% of the original while keep the old aspect ratio
98 stamper.setSize(PDFNet.Stamper.SizeType.e_absolute_size, (await pageOneCrop.width()) * 0.1, -1);
99 stamper.setOpacity(0.4);
100 stamper.setRotation(-67);
101
102 // put the image at the bottom right hand corner
103 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
104 const pgSet = await PDFNet.PageSet.createRange(1, (await doc.getPageCount()));
105 stamper.stampPage(doc, srcPage, pgSet);
106
107 await doc.save(outputPath + 'newsletter.ex3.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
108 await PDFNet.endDeallocateStack();
109 } catch (err) {
110 console.log(err.stack);
111 }
112
113 //--------------------------------------------------------------------------------
114 // Example 4) Add Image stamp to first 20 odd pages.
115 try {
116 await PDFNet.startDeallocateStack();
117 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
118 doc.initSecurityHandler();
119
120 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_absolute_size, 20, 20);
121 stamper.setOpacity(1);
122 stamper.setRotation(45);
123 stamper.setAsBackground(true);
124 stamper.setPosition(30, 40);
125 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
126
127 const pgSet = await PDFNet.PageSet.createFilteredRange(1, 20, PDFNet.PageSet.Filter.e_odd);
128 stamper.stampImage(doc, img, pgSet);
129
130 await doc.save(outputPath + 'newsletter.ex4.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
131 await PDFNet.endDeallocateStack();
132 } catch (err) {
133 console.log(err.stack);
134 }
135
136 //--------------------------------------------------------------------------------
137 // Example 5) Add text stamp to first 20 even pages
138 try {
139 await PDFNet.startDeallocateStack();
140
141 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
142 doc.initSecurityHandler();
143
144 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.05, 0.05);
145 stamper.setPosition(0, 0);
146 stamper.setOpacity(0.7);
147 stamper.setRotation(90);
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 await doc.save(outputPath + 'newsletter.ex5.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
154
155 await PDFNet.endDeallocateStack();
156 } catch (err) {
157 console.log(err.stack);
158 }
159
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.createFromFilePath(inputPath + 'newsletter.pdf');
166 doc.initSecurityHandler();
167
168 const fishDoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
169 fishDoc.initSecurityHandler();
170
171 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.3, 0.3);
172 stamper.setOpacity(1);
173 stamper.setRotation(270);
174 stamper.setAsBackground(true);
175 stamper.setPosition(0.5, 0.5, true);
176 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
177 const pgOne = await fishDoc.getPage(1);
178 const pgSet = await PDFNet.PageSet.createFilteredRange(1, await doc.getPageCount(), PDFNet.PageSet.Filter.e_even);
179 stamper.stampPage(doc, pgOne, pgSet);
180
181 await doc.save(outputPath + 'newsletter.ex6.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
182
183 await PDFNet.endDeallocateStack();
184 } catch (err) {
185 console.log(err.stack);
186 }
187
188 //--------------------------------------------------------------------------------
189 // Example 7) Add image stamp at top right corner in every pages
190 try {
191 await PDFNet.startDeallocateStack();
192
193 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
194 doc.initSecurityHandler();
195
196 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.1, 0.1);
197 stamper.setOpacity(0.8);
198 stamper.setRotation(135);
199 stamper.setAsBackground(false);
200 stamper.showsOnPrint(false);
201 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
202 stamper.setPosition(10, 10);
203
204 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
205 const pgSet = await PDFNet.PageSet.createFilteredRange(1, (await doc.getPageCount()), PDFNet.PageSet.Filter.e_all);
206 stamper.stampImage(doc, img, pgSet);
207
208 await doc.save(outputPath + 'newsletter.ex7.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
209
210 await PDFNet.endDeallocateStack();
211 } catch (err) {
212 console.log(err.stack);
213 }
214
215 //--------------------------------------------------------------------------------
216 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
217 // Because text stamp is set as background, the image is top of the text
218 // stamp. Text stamp on the first page is not visible.
219 try {
220 await PDFNet.startDeallocateStack();
221
222 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
223 doc.initSecurityHandler();
224
225 const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.07, -0.1);
226 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
227 stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
228 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_courier);
229 stamper.setFont(font);
230 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
231 stamper.setFontColor(redColorPt);
232 stamper.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_right);
233 stamper.setAsBackground(true);
234
235 const pgSet = await PDFNet.PageSet.createRange(1, 2);
236 stamper.stampText(doc, 'This is a title!', pgSet);
237
238 const img = await PDFNet.Image.createFromFile(doc, inputPath + 'peppers.jpg');
239 stamper.setAsBackground(false);
240
241 const pgSetImage = await PDFNet.PageSet.createRange(1, 1);
242 stamper.stampImage(doc, img, pgSetImage);
243
244 await doc.save(outputPath + 'newsletter.ex8.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
245
246 await PDFNet.endDeallocateStack();
247 } catch (err) {
248 console.log(err.stack);
249 }
250 };
251 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
252 };
253 exports.runStamperTest();
254})(exports);
255// eslint-disable-next-line spaced-comment
256//# sourceURL=StamperTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13$input_filename = "newsletter";
14
15//---------------------------------------------------------------------------------------
16// The following sample shows how to add new content (or watermark) PDF pages
17// using 'pdftron.PDF.Stamper' utility class.
18//
19// Stamper can be used to PDF pages with text, images, or with other PDF content
20// in only a few lines of code. Although Stamper is very simple to use compared
21// to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
22// need full control over PDF creation use ElementBuilder/ElementWriter to add
23// new content to existing PDF pages as shown in the ElementBuilder sample project.
24//---------------------------------------------------------------------------------------
25
26 PDFNet::Initialize($LicenseKey);
27 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
28
29 //--------------------------------------------------------------------------------
30 // Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
31
32 $doc = new PDFDoc($input_path.$input_filename.".pdf");
33 $doc->InitSecurityHandler();
34 $s = new Stamper(Stamper::e_relative_scale, 0.5, 0.5);
35
36 $s->SetAlignment(Stamper::e_horizontal_center, Stamper::e_vertical_center);
37 $red = new ColorPt(1.0, 0.0, 0.0); // set text color to red
38 $s->SetFontColor($red);
39 $s->StampText($doc, "If you are reading this\nthis is an even page", new PageSet(1, $doc->GetPageCount()));
40 //delete all text stamps in even pages
41 Stamper::DeleteStamps($doc, new PageSet(1, $doc->GetPageCount(), PageSet::e_odd));
42
43 $doc->Save($output_path.$input_filename.".ex1.pdf", SDFDoc::e_linearized);
44 $doc->Close();
45
46 //--------------------------------------------------------------------------------
47 // Example 2) Add Image stamp to first 2 pages.
48
49 $doc = new PDFDoc($input_path.$input_filename.".pdf");
50 $doc->InitSecurityHandler();
51
52 $s = new Stamper(Stamper::e_relative_scale, 0.05, 0.05);
53 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
54 $s->SetSize(Stamper::e_relative_scale, 0.5, 0.5);
55 //set position of the image to the center, left of PDF pages
56 $s->SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_center);
57 $pt = new ColorPt(0.0, 0.0, 0.0, 0.0);
58 $s->SetFontColor($pt);
59 $s->SetRotation(180);
60 $s->SetAsBackground(false);
61 //only stamp first 2 pages
62 $ps = new PageSet(1, 2);
63 $s->StampImage($doc, $img, $ps);
64
65 $doc->Save($output_path.$input_filename.".ex2.pdf", SDFDoc::e_linearized);
66 $doc->Close();
67
68 //--------------------------------------------------------------------------------
69 // Example 3) Add Page stamp to all pages.
70
71 $doc = new PDFDoc($input_path.$input_filename.".pdf");
72 $doc->InitSecurityHandler();
73
74 $fish_doc = new PDFDoc($input_path."fish.pdf");
75 $fish_doc->InitSecurityHandler();
76
77 $s = new Stamper(Stamper::e_relative_scale, 0.5, 0.5);
78 $src_page = $fish_doc->GetPage(1);
79 $page_one_crop = $src_page->GetCropBox();
80 // set size of the image to 10% of the original while keep the old aspect ratio
81 $s->SetSize(Stamper::e_absolute_size, $page_one_crop->Width() * 0.1, -1);
82 $s->SetOpacity(0.4);
83 $s->SetRotation(-67);
84 //put the image at the bottom right hand corner
85 $s->SetAlignment(Stamper::e_horizontal_right, Stamper::e_vertical_bottom);
86 $ps = new PageSet(1, $doc->GetPageCount());
87 $s->StampPage($doc, $src_page, $ps);
88
89 $doc->Save($output_path.$input_filename.".ex3.pdf", SDFDoc::e_linearized);
90 $doc->Close();
91
92 //--------------------------------------------------------------------------------
93 // Example 4) Add Image stamp to first 20 odd pages.
94
95 $doc = new PDFDoc($input_path.$input_filename.".pdf");
96 $doc->InitSecurityHandler();
97
98 $s = new Stamper(Stamper::e_absolute_size, 20.0, 20.0);
99 $s->SetOpacity(1);
100 $s->SetRotation(45);
101 $s->SetAsBackground(true);
102 $s->SetPosition(30.0, 40.0);
103 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
104 $ps = new PageSet(1, 20, PageSet::e_odd);
105 $s->StampImage($doc, $img, $ps);
106
107 $doc->Save($output_path.$input_filename.".ex4.pdf", SDFDoc::e_linearized);
108 $doc->Close();
109
110 //--------------------------------------------------------------------------------
111 // Example 5) Add text stamp to first 20 even pages
112
113 $doc = new PDFDoc($input_path.$input_filename.".pdf");
114 $doc->InitSecurityHandler();
115
116 $s = new Stamper(Stamper::e_relative_scale, 0.05, 0.05);
117 $s->SetPosition(0.0, 0.0);
118 $s->SetOpacity(0.7);
119 $s->SetRotation(90);
120 $s->SetSize(Stamper::e_font_size, 80, -1);
121 $s->SetTextAlignment(Stamper::e_align_center);
122 $ps = new PageSet(1, 20, PageSet::e_even);
123 $s->StampText($doc, "Goodbye\nMoon", $ps);
124
125 $doc->Save($output_path.$input_filename.".ex5.pdf", SDFDoc::e_linearized);
126 $doc->Close();
127
128 //--------------------------------------------------------------------------------
129 // Example 6) Add first page as stamp to all even pages
130
131 $doc = new PDFDoc($input_path.$input_filename.".pdf");
132 $doc->InitSecurityHandler();
133
134 $fish_doc = new PDFDoc($input_path."fish.pdf");
135 $fish_doc->InitSecurityHandler();
136
137 $s = new Stamper(Stamper::e_relative_scale, 0.3, 0.3);
138 $s->SetOpacity(1);
139 $s->SetRotation(270);
140 $s->SetAsBackground(true);
141 $s->SetPosition(0.5, 0.5, true);
142 $s->SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_bottom);
143 $page_one = $fish_doc->GetPage(1);
144 $ps = new PageSet(1, $doc->GetPageCount(), PageSet::e_even);
145 $s->StampPage($doc, $page_one, $ps);
146
147 $doc->Save($output_path.$input_filename.".ex6.pdf", SDFDoc::e_linearized);
148 $doc->Close();
149
150 //--------------------------------------------------------------------------------
151 // Example 7) Add image stamp at top right corner in every pages
152
153 $doc = new PDFDoc($input_path.$input_filename.".pdf");
154 $doc->InitSecurityHandler();
155
156 $s = new Stamper(Stamper::e_relative_scale, 0.1, 0.1);
157 $s->SetOpacity(0.8);
158 $s->SetRotation(135);
159 $s->SetAsBackground(false);
160 $s->ShowsOnPrint(false);
161 $s->SetAlignment(Stamper::e_horizontal_left, Stamper::e_vertical_top);
162 $s->SetPosition(10.0, 10.0);
163
164 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
165 $ps = new PageSet(1, $doc->GetPageCount(), PageSet::e_all);
166 $s->StampImage($doc, $img, $ps);
167
168 $doc->Save($output_path.$input_filename.".ex7.pdf", SDFDoc::e_linearized);
169 $doc->Close();
170
171 //--------------------------------------------------------------------------------
172 // Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
173 // Because text stamp is set as background, the image is top of the text
174 // stamp. Text stamp on the first page is not visible.
175
176 $doc = new PDFDoc($input_path.$input_filename.".pdf");
177 $doc->InitSecurityHandler();
178
179 $s = new Stamper(Stamper::e_relative_scale, 0.07, -0.1);
180 $s->SetAlignment(Stamper::e_horizontal_right, Stamper::e_vertical_bottom);
181 $s->SetAlignment(Stamper::e_horizontal_center, Stamper::e_vertical_top);
182 $s->SetFont(Font::Create($doc->GetSDFDoc(), Font::e_courier, true));
183 $red = new ColorPt(1.0, 0.0, 0.0, 0.0);
184 $s->SetFontColor($red); //set color to red
185 $s->SetTextAlignment(Stamper::e_align_right);
186 $s->SetAsBackground(true); //set text stamp as background
187 $ps = new PageSet(1, 2);
188 $s->StampText($doc, "This is a title!", $ps);
189
190 $img = Image::Create($doc->GetSDFDoc(), $input_path."peppers.jpg");
191 $s->SetAsBackground(false); // set image stamp as foreground
192 $first_page_ps = new PageSet(1);
193 $s->StampImage($doc, $img, $first_page_ps);
194
195 $doc->Save($output_path.$input_filename.".ex8.pdf", SDFDoc::e_linearized);
196 $doc->Close();
197 PDFNet::Terminate();
198
199?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#---------------------------------------------------------------------------------------
15# The following sample shows how to add new content (or watermark) PDF pages
16# using 'pdftron.PDF.Stamper' utility class.
17#
18# Stamper can be used to PDF pages with text, images, or with other PDF content
19# in only a few lines of code. Although Stamper is very simple to use compared
20# to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
21# need full control over PDF creation use ElementBuilder/ElementWriter to add
22# new content to existing PDF pages as shown in the ElementBuilder sample project.
23#---------------------------------------------------------------------------------------
24
25# Relative path to the folder containing the test files.
26input_path = "../../TestFiles/"
27output_path = "../../TestFiles/Output/"
28input_filename = "newsletter"
29
30def main():
31 # Initialize PDFNet
32 PDFNet.Initialize(LicenseKey)
33
34 #--------------------------------------------------------------------------------
35 # Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
36 doc = PDFDoc(input_path + input_filename + ".pdf")
37 doc.InitSecurityHandler()
38 s = Stamper(Stamper.e_relative_scale, 0.5, 0.5)
39
40 s.SetAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_center)
41 red = ColorPt(1, 0, 0) # set text color to red
42 s.SetFontColor(red)
43 s.StampText(doc, "If you are reading this\nthis is an even page", PageSet(1, doc.GetPageCount()))
44 # delete all text stamps in odd pages
45 Stamper.DeleteStamps(doc, PageSet(1, doc.GetPageCount(), PageSet.e_odd))
46
47 doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc.e_linearized)
48 doc.Close()
49
50 #--------------------------------------------------------------------------------
51 # Example 2) Add Image stamp to first 2 pages.
52
53 doc = PDFDoc(input_path + input_filename + ".pdf")
54 doc.InitSecurityHandler()
55 s = Stamper(Stamper.e_relative_scale, 0.05, 0.05)
56 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
57 s.SetSize(Stamper.e_relative_scale, 0.5, 0.5)
58
59 # set position of the image to the center, left of PDF pages
60 s.SetAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_center)
61 pt = ColorPt(0, 0, 0, 0)
62 s.SetFontColor(pt)
63 s.SetRotation(180)
64 s.SetAsBackground(False)
65 # only stamp first 2 pages
66 ps = PageSet(1, 2)
67 s.StampImage(doc, img, ps)
68
69 doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc.e_linearized)
70 doc.Close()
71
72 #--------------------------------------------------------------------------------
73 # Example 3) Add Page stamp to all pages.
74
75 doc = PDFDoc(input_path + input_filename + ".pdf")
76 doc.InitSecurityHandler()
77
78 fish_doc = PDFDoc(input_path + "fish.pdf")
79 fish_doc.InitSecurityHandler()
80 s = Stamper(Stamper.e_relative_scale, 0.5, 0.5)
81 src_page = fish_doc.GetPage(1)
82 page_one_crop = src_page.GetCropBox()
83 # set size of the image to 10% of the original while keep the old aspect ratio
84 s.SetSize(Stamper.e_absolute_size, page_one_crop.Width() * 0.1, -1)
85 s.SetOpacity(0.4)
86 s.SetRotation(-67)
87 # put the image at the bottom right hand corner
88 s.SetAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom)
89 ps = PageSet(1, doc.GetPageCount())
90 s.StampPage(doc, src_page, ps)
91 doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc.e_linearized)
92 doc.Close()
93
94 #--------------------------------------------------------------------------------
95 # Example 4) Add Image stamp to first 20 odd pages.
96
97 doc = PDFDoc(input_path + input_filename + ".pdf")
98 doc.InitSecurityHandler()
99
100 s = Stamper(Stamper.e_absolute_size, 20, 20)
101 s.SetOpacity(1)
102 s.SetRotation(45)
103 s.SetAsBackground(True)
104 s.SetPosition(30, 40)
105 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
106 ps = PageSet(1, 20, PageSet.e_odd)
107 s.StampImage(doc, img, ps)
108
109 doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc.e_linearized)
110 doc.Close()
111
112 #--------------------------------------------------------------------------------
113 # Example 5) Add text stamp to first 20 even pages
114
115 doc = PDFDoc(input_path + input_filename + ".pdf")
116 doc.InitSecurityHandler()
117 s = Stamper(Stamper.e_relative_scale, 0.05, 0.05)
118 s.SetPosition(0, 0)
119 s.SetOpacity(0.7)
120 s.SetRotation(90)
121 s.SetSize(Stamper.e_font_size, 80, -1)
122 s.SetTextAlignment(Stamper.e_align_center)
123 ps = PageSet(1, 20, PageSet.e_even)
124 s.StampText(doc, "Goodbye\nMoon", ps)
125
126 doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc.e_linearized)
127 doc.Close()
128
129 #--------------------------------------------------------------------------------
130 # Example 6) Add first page as stamp to all even pages
131
132 doc = PDFDoc(input_path + input_filename + ".pdf")
133 doc.InitSecurityHandler()
134
135 fish_doc = PDFDoc(input_path + "fish.pdf");
136 fish_doc.InitSecurityHandler()
137
138 s = Stamper(Stamper.e_relative_scale, 0.3, 0.3)
139 s.SetOpacity(1)
140 s.SetRotation(270)
141 s.SetAsBackground(True)
142 s.SetPosition(0.5, 0.5, True)
143 s.SetAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_bottom)
144 page_one = fish_doc.GetPage(1)
145 ps = PageSet(1, doc.GetPageCount(), PageSet.e_even)
146 s.StampPage(doc, page_one, ps)
147
148 doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc.e_linearized)
149 doc.Close()
150
151 #--------------------------------------------------------------------------------
152 # Example 7) Add image stamp at top left corner in every pages
153
154 doc = PDFDoc(input_path + input_filename + ".pdf")
155 doc.InitSecurityHandler()
156
157 s = Stamper(Stamper.e_relative_scale, 0.1, 0.1)
158 s.SetOpacity(0.8)
159 s.SetRotation(135)
160 s.SetAsBackground(False)
161 s.ShowsOnPrint(False)
162 s.SetAlignment(Stamper.e_horizontal_left, Stamper.e_vertical_top)
163 s.SetPosition(10, 10)
164 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
165 ps = PageSet(1, doc.GetPageCount(), PageSet.e_all)
166 s.StampImage(doc, img, ps)
167 doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc.e_linearized)
168 doc.Close()
169
170 #--------------------------------------------------------------------------------
171 # Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
172 # Because text stamp is set as background, the image is top of the text
173 # stamp. Text stamp on the first page is not visible.
174
175 doc = PDFDoc(input_path + input_filename + ".pdf")
176 doc.InitSecurityHandler()
177
178 s = Stamper(Stamper.e_relative_scale, 0.07, -0.1)
179 s.SetAlignment(Stamper.e_horizontal_right, Stamper.e_vertical_bottom)
180 s.SetAlignment(Stamper.e_horizontal_center, Stamper.e_vertical_top)
181 s.SetFont(Font.Create(doc.GetSDFDoc(), Font.e_courier, True))
182 red = ColorPt(1, 0, 0)
183 s.SetFontColor(red) # set text color to red
184 s.SetTextAlignment(Stamper.e_align_right)
185 s.SetAsBackground(True) # set text stamp as background
186 ps = PageSet(1, 2)
187 s.StampText(doc, "This is a title!", ps)
188
189 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
190 s.SetAsBackground(False) # set image stamp as foreground
191 first_page_ps = PageSet(1)
192 s.StampImage(doc, img, first_page_ps)
193
194 doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc.e_linearized)
195 doc.Close()
196 PDFNet.Terminate()
197
198if __name__ == '__main__':
199 main()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample shows how to add new content (or watermark) PDF pages
14# using 'pdftron.PDF.Stamper' utility class.
15#
16# Stamper can be used to PDF pages with text, images, or with other PDF content
17# in only a few lines of code. Although Stamper is very simple to use compared
18# to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
19# need full control over PDF creation use ElementBuilder/ElementWriter to add
20# new content to existing PDF pages as shown in the ElementBuilder sample project.
21#---------------------------------------------------------------------------------------
22
23# Relative path to the folder containing the test files.
24input_path = "../../TestFiles/"
25output_path = "../../TestFiles/Output/"
26input_filename = "newsletter"
27
28 # Initialize PDFNet
29 PDFNet.Initialize(PDFTronLicense.Key)
30
31 #--------------------------------------------------------------------------------
32 # Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
33
34 doc = PDFDoc.new(input_path + input_filename + ".pdf")
35 doc.InitSecurityHandler
36 s = Stamper.new(Stamper::E_relative_scale, 0.5, 0.5)
37
38 s.SetAlignment(Stamper::E_horizontal_center, Stamper::E_vertical_center)
39 red = ColorPt.new(1, 0, 0) # set text color to red
40 s.SetFontColor(red)
41 s.StampText(doc, "If you are reading this\nthis is an even page", PageSet.new(1, doc.GetPageCount))
42 # delete all text stamps in odd pages
43 Stamper.DeleteStamps(doc, PageSet.new(1, doc.GetPageCount, PageSet::E_odd))
44
45 doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc::E_linearized)
46 doc.Close
47
48 #--------------------------------------------------------------------------------
49 # Example 2) Add Image stamp to first 2 pages.
50
51 doc = PDFDoc.new(input_path + input_filename + ".pdf")
52 doc.InitSecurityHandler
53 s = Stamper.new(Stamper::E_relative_scale, 0.05, 0.05)
54 img = Image.Create(doc.GetSDFDoc, input_path + "peppers.jpg")
55 s.SetSize(Stamper::E_relative_scale, 0.5, 0.5)
56
57 # set position of the image to the center, left of PDF pages
58 s.SetAlignment(Stamper::E_horizontal_left, Stamper::E_vertical_center)
59 pt = ColorPt.new(0, 0, 0, 0)
60 s.SetFontColor(pt)
61 s.SetRotation(180)
62 s.SetAsBackground(false)
63 # only stamp first 2 pages
64 ps = PageSet.new(1, 2)
65 s.StampImage(doc, img, ps)
66
67 doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc::E_linearized)
68 doc.Close
69
70 #--------------------------------------------------------------------------------
71 # Example 3) Add Page stamp to all pages.
72
73 doc = PDFDoc.new(input_path + input_filename + ".pdf")
74 doc.InitSecurityHandler
75
76 fish_doc = PDFDoc.new(input_path + "fish.pdf")
77 fish_doc.InitSecurityHandler
78 s = Stamper.new(Stamper::E_relative_scale, 0.5, 0.5)
79 src_page = fish_doc.GetPage(1)
80 page_one_crop = src_page.GetCropBox
81 # set size of the image to 10% of the original while keep the old aspect ratio
82 s.SetSize(Stamper::E_absolute_size, page_one_crop.Width * 0.1, -1)
83 s.SetOpacity(0.4)
84 s.SetRotation(-67)
85 # put the image at the bottom right hand corner
86 s.SetAlignment(Stamper::E_horizontal_right, Stamper::E_vertical_bottom)
87 ps = PageSet.new(1, doc.GetPageCount)
88 s.StampPage(doc, src_page, ps)
89 doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc::E_linearized)
90 doc.Close
91
92 #--------------------------------------------------------------------------------
93 # Example 4) Add Image stamp to first 20 odd pages.
94
95 doc = PDFDoc.new(input_path + input_filename + ".pdf")
96 doc.InitSecurityHandler
97
98 s = Stamper.new(Stamper::E_absolute_size, 20, 20)
99 s.SetOpacity(1)
100 s.SetRotation(45)
101 s.SetAsBackground(true)
102 s.SetPosition(30, 40)
103 img = Image.Create(doc.GetSDFDoc, input_path + "peppers.jpg")
104 ps = PageSet.new(1, 20, PageSet::E_odd)
105 s.StampImage(doc, img, ps)
106
107 doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc::E_linearized)
108 doc.Close
109
110 #--------------------------------------------------------------------------------
111 # Example 5) Add text stamp to first 20 even pages
112
113 doc = PDFDoc.new(input_path + input_filename + ".pdf")
114 doc.InitSecurityHandler
115 s = Stamper.new(Stamper::E_relative_scale, 0.05, 0.05)
116 s.SetPosition(0, 0)
117 s.SetOpacity(0.7)
118 s.SetRotation(90)
119 s.SetSize(Stamper::E_font_size, 80, -1)
120 s.SetTextAlignment(Stamper::E_align_center)
121 ps = PageSet.new(1, 20, PageSet::E_even)
122 s.StampText(doc, "Goodbye\nMoon", ps)
123
124 doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc::E_linearized)
125 doc.Close
126
127 #--------------------------------------------------------------------------------
128 # Example 6) Add first page as stamp to all even pages
129
130 doc = PDFDoc.new(input_path + input_filename + ".pdf")
131 doc.InitSecurityHandler
132
133 fish_doc = PDFDoc.new(input_path + "fish.pdf");
134 fish_doc.InitSecurityHandler
135
136 s = Stamper.new(Stamper::E_relative_scale, 0.3, 0.3)
137 s.SetOpacity(1)
138 s.SetRotation(270)
139 s.SetAsBackground(true)
140 s.SetPosition(0.5, 0.5, true)
141 s.SetAlignment(Stamper::E_horizontal_left, Stamper::E_vertical_bottom)
142 page_one = fish_doc.GetPage(1)
143 ps = PageSet.new(1, doc.GetPageCount, PageSet::E_even)
144 s.StampPage(doc, page_one, ps)
145
146 doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc::E_linearized)
147 doc.Close
148
149 #--------------------------------------------------------------------------------
150 # Example 7) Add image stamp at top left corner in every pages
151
152 doc = PDFDoc.new(input_path + input_filename + ".pdf")
153 doc.InitSecurityHandler
154
155 s = Stamper.new(Stamper::E_relative_scale, 0.1, 0.1)
156 s.SetOpacity(0.8)
157 s.SetRotation(135)
158 s.SetAsBackground(false)
159 s.ShowsOnPrint(false)
160 s.SetAlignment(Stamper::E_horizontal_left, Stamper::E_vertical_top)
161 s.SetPosition(10, 10)
162 img = Image.Create(doc.GetSDFDoc, input_path + "peppers.jpg")
163 ps = PageSet.new(1, doc.GetPageCount, PageSet::E_all)
164 s.StampImage(doc, img, ps)
165 doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc::E_linearized)
166 doc.Close
167
168 #--------------------------------------------------------------------------------
169 # Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
170 # Because text stamp is set as background, the image is top of the text
171 # stamp. Text stamp on the first page is not visible.
172
173 doc = PDFDoc.new(input_path + input_filename + ".pdf")
174 doc.InitSecurityHandler
175
176 s = Stamper.new(Stamper::E_relative_scale, 0.07, -0.1)
177 s.SetAlignment(Stamper::E_horizontal_right, Stamper::E_vertical_bottom)
178 s.SetAlignment(Stamper::E_horizontal_center, Stamper::E_vertical_top)
179 s.SetFont(Font.Create(doc.GetSDFDoc, Font::E_courier, true))
180 red = ColorPt.new(1, 0, 0)
181 s.SetFontColor(red) # set text color to red
182 s.SetTextAlignment(Stamper::E_align_right)
183 s.SetAsBackground(true) # set text stamp as background
184 ps = PageSet.new(1, 2)
185 s.StampText(doc, "This is a title!", ps)
186
187 img = Image.Create(doc.GetSDFDoc, input_path + "peppers.jpg")
188 s.SetAsBackground(false) # set image stamp as foreground
189 first_page_ps = PageSet.new(1)
190 s.StampImage(doc, img, first_page_ps)
191
192 doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc::E_linearized)
193 doc.Close
194 PDFNet.Terminate
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4Imports System
5
6Imports pdftron
7Imports pdftron.Common
8Imports pdftron.Filters
9Imports pdftron.SDF
10Imports pdftron.PDF
11
12Module StamperTestVB
13 Dim pdfNetLoader As PDFNetLoader
14 Sub New()
15 pdfNetLoader = pdftron.PDFNetLoader.Instance()
16 End Sub
17
18 ' The following sample shows how to add new content (or watermark) PDF pages
19 ' using 'pdftron.PDF.Stamper' utility class.
20 '
21 ' Stamper can be used to PDF pages with text, images, or with other PDF content
22 ' in only a few lines of code. Although Stamper is very simple to use compared
23 ' to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you
24 ' need full control over PDF creation use ElementBuilder/ElementWriter to add
25 ' new content to existing PDF pages as shown in the ElementBuilder sample project.
26 Sub Main()
27
28 PDFNet.Initialize(PDFTronLicense.Key)
29
30 Dim input_path As String = "../../../../TestFiles/"
31 Dim output_path As String = "../../../../TestFiles/Output/"
32 Dim input_filename As String = "newsletter"
33
34 '--------------------------------------------------------------------------------
35 ' Example 1) Add text stamp to all pages, then remove text stamp from odd pages.
36 Try
37 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
38 doc.InitSecurityHandler()
39
40 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5)
41 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center)
42 s.SetFontColor(New ColorPt(1, 0, 0)) ' set text color to red
43 s.StampText(doc, "If you are reading this" + ControlChars.Lf + "this is an even page", New PageSet(1, doc.GetPageCount()))
44 'delete all text stamps in even pages
45 Stamper.DeleteStamps(doc, New PageSet(1, doc.GetPageCount(), PageSet.Filter.e_odd))
46
47 doc.Save(output_path + input_filename + ".ex1.pdf", SDFDoc.SaveOptions.e_linearized)
48 End Using
49 End Using
50 Catch ex As PDFNetException
51 Console.WriteLine(ex.Message)
52 Catch ex As Exception
53 MsgBox(ex.Message)
54 End Try
55
56
57 '--------------------------------------------------------------------------------
58 ' Example 2) Add Image stamp to first 2 pages.
59 Try
60 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
61 doc.InitSecurityHandler()
62
63 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.05, 0.05)
64 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
65 s.SetSize(Stamper.SizeType.e_relative_scale, 0.5, 0.5)
66 'set position of the image to the center, left of PDF pages
67 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_center)
68 s.SetFontColor(New ColorPt(0, 0, 0, 0))
69 s.SetRotation(180)
70 s.SetAsBackground(False)
71 'only stamp first 2 pages
72 s.StampImage(doc, img, New PageSet(1, 2))
73
74 doc.Save(output_path + input_filename + ".ex2.pdf", SDFDoc.SaveOptions.e_linearized)
75 End Using
76 End Using
77 Catch ex As PDFNetException
78 Console.WriteLine(ex.Message)
79 Catch ex As Exception
80 MsgBox(ex.Message)
81 End Try
82
83 '--------------------------------------------------------------------------------
84 ' Example 3) Add Page stamp to all pages.
85 Try
86 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
87 doc.InitSecurityHandler()
88
89 Using fish_doc As PDFDoc = New PDFDoc(input_path + "fish.pdf")
90 fish_doc.InitSecurityHandler()
91
92 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5)
93 Dim src_page As Page = fish_doc.GetPage(1)
94 Dim page_one_crop As Rect = src_page.GetCropBox()
95 ' set size of the image to 10% of the original while keep the old aspect ratio
96 s.SetSize(Stamper.SizeType.e_absolute_size, page_one_crop.Width() * 0.1, -1)
97 s.SetOpacity(0.4)
98 s.SetRotation(-67)
99 'put the image at the bottom right hand corner
100 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom)
101 s.StampPage(doc, src_page, New PageSet(1, doc.GetPageCount()))
102
103 doc.Save(output_path + input_filename + ".ex3.pdf", SDFDoc.SaveOptions.e_linearized)
104 End Using
105 End Using
106 End Using
107 Catch ex As PDFNetException
108 Console.WriteLine(ex.Message)
109 Catch ex As Exception
110 MsgBox(ex.Message)
111 End Try
112
113 '--------------------------------------------------------------------------------
114 ' Example 4) Add Image stamp to first 20 odd pages.
115 Try
116 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
117 doc.InitSecurityHandler()
118
119 Using s As Stamper = New Stamper(Stamper.SizeType.e_absolute_size, 20, 20)
120 s.SetOpacity(1)
121 s.SetRotation(45)
122 s.SetAsBackground(True)
123 s.SetPosition(30, 40)
124 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
125 s.StampImage(doc, img, New PageSet(1, 20, PageSet.Filter.e_odd))
126
127 doc.Save(output_path + input_filename + ".ex4.pdf", SDFDoc.SaveOptions.e_linearized)
128 End Using
129 End Using
130 Catch ex As PDFNetException
131 Console.WriteLine(ex.Message)
132 Catch ex As Exception
133 MsgBox(ex.Message)
134 End Try
135
136 '--------------------------------------------------------------------------------
137 ' Example 5) Add text stamp to first 20 even pages
138 Try
139 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
140 doc.InitSecurityHandler()
141
142 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.05, 0.05)
143 s.SetPosition(0, 0)
144 s.SetOpacity(0.7)
145 s.SetRotation(90)
146 s.SetSize(Stamper.SizeType.e_font_size, 80, -1)
147 s.SetTextAlignment(Stamper.TextAlignment.e_align_center)
148 s.StampText(doc, "Goodbye" + ControlChars.Lf + "Moon", New PageSet(1, 20, PageSet.Filter.e_even))
149
150 doc.Save(output_path + input_filename + ".ex5.pdf", SDFDoc.SaveOptions.e_linearized)
151 End Using
152 End Using
153 Catch ex As PDFNetException
154 Console.WriteLine(ex.Message)
155 Catch ex As Exception
156 MsgBox(ex.Message)
157 End Try
158
159
160 '--------------------------------------------------------------------------------
161 ' Example 6) Add first page as stamp to all even pages
162 Try
163 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
164 doc.InitSecurityHandler()
165
166 Using fish_doc As PDFDoc = New PDFDoc(input_path + "fish.pdf")
167 fish_doc.InitSecurityHandler()
168
169 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.3, 0.3)
170 s.SetOpacity(1)
171 s.SetRotation(270)
172 s.SetAsBackground(True)
173 s.SetPosition(0.5, 0.5, True)
174 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_bottom)
175 Dim page_one As Page = fish_doc.GetPage(1)
176 s.StampPage(doc, page_one, New PageSet(1, doc.GetPageCount(), PageSet.Filter.e_even))
177
178 doc.Save(output_path + input_filename + ".ex6.pdf", SDFDoc.SaveOptions.e_linearized)
179 End Using
180 End Using
181 End Using
182 Catch ex As PDFNetException
183 Console.WriteLine(ex.Message)
184 Catch ex As Exception
185 MsgBox(ex.Message)
186 End Try
187
188 '--------------------------------------------------------------------------------
189 ' Example 7) Add image stamp at top right corner in every pages
190 Try
191 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
192 doc.InitSecurityHandler()
193
194 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.1, 0.1)
195 s.SetOpacity(0.8)
196 s.SetRotation(135)
197 s.SetAsBackground(False)
198 s.ShowsOnPrint(False)
199 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_left, Stamper.VerticalAlignment.e_vertical_top)
200 s.SetPosition(10, 10)
201
202 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
203 s.StampImage(doc, img, New PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all))
204
205 doc.Save(output_path + input_filename + ".ex7.pdf", SDFDoc.SaveOptions.e_linearized)
206 End Using
207 End Using
208 Catch ex As PDFNetException
209 Console.WriteLine(ex.Message)
210 Catch ex As Exception
211 MsgBox(ex.Message)
212 End Try
213
214 '--------------------------------------------------------------------------------
215 ' Example 8) Add Text stamp to first 2 pages, and image stamp to first page.
216 ' Because text stamp is set as background, the image is top of the text
217 ' stamp. Text stamp on the first page is not visible.
218 Try
219 Using doc As PDFDoc = New PDFDoc(input_path + input_filename + ".pdf")
220 doc.InitSecurityHandler()
221
222 Using s As Stamper = New Stamper(Stamper.SizeType.e_relative_scale, 0.07, -0.1)
223 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom)
224 s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_top)
225 s.SetFont(Font.Create(doc, Font.StandardType1Font.e_courier, True))
226 s.SetFontColor(New ColorPt(1, 0, 0, 0)) 'set color to red
227 s.SetTextAlignment(Stamper.TextAlignment.e_align_right)
228 s.SetAsBackground(True) 'set text stamp as background
229 s.StampText(doc, "This is a title!", New PageSet(1, 2))
230
231 Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
232 s.SetAsBackground(False) ' set image stamp as foreground
233 s.StampImage(doc, img, New PageSet(1))
234
235 doc.Save(output_path + input_filename + ".ex8.pdf", SDFDoc.SaveOptions.e_linearized)
236 End Using
237 End Using
238 Catch ex As PDFNetException
239 Console.WriteLine(ex.Message)
240 Catch ex As Exception
241 MsgBox(ex.Message)
242 End Try
243 PDFNet.Terminate()
244 End Sub
245End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales