locked
How to Convert PDF file to base64binary. RRS feed

  • Question

  • hello,

    I wanted to convert PDF to base64binary in a class library.

    can some one help me how to do that?

    Thanks.

    Monday, July 9, 2012 9:01 PM

Answers

  • Hi bobby us,

    You'd just need to read the contents of the file into a byte array and then use the Convert.ToBase64String() method to get the Base64 string:

    byte[] pdfBytes = File.ReadAllBytes(pdfPath);
    string pdfBase64 = Convert.ToBase64String(pdfBytes);

    Regards,

    Tyler

    • Proposed as answer by Tyler_A Tuesday, July 10, 2012 3:57 PM
    • Marked as answer by Jason Dot Wang Tuesday, July 17, 2012 8:21 AM
    Monday, July 9, 2012 9:21 PM
  • If you want convert to base64 for store that as a blob in database, i recommend you convert to a byte[].
    Monday, July 9, 2012 10:33 PM

All replies

  • Hi bobby us,

    You'd just need to read the contents of the file into a byte array and then use the Convert.ToBase64String() method to get the Base64 string:

    byte[] pdfBytes = File.ReadAllBytes(pdfPath);
    string pdfBase64 = Convert.ToBase64String(pdfBytes);

    Regards,

    Tyler

    • Proposed as answer by Tyler_A Tuesday, July 10, 2012 3:57 PM
    • Marked as answer by Jason Dot Wang Tuesday, July 17, 2012 8:21 AM
    Monday, July 9, 2012 9:21 PM
  • Thanks Tyler

    I need base64binary.Is base64binary and base64string are same?


    v

    Monday, July 9, 2012 10:09 PM
  • Hi bobby us,

    I'm not entirely sure what you mean when you say "Base64Binary". The term itself is somewhat contradictory since Base64 and Binary are two different ways of expressing a number. 

    Generally when binary data needs to be transferred over a medium that transfers solely text it is converted to Base64 first, this is due to the fact that it's a lot more efficient to send "A" than "00000000". Could you expand on what you intend on doing with the resulting Base64Binary?

    Regards,

    Tyler

    Monday, July 9, 2012 10:22 PM
  • If you want convert to base64 for store that as a blob in database, i recommend you convert to a byte[].
    Monday, July 9, 2012 10:33 PM
  • Hi bobby,

      I really feel that the purpose of conversation from PDF to base64binary doesn't make sense.I found out the soluation about how to convert base64binary to PDF file,perhap it is more helpful for you.

      convert base64Binary to pdf 

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 11, 2012 5:36 AM
  • Hello Tyler,

    I am also able to use the above code for converting my pdf file to a base64 string.
    Below is my converted string -

    pdfBase64 = "JVBERi0xLjQNJeLjz9MNCjEgMCBvYmoNPDwvQWNyb0Zvcm0gMyAwIFIvTWV0YWRhdGEgNjE4IDAgUi9QYWdlcyAyID

    AgUi9UeXBlIC9DYXRhbG9nPj4NZW5kb2JqDTIgMCBvYmoNPDwvQ291bnQgNi9LaWRzIFs2IDAgUiA3IDAgUiA4IDAgUi

    A5IDAgUiAxMCAwIFIgMTEgMCBSXS9UeXBlIC9QYWdlcz4+DWVuZG9iag0zIDAgb2JqDTw8L0RB...

    If you see the above converted string, it is ending with characters such as "...". I have a doubt that there is something wrong with this conversion. Correct me if i am wrong, is that the correct conversion?

    Below is the code that i am using for my conversion

    byte[] pdfBytes = File.ReadAllBytes(@"C:\Users\ravsingh\Documents\visual studio 2012\Projects\ConsoleApplication1\Signed Contract_1486458475816.pdf");
                    string pdfBase64 = Convert.ToBase64String(pdfBytes);

    Thanks in advance.

    Regards,

    Ravi Singh


    Thursday, May 18, 2017 5:47 AM