PDF from Image
Shows how to capture an image and convert it to a PDF.
/* * Copyright © 2020-2026 PSPDFKit GmbH. All rights reserved. * * The PSPDFKit Sample applications are licensed with a modified BSD license. * Please see License for details. This notice may not be removed from this file. */
package com.pspdfkit.catalog.examples.kotlin
import android.app.Activityimport android.content.Contextimport android.content.Intentimport android.graphics.BitmapFactoryimport android.graphics.RectFimport android.net.Uriimport android.os.Bundleimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.processor.NewPageimport com.pspdfkit.document.processor.PageImageimport com.pspdfkit.document.processor.PdfProcessorimport com.pspdfkit.document.processor.PdfProcessorTaskimport com.pspdfkit.ui.PdfActivityimport com.pspdfkit.utils.Sizeimport com.pspdfkit.utils.getSupportParcelableExtra
class PdfFromImageExample(context: Context) : SdkExample(context, R.string.pdfFromImageExampleTitle, R.string.pdfFromImageExampleDescription) { override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { val intent = Intent(context, PdfFromImageActivity::class.java) intent.putExtra(PdfFromImageActivity.EXTRA_CONFIGURATION, configuration.build()) context.startActivity(intent) }}
class PdfFromImageActivity : Activity() {
private lateinit var configuration: PdfActivityConfiguration
private var waitingForResult = false
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
// Extract the configuration for displaying the viewer activity. configuration = intent.getSupportParcelableExtra(EXTRA_CONFIGURATION, PdfActivityConfiguration::class.java) ?: throw ExceptionInInitializerError( PdfFromImageActivity::class.java.simpleName + " was started without a PdfActivityConfiguration." )
// Prevent the example from requesting multiple documents at the same time. if (!waitingForResult) { waitingForResult = true startActivityForResult(getImagePickerIntent(), REQUEST_IMAGE) } }
private fun getImagePickerIntent(): Intent? { // Creates an intent that will open a file picker with the filter set to only open images. val intent = Intent() intent.type = "image/*" intent.action = Intent.ACTION_GET_CONTENT return if (intent.resolveActivity(packageManager) == null) null else Intent.createChooser(intent, "") }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { // Once the image was picked we are done with this activity, close it. finish()
if (requestCode == REQUEST_IMAGE && resultCode == RESULT_OK && data?.data != null) { // Grab the path to the selected image. val imageUri = data.data ?: return
// Create a PdfProcessorTask to create the new PDF. val task = createPdfFromImageTask(imageUri)
// Obtain a path to put the resulting file. // For simplicity we always put it in our application directory. val outputPath = filesDir.resolve("image.pdf")
// Process the document. PdfProcessor.processDocument(task, outputPath)
// And finally show it. PdfActivity.showDocument(this, Uri.fromFile(outputPath), configuration) } }
/** * This creates a [PdfProcessorTask] which will create a single page document using the supplied image as the page background. */ private fun createPdfFromImageTask(imageUri: Uri): PdfProcessorTask { // First obtain the size of the image. val options = BitmapFactory.Options().apply { // By setting this we won't actually load the image but only figure out the size. inJustDecodeBounds = true } BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri), null, options) val imageHeight: Int = options.outHeight val imageWidth: Int = options.outWidth
// We take A4 as a baseline, and alter the page's aspect ratio based on the given bitmap. val pageSize: Size = if (imageWidth <= imageHeight) { Size(NewPage.PAGE_SIZE_A4.width, imageHeight * (NewPage.PAGE_SIZE_A4.width / imageWidth)) } else { Size(NewPage.PAGE_SIZE_A4.height, imageHeight * NewPage.PAGE_SIZE_A4.height / imageWidth) }
// Now that we know the desired size we can create a PdfProcessorTask which will create a document containing a single page. return PdfProcessorTask.newPage( NewPage.emptyPage(pageSize) // We initialize our new page using the passed in image URI and calculated page size. .withPageItem(PageImage(this, imageUri, RectF(0f, pageSize.height, pageSize.width, 0f))) .build() ) }
companion object { internal const val EXTRA_CONFIGURATION = "Nutrient.PdfFromImageActivity.configuration"
private const val REQUEST_IMAGE = 1 }}This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.