Add Pages to PDF Files on Android
With PSPDFKit, there are four ways to add a page to a document:
-
Add a blank page programmatically
-
Add a new page programmatically from Android Canvas
-
Take a page from a different document and insert it as a new page programmatically
-
Interactively with the user interface
Adding a Blank Page
To create a new page in an existing document, first open the PdfDocument
and then use the PdfProcessor
to insert a new page:
private fun createDocumentWithNewPages(document: PdfDocument) { val outputFile = context.filesDir.resolve("${document.uid}-new-page.pdf") val task = PdfProcessorTask.fromDocument(document) // Create a yellow A5 page with a line pattern as the first page. task.addNewPage( NewPage.patternPage(NewPage.PAGE_SIZE_A5, PagePattern.LINES_7MM).backgroundColor(Color.rgb(241, 236, 121)).build(), 0) // Create an A0 page with an image as the second page. val bitmap = BitmapFactory.decodeStream(context.assets.open("inline-media/images/cover.jpg")) task.addNewPage( NewPage.emptyPage(NewPage.PAGE_SIZE_A0).withPageItem(PageImage(bitmap, PagePosition.CENTER)).build(), 1) // Start document processing. PdfProcessor.processDocument(task, outputFile) }
private void createDocumentWithNewPages(final @NonNull PdfDocument document) throws IOException { final File outputFile = new File(context.getFilesDir(), document.getUid() + "-new-page.pdf"); final PdfProcessorTask task = PdfProcessorTask.fromDocument(document); // Create a yellow A5 page with a line pattern as the first page. task.addNewPage( NewPage.patternPage(NewPage.PAGE_SIZE_A5, PagePattern.LINES_7MM) .backgroundColor(Color.rgb(241, 236, 121)) .build(), 0); // Create an A0 page with an image as the second page. final Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open("inline-media/images/cover.jpg")); task.addNewPage( NewPage.emptyPage(NewPage.PAGE_SIZE_A0) .withPageItem(new PageImage(bitmap, PagePosition.CENTER)) .build(), 1); // Start document processing. PdfProcessor.processDocument(task, outputFile); }
Adding a Page from Android Canvas
As of PSPDFKit 4.6 for Android, PdfProcessor
supports a new API for page creation, exposing a method with a callback, NewPage.Builder#fromCanvas(Context, Size, OnDrawCanvasCallback)
, which hands over the canvas used for drawing calls:
private fun createNewDocumentFromCanvas(context: Context) { val outputFile = File(context.getFilesDir(), "new-document.pdf") // Create a canvas based on an A4 page. val pageCanvas = NewPage.fromCanvas(context, NewPage.PAGE_SIZE_A4) { canvas -> val paint = Paint() paint.style = Paint.Style.STROKE val path = Path() path.cubicTo(0f, 0f, 100f, 300f, 400f, 300f) canvas.drawPath(path, paint) }.build() // Create a new processor task, passing in the new page definition. val task = PdfProcessorTask.newPage(pageCanvas) // Start document processing. PdfProcessor.processDocument(task, outputFile) }
private void createNewDocumentFromCanvas(Context context) { final File outputFile = new File(context.getFilesDir(), "new-document.pdf"); // Create a canvas based on an A4 page. final NewPage pageCanvas = NewPage.fromCanvas(context, NewPage.PAGE_SIZE_A4, canvas -> { Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); Path path = new Path(); path.cubicTo(0f, 0f, 100f, 300f, 400f, 300f); canvas.drawPath(path, paint); }).build(); // Create a new processor task, passing in the new page definition. final PdfProcessorTask task = PdfProcessorTask.newPage(pageCanvas); // Start document processing. PdfProcessor.processDocument(task, outputFile); }
💡 Tip: Check out
DocumentFromCanvasExample
in the Catalog app. This showcases how to create a document from scratch by drawing on canvas.
Inserting a Page from Another Document
PdfProcessor
also allows you to insert a single page into another document using NewPage.fromPage()
:
private fun createDocumentWithNewPages(document: PdfDocument) { val outputFile = context.filesDir.resolve("${document.uid}-new-page.pdf") val task = PdfProcessorTask.fromDocument(document) // The third page is cloned from the last page of the document. task.addNewPage( NewPage.fromPage(document, document.pageCount - 1).build(), 2) // Start document processing. PdfProcessor.processDocument(task, outputFile) }
private void createDocumentWithNewPages(final @NonNull PdfDocument document) throws IOException { final File outputFile = new File(context.getFilesDir(), document.getUid() + "-new-page.pdf"); final PdfProcessorTask task = PdfProcessorTask.fromDocument(document); // The third page is cloned from the last page of the document. task.addNewPage( NewPage.fromPage(document, document.getPageCount() - 1) .rotation(90) .build(), 2); // Start document processing. PdfProcessor.processDocument(task, outputFile); }
Using the User Interface
The Document Editor component offers a convenient UI for inserting blank pages or pages with predefined patterns into existing documents — this is perfect for adding new pages for more scratch space for drawing or adding textual notes.
While in editing mode, you can press the floating action button to add a new page. By default, tapping this will display the new page dialog, allowing you to configure the new page (various page patterns, background colors, page sizes, and landscape or portrait orientation).