Some test text!
Salesforce / Guides / Stamp content
A stamp in a PDF document is analogous to applying a rubber stamp on a paper document.
Apryse SDK benefits include:
The setWatermark
method enables you to draw custom content on a page as a bitmap.
To preserve vector data use the stamper tool.
const { documentViewer } = instance.Core;
const path = '/samples/full-apis/TestFiles/butterfly.png'
// Promise resolves with options object
const promise = new Promise(resolve => {
const img = new Image();
const options = {
custom: async (ctx, pageIndex, pageWidth, pageHeight) => {
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);
ctx.drawImage(
img,
pageWidth / 2 - img.width / 2,
pageHeight / 2 - img.height / 2
);
// the pageIndex is also passed in so you could have
// a different watermark for each page
}
};
img.onload = () => {
return resolve(options);
};
img.src = path;
});
documentViewer.setWatermark(promise);
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';
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();
});
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).
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 .
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales