Konwersja PDF do Base64
Curtis Chau
Zaktualizowano: 2 sierpnia 2026
Jak mogę przekonwertować PDF na Base64?
Obiekt PdfDocument nie zawiera bezpośredniej właściwości do uzyskania Base64. Możesz jednak uzyskać tablicę bajtów, która może być użyta do uzyskania ciągu Base64.
using System;
using IronPdf;
class Program
{
static void Main()
{
// Load the specified PDF file into a PdfDocument object
var pdf = PdfDocument.FromFile("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 IronPdf
Module Program
Sub Main()
' Load the specified PDF file into a PdfDocument object
Dim pdf = PdfDocument.FromFile("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 ModuleImports System
Imports IronPdf
Module Program
Sub Main()
' Load the specified PDF file into a PdfDocument object
Dim pdf = PdfDocument.FromFile("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 ModuleWyjaśnienie:
PdfDocumentInicjalizacja: ObiektPdfDocumentjest ładowany z nazwy pliku PDF, który chcesz przekonwertować za pomocąPdfDocument.FromFile.BinaryDataPobieranie: Pobiera dane binarne (w postaci tablicy bajtów) podanego PDF.- Konwersja 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.
Autor techniczny
Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podręczników.