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, PDF page) with Stamper tool
Make sure you have Full API enabled in WebViewer.
To stamp text, image, and a PDF page to a PDF document.
let imageName = 'path to your image file';
let fileName = 'path to your PDF file';
WebViewer({
fullAPI: true,
// other constructor options
}, viewerElement).then(instance => {
const { documentViewer, PDFNet } = instance.Core;
document.getElementById('myBtn').addEventListener('click', async () => {
await PDFNet.initialize();
const doc = await documentViewer.getDocument().getPDFDoc();
// Run PDFNet methods with memory management
await PDFNet.runWithCleanup(async () => {
// lock the document before a write operation
// runWithCleanup will auto unlock when complete
doc.lock();
const s = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5);
// Specifies if the stamp is to be stamped as an annotation.
// note that stamps created with this setting do not work with SetAsBackground, HasStamps, and DeleteStamps, if annotation is true.
//s.setAsAnnotation(true);
await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_top);
const font = await PDFNet.Font.create(doc, PDFNet.Font.StandardType1Font.e_courier);
await s.setFont(font);
const redColorPt = await PDFNet.ColorPt.init(1, 0, 0, 0);
await s.setFontColor(redColorPt);
await s.setTextAlignment(PDFNet.Stamper.TextAlignment.e_align_right);
await s.setAsBackground(false);
const pgSet = await PDFNet.PageSet.createRange(1, 2);
await s.stampText(doc, 'This is a title!', pgSet);
const img = await PDFNet.Image.createFromURL(doc, imageName);
s.setAsBackground(false);
const pgSetImage = await PDFNet.PageSet.createRange(1, 1);
await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_right, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
await s.stampImage(doc, img, pgSetImage);
const srcDoc = await PDFNet.PDFDoc.createFromURL(fileName);
const srcPage = await srcDoc.getPage(1);
await s.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom);
await s.stampPage(doc, srcPage, pgSet);
});
// clear the cache (rendered) data with the newly updated document
documentViewer.refreshAll();
// Update viewer to render with the new document
documentViewer.updateView();
// Refresh searchable and selectable text data with the new document
documentViewer.getDocument().refreshTextData();
});
})WebViewerInstance PDFNet.Stamper.create PDFDoc.createFromURL PDFNet.Image.createFromURL PDFNet.Stamper.setSize PDFNet.Stamper.setAlignment PDFNet.Stamper.setFontColor PDFNet.Stamper.setRotation PDFNet.Stamper.setAsAnnotation PDFNet.PageSet.createRange PDFNet.Stamper.stampImage PDFNet.Stamper.stampText PDFNet.Stamper.stampPage
WebViewerInstance PDFNet.Stamper.create PDFDoc.createFromURL PDFNet.Image.createFromURL PDFNet.Stamper.setSize PDFNet.Stamper.setAlignment PDFNet.Stamper.setFontColor PDFNet.Stamper.setRotation PDFNet.Stamper.setAsAnnotation PDFNet.PageSet.createRange PDFNet.Stamper.stampImage PDFNet.Stamper.stampText PDFNet.Stamper.stampPage
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.
Last updated
Was this helpful?