Section:

Adding a stamp to PDFs using Javascript

Try our creating stamp annotations guide if you're looking specifically for stamping annotations.

A stamp in a PDF document is analogous to applying a rubber stamp on a paper document.

Apryse SDK benefits include:

  • Stamp PDF pages with text, images, or with other PDF pages.
  • Embed fonts and images, and copy graphical elements from one page to another.

Stamp content (text, image) with canvas

The setWatermark method enables you to draw custom content on a page as a bitmap.

To preserve vector data use the stamper tool.

1const { documentViewer } = instance.Core;
2const path = '/samples/full-apis/TestFiles/butterfly.png'
3
4// Promise resolves with options object
5const promise = new Promise(resolve => {
6 const img = new Image();
7 const options = {
8 custom: async (ctx, pageIndex, pageWidth, pageHeight) => {
9
10 ctx.font = '50px serif';
11 ctx.fillText('Hello world', 50, 90);
12
13 ctx.drawImage(
14 img,
15 pageWidth / 2 - img.width / 2,
16 pageHeight / 2 - img.height / 2
17 );
18
19 // the pageIndex is also passed in so you could have
20 // a different watermark for each page
21 }
22 };
23 img.onload = () => {
24 return resolve(options);
25 };
26 img.src = path;
27});
28
29documentViewer.setWatermark(promise);

Stamp content (text, image, PDF page) with Stamper tool

To stamp text, image, and a PDF page to a PDF document.

1let imageName = 'path to your image file';
2let fileName = 'path to your PDF file';
3
4const { documentViewer, PDFNet } = instance.Core;
5
6document.getElementById('myBtn').addEventListener('click', async () => {
7 await PDFNet.initialize();
8 const doc = await documentViewer.getDocument().getPDFDoc();
9
10 // Run PDFNet methods with memory management
11 await PDFNet.runWithCleanup(async () => {
12
13 // lock the document before a write operation
14 // runWithCleanup will auto unlock when complete
15 doc.lock();
16 const s = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
17
18 // Specifies if the stamp is to be stamped as an annotation.
19 // note that stamps created with this setting do not work with SetAsBackground, HasStamps, and DeleteStamps, if annotation is true.
20 //s.setAsAnnotation(true);
21
22 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
23 const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_courier);
24 await s.setFont(font);
25 const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
26 await s.setFontColor(redColorPt);
27 await s.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_right);
28 await s.setAsBackground(false);
29 const pgSet = await PDFNet.PageSet.createRange(1, 2);
30 await s.stampText(doc, 'This is a title!', pgSet);
31
32 const img = await PDFNet.Image.createFromURL(doc, imageName);
33 s.setAsBackground(false);
34 const pgSetImage = await PDFNet.PageSet.createRange(1, 1);
35 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
36 await s.stampImage(doc, img, pgSetImage);
37
38 const srcDoc = await PDFNet.PDFDoc.createFromURL(fileName);
39 const srcPage = await srcDoc.getPage(1);
40 await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
41 await s.stampPage(doc, srcPage, pgSet);
42 });
43
44 // clear the cache (rendered) data with the newly updated document
45 documentViewer.refreshAll();
46
47 // Update viewer to render with the new document
48 documentViewer.updateView();
49
50 // Refresh searchable and selectable text data with the new document
51 documentViewer.getDocument().refreshTextData();
52});

Stamp a PDF File
Full code sample which shows how to stamp PDF pages with text, images, or with other PDF pages and how to add new content (or watermark).

About Stamper

Stamper can be used for PDF pages with text, images, or with other PDF content in only a few lines of code. Although Stamper is very simple to use compared to ElementBuilder/ElementWriter it is not as powerful or flexible. In case you need full control over PDF creation use ElementBuilder/ElementWriter to add new content to existing PDF pages as shown in the ElementBuilder sample project.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales