Converter PDF para Base64
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
Explicação:
PdfDocumentInicialização: O objetoPdfDocumenté inicializado com o nome do arquivo PDF que você deseja converter. SubstituaSomePdfLibrarypela biblioteca que você está usando.BinaryDataRecuperaçã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.

