PDFをBase64に変換する
This article was translated from English: Does it need improvement?
Translated
View the article in English
どのようにPDFをBase64に変換できますか?
PdfDocument オブジェクトには、Base64 を取得するための直接的なプロパティは含まれていません。ただし、バイト配列を取得することで、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
$vbLabelText
$csharpLabel
説明:
PdfDocument初期化:PdfDocumentオブジェクトは、変換する PDF のファイル名で初期化されます。SomePdfLibraryを実際に使用しているライブラリに置き換えます。BinaryData取得:指定された PDF のバイナリ データ (バイト配列として) を取得します。- Base64 変換:
Convert.ToBase64Stringメソッドを使用して、バイト配列を Base64 文字列に変換します。 - 出力Base64文字列: Base64エンコードされた文字列を検証のためにコンソールに出力します。
準備はできましたか?
Nuget ダウンロード 18,560,885 | バージョン: 2026.4 リリース

