Generate PDFs from a template on Android
Nutrient Android SDK enables you to create a new PDF document from a template. Our SDK ships with a predefined list of page patterns.
PagePattern Constant |
|
---|---|
Blank | BLANK |
Dots 5mm | DOTS_5MM |
Grid 5mm | GRID_5MM |
Line 5mm | LINES_5MM |
Line 7mm | LINES_7MM |
In addition to these built-in patterns, you can use the PagePattern
constructors, which can take either a DataProvider
or a file Uri
to build customized page templates from your own PDF documents.
ℹ️ Note: This feature requires the Document Editor component to be enabled in your license.
The PageTemplate class
PageTemplate
has two constructors you can use depending on what you’re trying to achieve:
-
PageTemplate(PdfDocument sourceDocument, int pageIndex, String templateName, Drawable previewImage)
will instantiate a template that takes the source document’s page at the provided index. -
PageTemplate(PagePattern pagePattern, String templateName, Drawable previewImage)
will instantiate a template that’s intended to be used as a tiled pattern. If you want to add a page with a repeating pattern to a document, this is the initializer you’ll use.
ℹ️ Note: Creating a tiled pattern page template requires the source document to be exported correctly. This means that the source PDF needs to contain a pattern itself. If a
PageTemplate
is instantiated using the tiled pattern initializer and the source document doesn’t contain a pattern, the rendering will fail silently.
Check out CustomPageTemplatesExample.java
in the Catalog app to see PageTemplate
in action.
Custom tiled templates
Many use cases for page templates require the background being tiled (or patterned).
A page is considered tiled if there are one or more images repeated on the page.
For a PDF to be able to work as a source for a tiled page template using PageTemplate(PagePattern pagePattern, String templateName, Drawable previewImage)
, it has to have actual pattern path information embedded.
To accomplish this, you can use Adobe Illustrator or any other vector editing tool.
When creating your own patterns, consider the following points:
-
What’s rendered on the page is the path information embedded in the PDF and not the actual PDF.
-
If your custom pattern needs certain spacing between tiles, that information needs to be included within the pattern information as well. Currently, there’s no way to specify spacing between tiles from the Nutrient API.
Click here to download a custom sample template.
Using PageTemplate
Create a NewPage
object using the emptyPage
method of its builder. The NewPage.Builder
type allows you to customize various properties of the page, such as the background color or page size:
// Create a configuration for an empty A4 size page with a white background color. val newPage = NewPage .emptyPage(NewPage.PAGE_SIZE_A4) .backgroundColor(Color.WHITE) .build()
// Create a configuration for an empty A4 size page with a white background color. final NewPage newPage = NewPage .emptyPage(NewPage.PAGE_SIZE_A4) .backgroundColor(Color.WHITE) .build();
Using PdfProcessor
The PdfProcessor
class allows you to use PdfProcessorTask
s to perform many types of operations on PDF documents. These operations include document creation, merging, and modification.
After configuring a page as described in the first section, you can create a PdfProcessor
and add the NewPage
to it. Feeding that task into the PdfProcessor
will then generate the actual PDF.
The example below shows how to generate a PDF using the PdfProcessor
API:
val outputFile: File = ... val pdfProcessorTask = PdfProcessorTask.newPage(newPage) val disposable = PdfProcessor.processDocumentAsync(pdfProcessorTask, outputFile) .subscribe { progress -> }
final File outputFile = ... final PdfProcessorTask pdfProcessorTask = PdfProcessorTask.newPage(newPage); final Disposable disposable = PdfProcessor.processDocumentAsync(pdfProcessorTask, outputFile) .subscribe(progress -> { });