Annotation Modification

Hi Team

I am using PdfViewCtrlTabHostFragment2 to render document. I want to detect modification of annotation so i am using ToolManager.AnnotationModificationListener but unable to implement properly. When i am add, edit,remove annotation then these methods are not calling. Please help me to solve this issue or please provide me any sample if available.

Ex- mPdfViewCtrlTabHostFragment?.addOnPreBuildToolbarListener(object :
ToolManager.AnnotationModificationListener,
PdfViewCtrlTabHostFragment2.OnPreBuildToolbarListener {

        override fun onPreBuildToolbar(builder: AnnotationToolbarBuilder?) {}

        override fun onAnnotationsAdded(annots: MutableMap<Annot, Int>?) {}

        override fun onAnnotationsPreModify(annots: MutableMap<Annot, Int>?) { }

        override fun onAnnotationsModified(annots: MutableMap<Annot, Int>?, extra: Bundle?) { }

        override fun onAnnotationsPreRemove(annots: MutableMap<Annot, Int>?) { }

        override fun onAnnotationsRemoved(annots: MutableMap<Annot, Int>?) { }

        override fun onAnnotationsRemovedOnPage(pageNum: Int) { }

        override fun annotationsCouldNotBeAdded(errorMessage: String?) {}
    }
    )

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:

How can we create an object of the toolbar when we are using PdfViewCtrlTabHostFragment2 instead of PDFViewCtrl.

Ex:- val mToolManager = ToolManagerBuilder.from() .build(getActivity(), pdfViewCtrl).

Hello,

Thanks for contacting us! Could you try the following sample code to add the AnnotationModificationListener?

        val toolManager: ToolManager =
            mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment().getToolManager()
        toolManager.addAnnotationModificationListener(object : AnnotationModificationListener {
            override fun onAnnotationsAdded(annots: Map<Annot, Int>) {}
            override fun onAnnotationsPreModify(annots: Map<Annot, Int>) {}
            override fun onAnnotationsModified(annots: Map<Annot, Int>, extra: Bundle) {}
            override fun onAnnotationsPreRemove(annots: Map<Annot, Int>) {}
            override fun onAnnotationsRemoved(annots: Map<Annot, Int>) {}
            override fun onAnnotationsRemovedOnPage(pageNum: Int) {}
            override fun annotationsCouldNotBeAdded(errorMessage: String) {}
        })
        mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment().getPDFViewCtrl()
            .setToolManager(toolManager)

Thank you,
Andrew

Hi Andrew

Thanks for the answer.

Andrew, I tried this sample but this method mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment() throw nullpointer exception.

Thanks-
Pawan

Hi Pawan,

You need to call this method after document is loaded.
See API:

See sample:

Also, it looks like there is an error from the our last post, you don’t actually set ToolManager, you will just need to getToolManager from the fragment: PdfViewCtrlTabBaseFragment - PDFTron API Reference | PDFTron Systems Inc.

        val toolManager =
            mPdfViewCtrlTabHostFragment.getCurrentPdfViewCtrlFragment().getToolManager()
        toolManager.addAnnotationModificationListener(object : AnnotationModificationListener {
            override fun onAnnotationsAdded(annots: Map<Annot, Int>) {}
            override fun onAnnotationsPreModify(annots: Map<Annot, Int>) {}
            override fun onAnnotationsModified(annots: Map<Annot, Int>, extra: Bundle) {}
            override fun onAnnotationsPreRemove(annots: Map<Annot, Int>) {}
            override fun onAnnotationsRemoved(annots: Map<Annot, Int>) {}
            override fun onAnnotationsRemovedOnPage(pageNum: Int) {}
            override fun annotationsCouldNotBeAdded(errorMessage: String) {}
        })

Thank Shirley

Shirley same as I tried AnnotationSyncingListener in onTabDocumentLoaded method.
Now I am getting a issue after using of annotation presetbar is disappearing and got this line in logcat - “PDFNet: Incremental save is not supported in admin-undo-own collaboration mode”.

Code -

override fun onTabDocumentLoaded(p0: String?) {

mPdfViewCtrlTabHostFragment.currentPdfViewCtrlFragment.toolManager.enableAnnotManager(
“12345-67890-ABCD-MuuEN9IEDZK_tIFkvKL”,
AnnotSyncingListner()
)
}

class AnnotSyncingListner : AnnotationSyncingListener {

override fun onLocalChange(action: String?, xfdfCommand: String?, xfdfJSON: String?) {
    Log.e(
        "Annot_Sync",
        "Action--->$action\n xfdfCommand---> $xfdfCommand\n xfdfJSON--->   $xfdfJSON"
    )
}

}


Hi Pawan,
Firstly, using annotation manager means you are saving XFDF separately from the document, so you should turn off saving setSavingEnabled(false):

Second, if you are using annotation manager, then you likely don’t want to also use add/modify/delete callbacks. If you need to use both, please kindly explain your use case and your user story.

Hi Shirley

As per my user story, Whenever I am performing an operation for add/modify/delete . I need to save xfdf command on our backend.

Hi Pawan,

Sounds good, so you should be able to disable saving, and use “onLocalChange” callback to achieve that without any of the add/modify/delete event. Are you still having any issue with this?

Hi Shirley

I am using PdfViewCtrlTabHostFragment2 and setSavingEnabled(false) method available in PdfViewCtrlTabBaseFragment. How we can use setSavingEnabled(false) with PdfViewCtrlTabHostFragment2 or any other option ?

Hi Pawan,

Typically you want to put it in the host callback (same one I linked before that will get you the document loaded event:

        override fun onTabHostShown() {
            mPdfViewCtrlTabHostFragment?.currentPdfViewCtrlFragment?.setSavingEnabled(false)
        }
        override fun onTabChanged(p0: String?) {
            mPdfViewCtrlTabHostFragment?.currentPdfViewCtrlFragment?.setSavingEnabled(false)
        }

From the host fragment, you have full access to the tab fragment, PDFViewCtrl, the document (PDFDoc) and the ToolManager.

Thanks.

Hi Shirley

Thank you so much.

Now it’s working fine.

Could you please give me one more favor.

How can we render the annotation?

Hi Pawan, could you elaborate on your goal? PDFTron renders annotations by default. Do you mean create them programmatically? What exactly are you looking to do? Thanks.

As per my user story, When I am creating an annotation I am sending xfdf string of that annotation on our backend. Whenever I will open that document again or login with another device or platform(Android/iOS) and open the same document.
I have to render that annotation from the backend(xfdf string) page by page. So my question is how can i render annoation with xfdf string after open the document?

To render XFDF from remote, please import them into the document, as you are using AnnotationManager, you can follow guide here: PDFTron

It’s working.

Thank you a lot, Shirley.