Konwersja PDF do Base64
Jak mogę przekonwertować PDF na Base64?
Obiekt PdfDocument nie zawiera bezpośredniego właściwości do uzyskania Base64. Jednakże, można uzyskać tablicę bajtów, którą następnie można wykorzystać do uzyskania ciągu 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
Wyjaśnienie:
PdfDocumentInicjalizacja: ObiektPdfDocumentjest inicjalizowany z nazwą pliku PDF, który chcesz konwertować. ZamieńSomePdfLibraryna rzeczywistą bibliotekę, której używasz.BinaryDataPobranie: Pobiera dane binarne (jako tablica bajtów) podanego PDF.- Konwersja do Base64: Metoda
Convert.ToBase64Stringjest używana do konwersji tablicy bajtów na ciąg Base64. - Ciąg Base64 Wyjściowy: Zakodowany ciąg Base64 jest drukowany na konsoli dla weryfikacji.

