Converter PDF para Base64

This article was translated from English: Does it need improvement?
Translated
View the article in English
Como posso converter um PDF para Base64?

O objeto PdfDocument não contém uma propriedade direta para obter Base64. No entanto, você pode obter o array de bytes, que pode então ser usado para obter a string Base64.

using System;
using SomePdfLibrary; // Make sure to import the library used for handling PDF files

class Program
{
    static void Main()
    {
        // Create a PdfDocument object for the specified PDF file
        var pdf = new PdfDocument("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 SomePdfLibrary; // Make sure to import the library used for handling PDF files

class Program
{
    static void Main()
    {
        // Create a PdfDocument object for the specified PDF file
        var pdf = new PdfDocument("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 SomePdfLibrary ' Make sure to import the library used for handling PDF files

Friend Class Program
	Shared Sub Main()
		' Create a PdfDocument object for the specified PDF file
		Dim pdf = New PdfDocument("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 Class
$vbLabelText   $csharpLabel

Explicação:

  • PdfDocument Inicialização: O objeto PdfDocument é inicializado com o nome do arquivo PDF que você deseja converter. Substitua SomePdfLibrary pela biblioteca que você está usando.
  • BinaryData Recuperação: Recupera os dados binários (como uma matriz de bytes) do PDF fornecido.
  • Conversão para Base64: O método Convert.ToBase64String é usado para converter o array de bytes em uma string Base64.
  • Saída da string Base64: A string codificada em Base64 é impressa no console para verificação.
Curtis Chau
Redator Técnico

Curtis Chau é bacharel em Ciência da Computação (Universidade Carleton) e se especializa em desenvolvimento front-end, com experiência em Node.js, TypeScript, JavaScript e React. Apaixonado por criar interfaces de usuário intuitivas e esteticamente agradáveis, Curtis gosta de trabalhar com frameworks modernos e criar manuais ...

Leia mais
Pronto para começar?
Nuget Downloads 18,318,263 | Versão: 2026.4 acaba de ser lançado
Still Scrolling Icon

Ainda está rolando a tela?

Quer provas rápidas? PM > Install-Package IronPdf
executar um exemplo Veja seu HTML se transformar em um PDF.