Search your query

Wednesday, February 1, 2017

Code to convert PDF file into Binary Stream


     /// <summary>
    /// PDF to base64 (Binary) Converter
    /// </summary>

    public static void main(Args args) //PDF2base64Converter
    {
        System.Byte[] pdfDocBuffer;
        System.IO.FileInfo fi_pdfDoc;
        System.IO.FileStream fs;
        str Content;
 
        // Grant clrinterop permission.
        new InteropPermission(InteropKind::ClrInterop).assert();

        //Load the file
        fi_pdfDoc = new System.IO.FileInfo(@'d:/SSRS/CustTrans_SSRS.pdf');

        //Initiallize the byte array by setting the length of the file
        pdfDocBuffer= new System.Byte[int642int(fi_pdfDoc.get_Length())]();

        // Stream the file
        fs= new System.IO.FileStream(fi_pdfDoc.get_FullName(), System.IO.FileMode::Open,        
        System.IO.FileAccess::Read);

        fs.Read(pdfDocBuffer, 0, pdfDocBuffer.get_Length());

        // Convert the file into a base64 string
        Content = System.Convert::ToBase64String(pdfDocBuffer, 0, pdfDocBuffer.get_Length());

        //Revert the access
        CodeAccessPermission::revertAssert();

        info(Content);
   }

No comments:

Post a Comment