Konvertieren von PDF in Base64
Wie kann ich eine PDF-Datei in Base64 konvertieren?
Das Objekt PdfDocument enthält keine direkte Eigenschaft zum Abrufen von Base64. Sie können jedoch das Byte-Array abrufen, aus dem dann die Base64-Zeichenkette gewonnen werden kann.
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: DasPdfDocumentObjekt wird mit dem Dateinamen der PDF-Datei initialisiert, die Sie konvertieren möchten. Ersetzen SieSomePdfLibrarydurch die tatsächlich verwendete Bibliothek.BinaryDataAbruf: Es ruft die Binärdaten (als Byte-Array) der gegebenen PDF ab.- Base64-Konvertierung: Die Methode
Convert.ToBase64Stringwird verwendet, um das Byte-Array in eine Base64-Zeichenkette umzuwandeln. - Ausgabe des Base64-Strings: Der Base64-kodierte String wird zur Überprüfung auf der Konsole ausgedruckt.

