Some test text!

menu
search

Read & write a PDF file from/to memory buffer in Kotlin

Sample Kotlin code for using PDFTron SDK to read/write a PDF document from/to memory buffer. This is useful for applications that work with dynamic PDFdocuments that don't need to be saved/read from a disk.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.common.PDFNetException
import com.pdftron.filters.FilterReader
import com.pdftron.filters.MappedFile
import com.pdftron.pdf.Element
import com.pdftron.pdf.ElementReader
import com.pdftron.pdf.ElementWriter
import com.pdftron.pdf.PDFDoc
import com.pdftron.sdf.SDFDoc
import java.io.*
import java.util.*

class PDFDocMemoryTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_pdfdocmemory_title)
        setDescription(R.string.sample_pdfdocmemory_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)

        // The following sample illustrates how to read/write a PDF document from/to
        // a memory buffer.  This is useful for applications that work with dynamic PDF
        // documents that don't need to be saved/read from a disk.
        try {
            // Read a PDF document in a memory buffer.
            val file = MappedFile(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "tiger.pdf")!!.absolutePath)
            val file_sz = file.fileSize()

            val file_reader = FilterReader(file)

            val mem = ByteArray(file_sz.toInt())

            val bytes_read = file_reader.read(mem)
            val doc = PDFDoc(mem)

            doc.initSecurityHandler()
            val num_pages = doc.pageCount

            val writer = ElementWriter()
            val reader = ElementReader()
            var element: Element?

            // Create a duplicate of every page but copy only path objects

            for (i in 1..num_pages) {
                val itr = doc.getPageIterator(2 * i - 1)
                val current = itr.next()!!
                reader.begin(current)
                val new_page = doc.pageCreate(current.getMediaBox())
                doc.pageInsert(itr, new_page)

                writer.begin(new_page)
                // Read page contents
                while (true) {
                    element = reader.next()
                    if (element == null) {
                        break
                    }
                    //if (element.getType() == Element.e_path)
                    writer.writeElement(element)
                }

                writer.end()
                reader.end()
            }

            doc.save(Utils.createExternalFile("doc_memory_edit.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)

            // Save the document to a memory buffer.

            val buf = doc.save(SDFDoc.SaveMode.REMOVE_UNUSED, null)
            // doc.Save(buf, buf_sz, Doc::e_linearized, NULL);

            // Write the contents of the buffer to the disk
            run {
                val outfile = File(Utils.createExternalFile("doc_memory_edit.txt", mFileList).absolutePath)
                val fop = FileOutputStream(outfile)
                if (!outfile.exists()) {
                    outfile.createNewFile()
                }
                fop.write(buf)
                fop.flush()
                fop.close()
            }

            // Read some data from the file stored in memory
            reader.begin(doc.getPage(1))
            // Read page contents
            while (true) {
                element = reader.next()
                if (element == null) {
                    break
                }
                if (element.type == Element.e_path) mOutputListener!!.print("Path, ")
            }
            reader.end()

            doc.close()
            mOutputListener!!.println("\n\nDone. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ...")
        } catch (e: PDFNetException) {
            mOutputListener!!.printError(e.stackTrace)
        } catch (e: FileNotFoundException) {
            // TODO Auto-generated catch block
            mOutputListener!!.printError(e.stackTrace)
        } catch (e: IOException) {
            // TODO Auto-generated catch block
            mOutputListener!!.printError(e.stackTrace)
        }

        //This sample illustrates how to open a PDF document
        //from an Java InputStream and how to save to an OutputStream.
        try {
            val doc = PDFDoc(FileInputStream(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath))
            doc.save(FileOutputStream(Utils.createExternalFile("StreamTest.pdf", mFileList).absolutePath), SDFDoc.SaveMode.NO_FLAGS, null)
            doc.close()
            mOutputListener!!.println("\n\nDone. Result saved in StreamTest.pdf ...")
        } catch (e: PDFNetException) {
            // TODO Auto-generated catch block
            mOutputListener!!.printError(e.stackTrace)
        } catch (e: FileNotFoundException) {
            // TODO Auto-generated catch block
            mOutputListener!!.printError(e.stackTrace)
        } catch (e: IOException) {
            // TODO Auto-generated catch block
            mOutputListener!!.printError(e.stackTrace)
        }

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More
close