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 la matriz 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);
}
}Explicación:
PdfDocumentInicialización: El objetoPdfDocumentse inicializa con el nombre del archivo del PDF que deseas convertir. ReemplazaSomePdfLibrarycon la biblioteca actual que estás utilizando.- Recuperación de
BinaryData: Recupera los datos binarios (como una matriz de bytes) del PDF dado. - Conversión a Base64: Se usa el método
Convert.ToBase64Stringpara convertir la matriz de bytes en una cadena Base64. - Cadena Base64 de salida: La cadena codificada en Base64 se imprime en la consola para verificación.






