Search code examples
androidpspdfkit

How to display Custom Dialogfragment when creating PDF Annotations with PSPDFKit


I am investigating the PDF library PSPDFKit for Android.

// Inside my app's dependencies {}
implementation 'com.pspdfkit:pspdfkit-demo:4.0.2'

The library is impressive and has many valuable features "Out Of The Box". However, I have encountered an issue with the Dialog Fragment that displays when creating Note Annotations.

I need to have a customized dialog UI as the PDFs my application displays allow multiple users to add/edit annotations. This requires the dialog that is displayed when editing annotations to contain the user profile image and full name that created the annotation.

I am using com.pspdfkit.ui.PdfFragment as a child fragment in my application and cannot see any way that allows me to supply a custom dialogFragment when creating and/or editing Note annotations.

Is it possible to customise the dialog fragment displayed when creating/editing a Note fragment in PSPDFKit?

From what I can see of the source code, it's this method I need to override within PSPDFKit.

public void enterAnnotationCreationMode(@NonNull final AnnotationTool annotationTool) {
        this.viewCoordinator.a(new b() {
            public void run(@NonNull FrameLayout container, @NonNull PdfPasswordView passwordView, @NonNull View errorView, @NonNull final DocumentView documentView) {
                el var10000 = com.pspdfkit.framework.a.d();
                PdfFragment.this.getContext();
                if(var10000.a(PdfFragment.this.configuration)) {
                    if(!PdfFragment.this.getAnnotationPreferences().isAnnotationCreatorSet()) {
                        AnnotationCreatorInputDialogFragment.show(PdfFragment.this.getActivity().getSupportFragmentManager(), (String)null, new OnAnnotationCreatorSetListener() {
                            public void onAnnotationCreatorSet(String annotationCreator) {
                                documentView.enterAnnotationCreationMode(annotationTool);
                            }

                            public void onAbort() {
                            }
                        });
                        com.pspdfkit.framework.a.f().a("show_annotation_creator_dialog").a();
                    } else {
                        documentView.enterAnnotationCreationMode(annotationTool);
                    }
                } else {
                    throw new PSPDFKitException("Entering annotation creation mode for " + annotationTool + " is not permitted, either by the license or configuration.");
                }
            }
        }, true);
    }

Solution

  • As of PSPDFKit 4.0.2 there is no way to replace the note annotation editor UI. In general, it is a good idea to directly contact the PSPDFKit support for missing features at https://pspdfkit.com/support/request/

    However, this is already tracked by the PSPDFKit team and will most likely be available in future versions.

    As a workaround: You could implement your custom note action and add it to the AnnotationCreationToolbar. There's a guide article on how to modify the actions on the toolbar using the toolbar grouping API. This can be used to also remove existing actions and add new ones.

    Using a OnContextualToolbarLifecycleListener you can register your custom filter logic, that removes the default note action and replaces it with your custom logic (that will then display your custom dialog).

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            toolbarCoordinatorLayout.setOnContextualToolbarLifecycleListener(this);
        }
    
        @Override
        public void onPrepareContextualToolbar(@NonNull ContextualToolbar toolbar) {
            if (toolbar instanceof AnnotationCreationToolbar) {
                toolbar.setMenuItemGroupingRule(new CustomNoteAnnotationActionRule(this));
            }
        }
    

    You can then register whenever your custom note icon is pressed to switch into "note creation mode". Whenever DocumentListener#onPageClick() is called then, you could show your custom annotation creation UI.