1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C# Windowsフォームアプリケーションで PdfiumViewer を使用してPDFを表示

Posted at

はじめに

Windows Forms アプリケーションで PDF ファイルを表示する方法はいくつかありますが、PdfiumViewer を使用すると、よりスムーズに PDF の表示・操作が可能になります。前回の記事で、WebBrowser を利用して PDF ファイルをデータベースから取得し、表示する方法を解説しました。

PdfiumViewer とは?

PdfiumViewer は、PDFium をベースとした .NET 用の PDF ビューアライブラリです。高速な描画とシンプルなインターフェースを提供し、Windows Forms で簡単に PDF を表示できます。

PdfiumViewer のインストール

まず、NuGet パッケージマネージャを使用して PdfiumViewer をインストールします。

この記事ではMicrosoft Visual Studio 2022、.Net Framework 4.8を使っています。
Install-Package PdfiumViewer
Install-Package PdfiumViewer.Native.x86.v8-xfa
Install-Package PdfiumViewer.Native.x86_64.v8-xfa 

Windows Forms アプリケーションのセットアップ

Windows Forms に PdfiumViewer を組み込むために、新しい Form を作成し、PDF ビューアを設定します。

クラスの作成

クラスを作成し、ボタンやDataGridViewを追加します。

public partial class FileManagement : Form
{
    private PdfViewer pdfViewer;
    private DataGridView dataGridView;
    private Button btnNew;
    private Button btnDisplayPdfviewer;
    private FileServices _fileServices;
    private DataTable _dt;
    private MemoryStream stream;

    public FileManagement()
    {
        InitializeControls();
    }

    private void InitializeControls()
    {
        // ボタンの設定
        btnNew = new Button { Text = "新規", Location = new Point(712, 37), Width = 150 };

        btnDisplayPdfviewer = new Button { Text = "表示(PdfViewer)", Location = new Point(712, 133), Width = 150 };

        btnNew.Click += btnAdd_Click;
        
        btnDisplayPdfviewer.Click += btnShow_Click;

        // DataGridViewの設定
        dataGridView = new DataGridView { Location = new Point(32, 37), Width = 602, Height = 313 };

        // フォームに追加
        this.Controls.Add(btnNew);
        this.Controls.Add(btnDisplayPdfviewer);
        this.Controls.Add(dataGridView);
    }
}

PDF を PdfiumViewer で表示する

選択したファイルを PdfiumViewer で開くように実装します。

private void btnShow_Click(object sender, EventArgs e)
{
    LoadPdfFromDatabase("pdfViewer");
}

private async void LoadPdfFromDatabase(string openWith)
{
    if (dataGridView.SelectedRows.Count > 0)
    {
        DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
        string fileNo = selectedRow.Cells["fileNo"].Value.ToString();

        string where = $"WHERE fileNo = {int.Parse(fileNo)}";
        var data = await _fileServices.GetFile(where) ?? new DataTable();
        
        foreach (DataRow row in data.Rows)
        {
            byte[] pdfData = (byte[])row["fileData"];
            string fileName = row["fileName"].ToString();

            if (openWith == "pdfViewer")
            {
                FileManagement form = new FileManagement(pdfData);
                form.ShowDialog();
            }
        }
    }
}

PdfViewer を使用した PDF 表示

データベースから取得したバイナリデータを PdfiumViewer で開きます。

public FileManagement(byte[] pdfData)
{
    Text = "PDF Viewer";
    Size = new Size(800, 600);
    pdfViewer = new PdfViewer { Dock = DockStyle.Fill };

    // PDFをMemoryStreamに読み込む
    stream = new MemoryStream(pdfData);
    pdfViewer.Document = PdfDocument.Load(stream);

    this.Controls.Add(pdfViewer);
}

image.png

image.png

まとめ

PdfiumViewer を使用して C# Windows Forms アプリケーションで PDF を表示する方法を紹介しました。データベースに保存された PDF がPdfiumViewer を利用することで、より快適な PDF ビューアを構築できます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?