Convertir PDF a Base64
¿Cómo puedo convertir un PDF a Base64?
El objeto PdfDocument no contiene una propiedad directa para obtener Base64. Sin embargo, puedes obtener el arreglo de bytes, que luego se puede usar para obtener la cadena 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
Explicación:
- Inicialización de
PdfDocument: El objetoPdfDocumentse inicializa con el nombre del archivo del PDF que deseas convertir. ReemplazaSomePdfLibrarycon la biblioteca actual que estás usando. - Recuperación de
BinaryData: Recupera los datos binarios (como un arreglo de bytes) del PDF dado. - Conversión a Base64: El método
Convert.ToBase64Stringse utiliza para convertir el arreglo de bytes en una cadena Base64. - Cadena Base64 de salida: La cadena codificada en Base64 se imprime en la consola para verificación.

