Konvertieren von PDF in Base64
Wie kann ich eine PDF-Datei in Base64 konvertieren?
Das Objekt PdfDocument enthält keine direkte Eigenschaft, um Base64 zu erhalten. Sie können jedoch das Byte-Array abrufen, welches dann verwendet werden kann, um den Base64-String zu erhalten.
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
Erklärung:
PdfDocumentInitialisierung: Das ObjektPdfDocumentwird mit dem Dateinamen des PDF initialisiert, das Sie konvertieren möchten. Ersetzen SieSomePdfLibrarydurch die tatsächliche Bibliothek, die Sie verwenden.BinaryDataAbruf: Es ruft die Binärdaten (als Byte-Array) des angegebenen PDF ab.- Base64-Konvertierung: Die Methode
Convert.ToBase64Stringwird verwendet, um das Byte-Array in einen Base64-String zu konvertieren. - Ausgabe des Base64-Strings: Der Base64-kodierte String wird zur Überprüfung auf der Konsole ausgedruckt.

