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, se puede 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);
}
}
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:
PdfDocumentInicialización: El objetoPdfDocumentse inicializa con el nombre del archivo PDF que desea convertir. ReemplaceSomePdfLibrarycon la biblioteca real que está utilizando.BinaryDataRecuperación: Recupera los datos binarios (como una matriz de bytes) del PDF dado.- Conversión Base64: El método
Convert.ToBase64Stringse utiliza para 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.

