Create pages (Android)

You can create pages using the add pages dialog or programmatically with Xamarin.Android

Add or create new document pages in Android

This tutorial only applies to Xamarin.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.

C#

1var addPageDialogFragment = AddPageDialogFragment.NewInstance();
2addPageDialogFragment.CreateNewDocument += (sender, e) =>
3{
4 if (e.PdfDoc == null || e.Title == null)
5 {
6 return;
7 }
8 var title = e.Title;
9 if (!title.EndsWith(".pdf", true, null))
10 {
11 title = title + ".pdf";
12 }
13 Java.IO.File folder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
14 Java.IO.File documentFile = new Java.IO.File(folder, title);
15 try
16 {
17 e.PdfDoc.Save(documentFile.AbsolutePath, (long)pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused, null);
18 Console.WriteLine("New file saved: " + documentFile.AbsolutePath);
19 }
20 catch (Exception ex)
21 {
22 Console.WriteLine(ex.Message);
23 }
24 finally
25 {
26 e.PdfDoc.Close();
27 }
28
29};
30addPageDialogFragment.Show(this.SupportFragmentManager, "add_page_dialog");

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.

C#

1var addPageDialogFragment = AddPageDialogFragment.NewInstance(myPageWidth, myPageHeight);
2addPageDialogFragment.AddNewPages += (sender, e) =>
3{
4 if (mPdfDoc == null || mPdfViewCtrl == null)
5 {
6 return;
7 }
8 bool shouldUnlock = false;
9 try
10 {
11 mPdfViewCtrl.DocLock(true);
12 shouldUnlock = true;
13
14 for (int i = 1, cnt = e.Pages.Length; i <= cnt; i++)
15 {
16 int newPageNum = mPdfViewCtrl.CurrentPage + i;
17 var pageNative = e.Pages[i - 1];
18 var page = TypeConvertHelper.ConvPageToManaged(pageNative);
19 mPdfDoc.PageInsert(mPdfDoc.GetPageIterator(newPageNum), page);
20 }
21 }
22 catch (PDFNetException ex)
23 {
24 Console.WriteLine(ex.GetMessage());
25 }
26 finally
27 {
28 if (shouldUnlock)
29 {
30 mPdfViewCtrl.DocUnlock();
31 }
32 }
33 mPdfViewCtrl.UpdatePageLayout();
34 mPdfViewCtrl.CurrentPage = mPdfViewCtrl.CurrentPage + 1;
35 if (mSeekBar != null)
36 {
37 mSeekBar.SetProgress(mPdfViewCtrl.CurrentPage);
38 }
39};
40addPageDialogFragment.Show(this.SupportFragmentManager, "add_page_dialog");

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales