Search code examples
c#encodingbase64

Base64 Encode a PDF in C#?


Can someone provide some light on how to do this? I can do this for regular text or byte array, but not sure how to approach for a pdf. do i stuff the pdf into a byte array first?


Solution

  • Use File.ReadAllBytes to load the PDF file, and then encode the byte array as normal using Convert.ToBase64String(bytes).

     Byte[] fileBytes = File.ReadAllBytes(@"TestData\example.pdf");
     var content = Convert.ToBase64String(fileBytes);