Document Creation

PSPDFKit can create new PDF documents or single PDF pages from various sources such as images or existing PDF patterns. This feature requires the Document Editor component.

The Document Editor component already offers a convenient UI for inserting blank pages or pages with predefined patterns into existing documents. This is perfect for when you need new pages for more scratch space for drawing or adding textual notes.

Info: This guide describes how to add pages to existing documents and how to create new documents from scratch. If you are interested in additional functions of the PdfProcessor, consult our Document Processing guide or take a look at the DocumentProcessingExample inside the Catalog app.

Adding Pages to Existing Documents

To create a new page in an existing document, first open the PdfDocument and then use the PdfProcessor to insert a new page:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)

    // The third page is cloned from the last page of the document and rotated by 90 degrees.
    task.addNewPage(
        NewPage.fromPage(document, document.pageCount - 1).rotation(90).build(),
        2)

    // Start document processing.
    PdfProcessor.processDocument(task, outputFile)
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);

    // The third page is cloned from the last page of the document and rotated by 90 degrees.
    task.addNewPage(
        NewPage.fromPage(document, document.getPageCount() - 1)
            .rotation(90)
            .build(),
        2);

    // Start document processing.
    PdfProcessor.processDocument(task, outputFile);
}

Creating New Documents

The same approach (i.e. using the PdfProcessor) can be used for creating documents from scratch. Instead of creating the PdfProcessorTask with a source document, use the constructor, taking a NewPage object. After processing, the used outputFile will contain the fresh document:

Copy
1
2
3
4
5
6
7
8
9
private fun createNewDocument() {
    val outputFile = context.filesDir.resolve("new-document.pdf")

    // Create a new processor task, passing in a new page definition. This can also define colors, images, or pages of other documents.
    val task = PdfProcessorTask.newPage(NewPage.patternPage(NewPage.PAGE_SIZE_A4, PagePattern.LINES_7MM).build())

    // Start document processing, creating a blank file.
    PdfProcessor.processDocument(task, outputFile)
}
Copy
1
2
3
4
5
6
7
8
9
private void createNewDocument() {
    final File outputFile = new File(context.getFilesDir(), "new-document.pdf");

    // Create a new processor task, passing in a new page definition. This can also define colors, images, or pages of other documents.
    final PdfProcessorTask task = PdfProcessorTask.newPage(NewPage.patternPage(NewPage.PAGE_SIZE_A4, PagePattern.LINES_7MM).build());

    // Start document processing, creating a blank file.
    PdfProcessor.processDocument(task, outputFile);
}

Adding a New 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:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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, new NewPage.OnDrawCanvasCallback() {
        @Override
        public void onDrawCanvas(Canvas 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 the DocumentFromCanvasExample in the Catalog app. This showcases how to create a document from scratch by drawing on canvas.