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);
    }
}
$vbLabelText   $csharpLabel

説明:

  • PdfDocument 初期化: PdfDocument オブジェクトは、変換する PDF のファイル名で初期化されます。 SomePdfLibrary を実際に使用しているライブラリに置き換えます。
  • BinaryData 取得:指定された PDF のバイナリ データ (バイト配列として) を取得します。
  • Base64 変換: Convert.ToBase64String メソッドを使用して、バイト配列を Base64 文字列に変換します。
  • 出力Base64文字列: Base64エンコードされた文字列を検証のためにコンソールに出力します。
カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 17,803,474 | バージョン: 2026.3 リリース
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronPdf
サンプルを実行するHTML が PDF に変換されるのを確認します。