Convert PDF to Base64

How can I convert a PDF to Base64?

The PdfDocument object doesn't contain a direct property to get Base64. However, you can get the byte array, which can then be used to obtain the Base64 string.

using System;
using IronPdf;

class Program
{
    static void Main()
    {
        // Load the specified PDF file into a PdfDocument object
        var pdf = PdfDocument.FromFile("MyPDF.pdf");

        // Get the binary data (byte array) from the PDF document
        var byteArray = pdf.BinaryData;

        // Convert the byte array to a Base64 string
        var base64Result = Convert.ToBase64String(byteArray);

        // Output the Base64 result
        Console.WriteLine("Base64 of PDF: " + base64Result);
    }
}
using System;
using IronPdf;

class Program
{
    static void Main()
    {
        // Load the specified PDF file into a PdfDocument object
        var pdf = PdfDocument.FromFile("MyPDF.pdf");

        // Get the binary data (byte array) from the PDF document
        var byteArray = pdf.BinaryData;

        // Convert the byte array to a Base64 string
        var base64Result = Convert.ToBase64String(byteArray);

        // Output the Base64 result
        Console.WriteLine("Base64 of PDF: " + base64Result);
    }
}
Imports System
Imports IronPdf

Module Program
    Sub Main()
        ' Load the specified PDF file into a PdfDocument object
        Dim pdf = PdfDocument.FromFile("MyPDF.pdf")

        ' Get the binary data (byte array) from the PDF document
        Dim byteArray = pdf.BinaryData

        ' Convert the byte array to a Base64 string
        Dim base64Result = Convert.ToBase64String(byteArray)

        ' Output the Base64 result
        Console.WriteLine("Base64 of PDF: " & base64Result)
    End Sub
End Module
$vbLabelText   $csharpLabel

Explanation:

  • PdfDocument Initialization: The PdfDocument object is loaded from the file name of the PDF you want to convert using PdfDocument.FromFile.
  • BinaryData Retrieval: It retrieves the binary data (as a byte array) of the given PDF.
  • Base64 Conversion: The Convert.ToBase64String method is used to convert the byte array into a Base64 string.
  • Output Base64 String: The Base64 encoded string is printed to the console for verification.
Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 19,972,802 | Version: 2026.7 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast? PM > Install-Package IronPdf
run a sample watch your HTML become a PDF.