3

I have written a c# web service that returns a pdf in a stream of bytes as response. Once I make a call to the web-service from my android app, I will store the response in an array byte till here I will be able to do it. But after that I need to convert that byte array into pdf, I should be able to display that. I have a menu page in which once the button is pressed the call is made to the web service with file name and on click of button I should be able to open pdf. Is this possible? Or there is some other, better solution? I checked on the net for better understanding, but I was unable to find one that could help me understand better.

Thanks for the suggestion, but I don't have the pdf in hand, I just have the array bytes, which I got from the web service. So I now need to regenerate the pdf from this array of bytes and display it, but I am not getting how to do it.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • possible duplicate of [Display PDF within app on Android?](http://stackoverflow.com/questions/2456344/display-pdf-within-app-on-android) –  Aug 28 '15 at 05:49
  • see also http://stackoverflow.com/questions/9666030/display-pdf-file-inside-my-android-application and http://stackoverflow.com/questions/14578530/how-to-open-display-documents-pdf-doc-without-external-app –  Aug 28 '15 at 05:50
  • you shouhttp://stackoverflow.com/questions/9060309/downloading-pdf-from-server-and-displaying-itld post the code – karan Aug 28 '15 at 05:51
  • http://stackoverflow.com/questions/20603713/opening-pdf-file-from-server-using-android-intent – karan Aug 28 '15 at 05:52
  • how to write byte array to file http://stackoverflow.com/questions/6828634/write-byte-to-file-in-java – karan Aug 28 '15 at 06:01

1 Answers1

0

Try following these steps

  1. Convert byte array to InputStream
val inputStream = ByteArrayInputStream(byteArray) 
  1. Save InputStream as PDF file:
    suspend fun saveInputStreamAsPdfFile(inputStream: InputStream, applicationContext: Context): File? {
        var outputFile: File? = null
        withContext(Dispatchers.IO) {
            try {
                val directory = ContextCompat.getExternalFilesDirs(applicationContext, "documents").first()
                val outputDir = File(directory, "outputPath")
                outputFile = File(outputDir, UUID.randomUUID().toString() + ".pdf")
                if (!outputDir.exists()) {
                    outputDir.mkdirs()
                }
                val outputStream = FileOutputStream(outputFile, false)
                inputStream.use { fileOut -> fileOut.copyTo(outputStream) }
                outputStream.close()
            } catch (e: Exception) {
                // Something went wrong
            }
        }
        return outputFile
    }
  1. Show PDF with PdfRenderer
var totalPdfPages: Int

fun showPdf(pdfFile: File) {
    val input = ParcelFileDescriptor.open(pdfFile, MODE_READ_ONLY)
    val renderer = PdfRenderer(input)
    val wrapper = PdfRendererWrapper(renderer)
    totalPdfPages = wrapper.getTotalPages()
    showPdfPage(0)
}

fun showPdfPage(currentPageIndex: Int) {
    val pageBitmap = wrapper.getBitmap(currentPageIndex)
    imageView.setImageBitmap(pageBitmap) // Show current page
}
Juan Fraga
  • 434
  • 3
  • 9