How to Access Rubber Stamp through a custom function?

I’ve added two rubber stamps(screenshot 1 & 2), now I want to select them with a custom function, I’ve done similar for other tools like pen, eraser, highlighter(screenshot 3). But unable to figure it out for selecting a rubber stamp.


The following method is for selecting tools like pen, eraser, etc. And this is working fine, I want something similar to select rubber stamp.

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Saban,

You can follow the code from our RubberStampOverlay to get an idea of how to select a stamp and show a preview programatically:

This is a quick snippet of how you can achieve this:

  const { docViewer, annotManager } = instance;

  const stampTool = docViewer.getTool('AnnotationCreateRubberStamp')

  async function getStamps() {
    return await stampTool.getStandardStampAnnotations();
  }

  async function setStampTool() {
    docViewer.setToolMode(stampTool);
    // get the first default stamp APPROVED
    const defaultStamps = await getStamps();
    const approvedStamp = defaultStamps[0]
    await stampTool.setRubberStamp(approvedStamp, approvedStamp.stampText);
    stampTool.showPreview();
  }

When calling this function, the stamp tool will be selected and immediately show the preview for the stamp to be placed. Clicking on the page then places the stamp.

Let me know if this is what you had in mind.

Best Regards,
Armando Dorantes Bollain
Software Developer
PDFTron Systems, Inc.

Hi @Armando_Bollain , the above code works well for the custom images I’m adding in standardRubberStamps, I’ve two more queries

1- I can’t select the “Expired” or “Approved” stamps. It throws the following error (screenshot 1)
2- I want to add a standardRubberStamp with the design of “Expired” but I want the custom text inside it. How can I achieve it? I’m attaching the screenshot of what I want to achieve (screenshot 2)


Hi Saban,

There is actually a parameter missing in the documentation for setRubberStamp; if you notice on my snippet it takes a second parameter with the text for the stamp. The error you are seeing is because this parameter is not being passed. The reason for this parameter is so that it is straightforward to switch languages.

This also makes it easy to achieve what you want for your second question; you can simply select the “Expired” stamp, and then pass a string with the number 30. You can clone this stamp as well and modify it. See snippet below:

  async function setStampTool() {
    docViewer.setToolMode(stampTool);
    const defaultStamps = await getStamps();
    console.log({ defaultStamps })
    // Select the expired stamp
    const expiredStamp = defaultStamps[7]
    await stampTool.setRubberStamp(expiredStamp. '30'); //Add the second parameter with text
    stampTool.showPreview();
  }

If you want further custom stamps you can refer to this guide.

I’ll fix the docs so it shows the second parameter for setRubberStamp.

Best Regards,
Armando Dorantes Bollain
Software Developer
PDFTron Systems, Inc.

Hey, @Armando_Bollain it worked perfectly, thanks for the help.

I’ve two more doubts regarding the same,

1- After adding the annotation, I’m fetching the complete annotation list using the below code, but how can I get the content inside the rubber stamp? In the following case number “30”. (screenshot 1)

let docViewer = this.instance.Core.documentViewer
const annotManager = docViewer.getAnnotationManager();
let annotationList = annotManager.getAnnotationsList()

2- Also if you see in screenshot 1, there’s a varibale ToolName, which has value ““AnnotationCreateRubberStamp”” , for my usecase I want to change it to “AnnotationCreateStamp”. Is it possible?

Thanks

Hi Saban,

For your first question, we have a getStampText and setStampText API, but it is not currently exposed. I have made a change to the code to expose this. This will be part of our next release.

For your second question, it is not possible to rename the tool. Moreover, this would be a collision as we already have a tool named AnnotationCreateStamp.

Best Regards,
Armando Dorantes Bollain
Software Developer
PDFTron Systems, Inc.