user25198750
user25198750

Reputation: 1

Android App crashes when rendering PDF with Android Pdf Viewer

My app code is below.

pdfView.fromFile(File(pdfPath))
.pages(0)
.enableSwipe(false)
.swipeHorizontal(false)
.enableDoubletap(false)
.defaultPage(0)
.enableAnnotationRendering(true)
.password(null)
.scrollHandle(null)
.enableAntialiasing(false)
.onError(OnErrorListener {
    // error handling
})
.load()

It seems like there is some problem with the annotation because it doesn't crash when enableAnnotationRendering(false). Also, some PDFs crash and some don't. PDFs edited on Windows or Android do not crash, but PDFs edited on Mac or iPhone do crash. I suspect that rendering is failing due to missing fonts, but is there a possibility that the app will crash if there are no fonts?

Also, even if rendering fails, it cannot be caught with onError(). What can I do to prevent the app from crashing?

Thing you want to do

  1. Catch rendering failures with onError() or another method to avoid crashing the app.
  2. 1 is completed, and I want to change it to the device's default font if the font does not exist.

Upvotes: -1

Views: 143

Answers (1)

user25198750
user25198750

Reputation: 1

Using PDFBox (TomRoush), the crash no longer occurs using the following two methods.

The first method is to set AcroForm's needAppearance to false.
The modified code is below.

val document = PDDocument.load(File(pdfPath))
val form = document.documentCatalog.acroForm
if(form != null) {
   form.needAppearances = false
}
document.save(pdfPath)
document.close()

The second method is to obtain PDAnnotation and specify a different font for the font (COSName.DA).
The modified code is below.

PDDocument.load(File(pdfPath)).use { document ->
  val annotations = arrayListOf<PDAnnotation>()
  val page = document.pages[0]
    for (annotation in page.annotations) {
      if (annotation.subtype == PDAnnotationWidget.SUB_TYPE) {
        var cosObject = annotation.cosObject
        cosObject.setString(COSName.DA, PDType1Font.HELVETICA.baseFont)
        annotations.add(annotation)
      }
    }
  page.annotations = annotations
  document.save(pdfPath)
}

Upvotes: 0

Related Questions