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
Explanation:
PdfDocumentInitialization: ThePdfDocumentobject is loaded from the file name of the PDF you want to convert usingPdfDocument.FromFile.BinaryDataRetrieval: It retrieves the binary data (as a byte array) of the given PDF.- Base64 Conversion: The
Convert.ToBase64Stringmethod 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.

