Custom Stamp as bar button

Please give a brief summary of your issue:

I am trying to create a custom stamp as an image. PDFTron has the PTCheckMarkStampCreate and the PTCrossMarkStampCreate. I essentially want to create my own and add them ad bar button items to the toolManager toolbar. If I could use those default ones and make them default to certain colors and sizes that would be great, but if not how would I create a custom stamp using an image and position it where ever the user taps?

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 there,
Thank you for getting in touch with us.
You can use the image-stamp tool to stamp an image onto the page: https://www.pdftron.com/api/ios/Classes/PTImageStampCreate.html
Does this approach work for you?

I’ve played around with that. If I wanted to create a custom bar button item that is selected and use it to stamp the document is that what I’d use?

let xMarkItem = PTSelectableBarButtonItem(image: image, style: .plain, target: self, action: #selector(customToolAction(_:)))

I am following your documentation here. In the selectors function call is there a method I can use to tell it to stamp anywhere the user taps?

And then how do I pass it to documentController.toolGroupManager.createItem(forToolClass: )

Here is an image that might make it more helpful with what I am trying to do. Currently I am using PDFTron system PTCheckMarkStampCreate and PTCrossMarkStampCreate to create a UIBarButtonItem by calling toolGroupManager.createItem(forToolClass: PTCrossMarkStampCreate.self)

How would I either override those to be my own custom image or how would I create my own stamp to add it to my toolGroupManager.annotateItemGroup .

I would like to make them look like this instead.
Screen Shot 2022-08-04 at 9.20.18 AM

Is there a way to override PTCheckMarkStampCreate and PTCrossMarkStampCreate too look like this? Or do you have example of how I can create a new stamp. Thank you

Hi Jack,

You can look at creating your own stamp by subclassing PTCreateToolBase class.

You can take help of this sample code:

class CheckMarkStampTool: PTCreateToolBase {
    override func pdfViewCtrl(_ pdfViewCtrl: PTPDFViewCtrl, handleTap gestureRecognizer: UITapGestureRecognizer) -> Bool {
        do {
            try pdfViewCtrl.docLock(true) { doc in
                guard let doc = doc else { return }
                
                let image = PTImage.create(doc.getSDFDoc(), filename: "<path-to-image>")
                let stamp: PTStamper = PTStamper(size_type: e_ptabsolute_size, a: 50, b: 50)
                stamp.setAlignment(e_pthorizontal_left, vertical_alignment: e_ptvertical_bottom)
                
                if let touchPtScreen = PTPDFPoint(px: self.endPoint.x, py: self.endPoint.y) {
                    let stampPoint = pdfViewCtrl.convScreenPt(toPagePt: touchPtScreen, page_num: self.pageNumber)
                    stamp.setPosition(stampPoint.getX(), vertical_distance: stampPoint.getY(), use_percentage: false)
                }
                
                let ps = PTPageSet(one_page: self.pageNumber)
                stamp.setAsAnnotation(true)
                stamp.stampImage(doc, src_img: image, dest_pages: ps)
                
                if let page = doc.getPage(UInt32(self.pageNumber)) {
                    let numAnnots = page.getNumAnnots()
                    if let newAnnot = page.getAnnot(numAnnots-1) {
                        pdfViewCtrl.update(with: newAnnot, page_num: self.pageNumber)
                    }
                }
            }
        } catch {
            print("error:\(error)")
        }
        return true
    }

Let us know if this works for you.

Best Regards,
Sahil Behl.

This was very helpful. Thanks @sbehl! I noticed that when I use an Apple Pencil that the new custom stamp is way off? Any ideas on why that might be? It works great without the Pencil but for some reason when I select my custom stamp it is far off where I actually want it.

Hi Jack,

It looks likes the [-PTCreateToolBase pdfViewCtrl:onTouchesMoved:withEvent: gets called when using the Pencil as it detects tiny bits of movement. This is internally modifying the tool’s self.endPoint property which is causing the annotation to be stamped at the wrong position.
it can be resolved by overriding onTouchesMoved without calling super:

override func pdfViewCtrl(_ pdfViewCtrl: PTPDFViewCtrl, onTouchesMoved touches: Set<UITouch>, with event: UIEvent?) -> Bool {
           // Don't call the super call's implementation.
           return true.
}

Please let us know if this works for you.

Best Regards,
Sahil Behl