Create a page

There are two options to create a page. First is using a UI component that can create a new page or a new document. Second is an API guide to programmatically create a page.

Create document pages in Android

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

Apryse Docs Image

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

Page properties

The following properties are available for creating or adding pages:

  • PageType 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.
  • PageSize 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

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

Create 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.

1void addPagesToNewDocument(FragmentManager fragmentManager, final String folder) {
2 AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance();
3 addPageDialogFragment.setOnCreateNewDocumentListener(new AddPageDialogFragment.OnCreateNewDocumentListener() {
4 @Override
5 public void onCreateNewDocument(PDFDoc doc, String title) {
6 if (doc == null || title == null) {
7 return;
8 }
9 if (!FilenameUtils.isExtension(title, "pdf")) {
10 title = title + ".pdf";
11 }
12 File documentFile = new File(folder, title);
13 try {
14 SDFDoc.SaveMode saveModes[] = new SDFDoc.SaveMode[]{SDFDoc.SaveMode.REMOVE_UNUSED};
15 doc.save(documentFile.getAbsolutePath(), saveModes, null);
16 } catch (PDFNetException e) {
17 e.printStackTrace();
18 } finally {
19 try {
20 doc.close();
21 } catch (PDFNetException e) {
22 e.printStackTrace();
23 }
24 }
25 }
26 });
27 addPageDialogFragment.show(fragmentManager, "add_page_dialog");
28}

Add 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.

1void addPagesToCurrentDocument(final PDFViewCtrl pdfViewCtrl, final FragmentManager fragmentManager) throws Exception {
2 final WeakReference<PDFViewCtrl> pdfViewCtrlRef = new WeakReference<>(pdfViewCtrl);
3 pdfViewCtrl.docLockRead(new PDFViewCtrl.LockRunnable() {
4 @Override
5 public void run() throws Exception {
6 // enable user to add new pages with the same size as the last page of the current document
7 Page lastPage = pdfViewCtrl.getDoc().getPage(pdfViewCtrl.getDoc().getPageCount());
8 AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance(lastPage.getPageWidth(), lastPage.getPageHeight());
9 addPageDialogFragment.setOnAddNewPagesListener(new AddPageDialogFragment.OnAddNewPagesListener() {
10 @Override
11 public void onAddNewPages(final Page[] pages) {
12 final PDFViewCtrl pdfViewCtrl = pdfViewCtrlRef.get();
13 if (pages == null || pdfViewCtrl == null) {
14 return;
15 }
16 final PDFDoc doc = pdfViewCtrl.getDoc();
17 if (doc == null) {
18 return;
19 }
20
21 try {
22 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
23 @Override
24 public void run() throws Exception {
25 List<Integer> pageList = new ArrayList<>();
26 for (int i = 1, cnt = pages.length; i <= cnt; i++) {
27 int newPageNum = pdfViewCtrl.getCurrentPage() + i;
28 pageList.add(newPageNum);
29 doc.pageInsert(doc.getPageIterator(newPageNum), pages[i - 1]);
30 }
31
32 // To support undo/redo when a tool manager is attached to the PDFViewCtrl
33 ToolManager toolManager = (ToolManager) pdfViewCtrl.getToolManager();
34 if (toolManager != null) {
35 toolManager.raisePagesAdded(pageList);
36 }
37 pdfViewCtrl.updatePageLayout();
38 }
39 });
40 } catch (Exception e) {
41 e.printStackTrace();
42 }
43 }
44 });
45 addPageDialogFragment.show(fragmentManager, "add_page_dialog");
46 }
47 });
48}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales