Add page dialog

In this document
chevron_rightPage options
chevron_rightpage type (
chevron_rightpage size (
chevron_rightpage color (

With AddPageDialogFragment users are able to add new pages to an existing document, or to create a new document with new pages. The new pages can have various types, sizes and colors.

Add page dialog from left to right: dialog for creating new document, dialog for adding pages to an existing document.

Page options

The following options are available for creating pages:

page type (PageType)

Currently, the following types of page are supported in this dialog:

  • Blank: a page with nothing on it.
  • Lined: a page with horizontal lines on it.
  • Grid: a page with a superimposed grid on it.
  • Graph: a page with Cartesian axes on it.
  • Music: a page set up with modern staff notation for notating music.

page size (PageSize)

Currently, the following page sizes are supported in this dialog: Custom, Letter, Legal, A4, A3, Ledger.

The Custom option is only available when adding new pages to an existing PDF document, not when a new document is being created. The page size of Custom option is specified as here: add pages to an existing document

page color (PageColor)

The page background can be set to the following colors: White, Yellow, and Blueprint.

Creating a new PDF document

To create a new PDF document, call newInstance() and override the OnCreateNewDocumentListener interface. The implementation of onCreateNewDocument(PDFDoc, String) should create a new file with the given title normalized to a ".pdf" extension.

void addPagesToNewDocument(FragmentManager fragmentManager, final String folder) {
    AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance();
    addPageDialogFragment.setOnCreateNewPageListener(new AddPageDialogFragment.OnCreateNewDocumentListener() {
            @Override
            public void onCreateNewDocument(PDFDoc doc, String title) {
                if (doc == null || title == null) {
                    return;
                }
                if (!FilenameUtils.isExtension(title, "pdf")) {
                    title = title + ".pdf";
                }
                File documentFile = new File(folder, title);
                try {
                    SDFDoc.SaveMode saveModes[] = new SDFDoc.SaveMode[]{SDFDoc.SaveMode.REMOVE_UNUSED};
                    doc.save(documentFile.getAbsolutePath(), saveModes, null);
                } catch (PDFNetException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        doc.close();
                    } catch (PDFNetException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    addPageDialogFragment.show(fragmentManager, "add_page_dialog");
}

Adding pages to an existing document

To add pages to an existing PDF, create a new instance of the add page dialog fragment using newInstance(double, double) and provide the valid page width and page height arguments.These arguments will be used only if the user selects the Custom option in the Page Size dropdown. You also must implement the OnAddNewPagesListener interface and override onAddNewPages(Page[]), in which the implementation should add the provided pages to the document.

void addPagesToCurrentDocument(FragmentManager fragmentManager, PDFViewCtrl pdfViewCtrl) {
    boolean shouldUnlockRead = false;
    final WeakReference<PDFViewCtrl> pdfViewCtrlRef = new WeakReference<>(pdfViewCtrl);
    try {
        pdfViewCtrl.docLockRead();
        shouldUnlockRead = true;
        // enable user to add new pages with the same size as the last page of the current document
        Page lastPage = pdfViewCtrl.getDoc().getPage(pdfViewCtrl.getDoc().getPageCount());
        AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance(lastPage.getPageWidth(), lastPage.getPageHeight());
        addPageDialogFragment.setOnAddNewPagesListener(new AddPageDialogFragment.OnAddNewPagesListener() {
                @Override
                public void onAddNewPages(Page[] pages) {
                    PDFViewCtrl pdfViewCtrl = pdfViewCtrlRef.get();
                    if (pages == null || pdfViewCtrl == null) {
                        return;
                    }
                    PDFDoc doc = pdfViewCtrl.getDoc();
                    if (doc == null) {
                        return;
                    }

                    boolean shouldUnlock = false;
                    try {
                        pdfViewCtrl.docLock(true);
                        shouldUnlock = true;
                        List<Integer> pageList = new ArrayList<>();
                        for (int i = 1, cnt = pages.length; i <= cnt; i++) {
                            int newPageNum = pdfViewCtrl.getCurrentPage() + i;
                            pageList.add(newPageNum);
                            doc.pageInsert(doc.getPageIterator(newPageNum), pages[i - 1]);
                        }

                        // To support undo/redo when a tool manager is attached to the PDFViewCtrl
                        ToolManager toolManager = (ToolManager) pdfViewCtrl.getToolManager();
                        if (toolManager != null) {
                            toolManager.raisePagesAdded(pageList);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (shouldUnlock) {
                            pdfViewCtrl.docUnlock();
                        }
                        try {
                            pdfViewCtrl.updatePageLayout();
                        } catch (PDFNetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        addPageDialogFragment.show(fragmentManager, "add_page_dialog");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlockRead) {
            pdfViewCtrl.docUnlockRead();
        }
    }
}