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 (e.g. A4, US Letter, Auto).

Get an instance of PDFRenderer from ScanbotSDK:

import io.scanbot.sdk.ScanbotSDKimport io.scanbot.sdk.process.PDFRenderer
val pdfRenderer: PDFRenderer = ScanbotSDK(context).pdfRenderer()

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

val pages: List<Page> = ...val pdfFile: File? = pdfRenderer.renderDocumentFromPages(pages, PDFPageSize.FIXED_A4)

Please note: 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 pdfFile: File? = pdfRenderer.renderDocumentFromImages(imageFileUris, false, PDFPageSize.FIXED_A4)

You can specify one of the following PDFPageSize as output size:

  • A4: The page has the aspect ratio of the image, but is fitted into A4 size. Whether portrait or landscape depends on the image's aspect ratio.
  • FIXED_A4: The page has A4 size. The image is fitted and centered within the page. Whether portrait or landscape depends on the image's aspect ratio.
  • US_LETTER: The page has the aspect ratio of the image, but is fitted into US letter size. Whether portrait or landscape depends on the image's aspect ratio.
  • FIXED_US_LETTER: The page has US letter size. The image is fitted and centered within the page. Whether portrait or landscape depends on the image's aspect ratio.
  • AUTO: For each page the best matching format (A4 or US letter) is used. Whether portrait or landscape depends on the image's aspect ratio.
  • AUTO_LOCALE: Each page of the result PDF will be of US letter or A4 size depending on the current locale. Whether portrait or landscape depends on the image's aspect ratio.
  • FROM_IMAGE: Each page is as large as its image at 72 dpi.