> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/salesforce/content-edit/stamp.md).

# Adding a stamp to PDFs using Javascript

Learn how to create stamp annotations in PDF documents with our comprehensive guide. Stamp text, images, and PDF pages easily with Apryse SDK. Stamp your PDF files efficiently with our step-by-step in

{% hint style="warning" %}
Try our [creating stamp annotations](/web/get-started/guides/create-annotation-stamp.md) guide if you're looking specifically for stamping annotations.
{% endhint %}

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`](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#setWatermark__anchor) method enables you to draw custom content on a page as a bitmap.

To preserve vector data use the [stamper tool](https://docs.apryse.com).

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
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);
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.setWaterMark](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#setWatermark__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
const { docViewer } = instance;
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;
});

docViewer.setWatermark(promise);
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.setWaterMark](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#setWatermark__anchor)
{% endtab %}
{% endtabs %}

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

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

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
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();
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [PDFNet.Stamper.create](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#.create__anchor) [PDFDoc.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.Image.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.Image.html#.createFromURL__anchor) [PDFNet.Stamper.setSize](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setSize__anchor) [PDFNet.Stamper.setAlignment](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setAlignment__anchor) [PDFNet.Stamper.setFontColor](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setFontColor__anchor) [PDFNet.Stamper.setRotation](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setRotation__anchor) [PDFNet.Stamper.setAsAnnotation](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setAsAnnotation__anchor) [PDFNet.PageSet.createRange](https://sdk.apryse.com/api/web/Core.PDFNet.PageSet.html#.createRange__anchor) [PDFNet.Stamper.stampImage](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampImage__anchor) [PDFNet.Stamper.stampText](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampText__anchor) [PDFNet.Stamper.stampPage](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampPage__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
let imageName = 'path to your image file';
let fileName = 'path to your PDF file';

const { docViewer, PDFNet } = instance;

document.getElementById('myBtn').addEventListener('click', async () => {
  await PDFNet.initialize();
  const doc = await docViewer.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
  docViewer.refreshAll();

  // Update viewer to render with the new document
  docViewer.updateView();

  // Refresh searchable and selectable text data with the new document
  docViewer.getDocument().refreshTextData();
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [PDFNet.Stamper.create](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#.create__anchor) [PDFDoc.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.Image.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.Image.html#.createFromURL__anchor) [PDFNet.Stamper.setSize](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setSize__anchor) [PDFNet.Stamper.setAlignment](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setAlignment__anchor) [PDFNet.Stamper.setFontColor](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setFontColor__anchor) [PDFNet.Stamper.setRotation](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setRotation__anchor) [PDFNet.Stamper.setAsAnnotation](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#setAsAnnotation__anchor) [PDFNet.PageSet.createRange](https://sdk.apryse.com/api/web/Core.PDFNet.PageSet.html#.createRange__anchor) [PDFNet.Stamper.stampImage](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampImage__anchor) [PDFNet.Stamper.stampText](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampText__anchor) [PDFNet.Stamper.stampPage](https://sdk.apryse.com/api/web/Core.PDFNet.Stamper.html#stampPage__anchor)
{% endtab %}
{% endtabs %}

[Stamp a PDF File](/web/get-started/samples.md#stamper) 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](/web/get-started/samples.md#elementbuilder).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/salesforce/content-edit/stamp.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
