Skip to main content

Creating PDF Documents | Android Document Scanner

Since version 1.39.0 the Scanbot SDK for Android provides a simple and convenient API (PDFRenderer) to create PDF document files. The PDFRenderer supports multi-page PDFs where each given image will be stored as a PDF page. Furthermore, you can specify the physical output page size, orientation and PDF metadata.

Get an instance of PDFRenderer from ScanbotSDK:

import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.process.PDFRenderer

val pdfRenderer: PDFRenderer = ScanbotSDK(context).createPdfRenderer()

Then you can create a PDF file from Page objects stored by our RTU UI components (e.g. the result from the DocumentScannerActivity):

val pages: List<Page> = ...

val pdfConfig = PdfConfig(
pdfAttributes = PdfAttributes(
author = "Your author",
creator = "Your creator",
title = "Your title",
subject = "Your subject",
keywords = "Your keywords"
),
pageSize = PageSize.A4,
pageDirection = PageDirection.AUTO,
pageFit = PageFit.FIT_IN,
dpi = 72,
jpegQuality = 80,
resample = false
)

val pdfFile: File? = pdfRenderer.renderDocumentFromPages(pages, pdfConfig)
info

The PDF Renderer uses the document image (cropped image) of a Page object. So make sure all Page objects contain document images.

Alternatively, create a PDF file from arbitrary image files (JPG or PNG) provided as file URIs:

val imageFileUris: List<Uri> = // ["file:///some/path/file1.jpg", "file:///some/path/file2.jpg", ...]
val pdfConfig = PdfConfig.defaultConfig()
val pdfFile: File? = pdfRenderer.renderDocumentFromImages(imageFileUris, false, pdfConfig)

You can omit the PdfConfig parameter to use the default PDF settings. In this case PdfConfig.defaultConfig() will be used. It has empty PdfAttributes, PageSize.CUSTOM as page size and PageDirection.AUTO as the default page orientation.

PdfAttributes are used to set the PDF metadata. You can set the following attributes (all of which are optional):

  • author
  • creator
  • title
  • subject
  • keywords

PageSize can be one of the following:

  • PageSize.LETTER - represents 8.5 x 11 (inches) page size. The image is fitted and centered within the page.
  • PageSize.LEGAL - represents 8.5 x 14 (inches) page size. The image is fitted and centered within the page.
  • PageSize.A3 - represents 297 x 420 (mm) page size. The image is fitted and centered within the page.
  • PageSize.A4 - represents 210 x 297 (mm) page size. The image is fitted and centered within the page.
  • PageSize.A5 - represents 148 x 210 (mm) page size. The image is fitted and centered within the page.
  • PageSize.B4 - represents 250 x 353 (mm) page size. The image is fitted and centered within the page.
  • PageSize.B5 - represents 176 x 250 (mm) page size. The image is fitted and centered within the page.
  • PageSize.EXECUTIVE - represents 7.25 x 10.5 (inches) page size. The image is fitted and centered within the page.
  • PageSize.US4x6 - represents 4 x 6 (inches) page size. The image is fitted and centered within the page.
  • PageSize.US4x8 - represents 4 x 8 (inches) page size. The image is fitted and centered within the page.
  • PageSize.US5x7 - represents 5 x 7 (inches) page size. The image is fitted and centered within the page.
  • PageSize.COMM10 - represents 4.125 x 9.5 (inches) page size. The image is fitted and centered within the page.
  • PageSize.CUSTOM - represents a custom page size. Each page is as large as its image at 72 dpi.

PageDirection can be one of the following:

  • PageDirection.AUTO - page orientation will be detected automatically. Whether the orientation is portrait or landscape depends on the image's aspect ratio.
  • PageDirection.PORTRAIT - page orientation will be set to portrait.
  • PageDirection.LANDSCAPE - page orientation will be set to landscape.

PageFit can be one of the following:

  • PageFit.FIT_IN - fit image into page, preserves aspect ratio.
  • PageFit.FILL_IN - fill page with image, preserves aspect ratio.
  • PageFit.STRETCH - stretch image to fill page, does NOT preserve aspect ratio.
  • PageFit.NONE - no resizing, centers the image.

dpi - the dpi parameter has two different meanings depending on the value of pageSize and pageFit. If pageSize is CUSTOM or pageFit is NONE, then dpi is the conversion ratio used to convert from units of pixels to physical inches when adding bitmap images (JPEG, PNG, or raw) to the PDF. Otherwise, if resample is true, then the image is downscaled if necessary (if the image after being fit to the page has a higher calculated DPI than dpi) to match dpi before adding it to the PDF. Otherwise, the setting is ignored and the calculated image DPI is used instead.

jpegQuality - JPEG quality for images. Applies if an image is added as a Bitmap and therefore needs to be encoded. Also applies if resample is true and the image being added needs to be downscaled. Otherwise, when adding JPEG files to the PDF, the files are copied directly into the PDF and not re-encoded, which is many times faster and also preserves the quality of the original.

resample - if false (works faster): always geometrically rescale the image to fit the page if necessary. If true (works slower): downscale the bitmap to match the dpi setting before adding it to the PDF if the calculated image DPI after stretching is greater than dpi.

Want to scan longer than one minute?

Generate your free "no-strings-attached" Trial License and properly test the Scanbot SDK.

Get your free Trial License

What do you think of this documentation?