ひとりも見捨てないことを、あきらめない

学校教育、社会教育、数学、技術家庭科、Youtube、EdTech、ICT、プログラミング、その他

PDFファイルを読むことに成功しました 12/31 火

 Visual Studio 2019 の C# NET Core では、NuGet を用いて、PDFium ライブラリを使うことができます。しかし、私が拝見したブログの内容をそのまま実現することはできませんでした。

 そうなると、他の方法を考えなければなりません。NuGet のライブラリには、ダウンロードの件数が記載されています。「ダウンロードの件数が多い方が、人気があって使いやすいだろう」と考えました。残念ながら件数順に並んでいるわけではないので、ざっくり眺めてみて、次のいくつかを候補として考えました。

  • PDFium Viewer
  • CUBE PDFium
  • PDFiumLight
  • PDFium.Net.SDK

 どれが良いか分からないので、順番にやってみるしかありません。しかし、インストールがそもそもできなかったり、肝心の C# NET での使い方がわからなかったりで、なかなかうまく行きません。マニュアルが見当たらないというのも、困ったことです。ライブラリだけあっても、使い方が分からなければ、使いようがありません。

 そんなときに、(あれこれ検索しているうちに)見つけたのが、次のページです。

  そのものズバリで、「NuGet での pdfium パッケージのベスト20 」です。見てみると、最も人気があるのは、PDFium.Net.SDK でした。そこで、このパッケージについて集中的に検索してみると、次のようなマニュアルページが見つかりました。

 これが、まさに私が必要としていたページで、しかも C# のサンプルプログラムも、多数掲載されています。また、最初のページには、次のような英文が記載されていました。

Compatibility

Pdfium.Net SDK is available for .Net Framework 2.0 - 4.7 on 32- and 64-bit operating systems.

SDK has been tested with Windows XP, Vista, 7, 8, 8.1 and 10, and is fully compatible with all of them. The native PDFium.dll library included to this project is supplied in both 32-bit and 64-bit versions, so your .NET application can be "Any CPU".

 そして、私が必要としていたのは、「PDF を画像に変換する」という機能なのですが、そのことについては、次のようなサンプルが記載されていました。

    public void RenderPage()
{
    //Initialize the SDK library
    //You have to call this function before you can call any PDF processing functions.
    PdfCommon.Initialize();

    //Open and load a PDF document from a file.
    using (var doc = PdfDocument.Load(@"c:\test001.pdf"))
    {
        int i = 0;
        //Iterate all pages;
        foreach (var page in doc.Pages)
        {
            //Gets page width and height measured in points. One point is 1/72 inch (around 0.3528 mm)
            int width = (int)page.Width;
            int height = (int)page.Height;

            //Create a bitmap
            using (var bmp = new PdfBitmap(width, height, true))
            {
                //Fill background
                bmp.FillRect(0, 0, width, height, FS_COLOR.White);
                //Render contents in a page to a drawing surface specified by a coordinate pair, a width, and a height.
                page.Render(bmp, 0, 0, width, height,
                    Patagames.Pdf.Enums.PageRotate.Normal,
                    Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT);
                //Get .Net image and save it into file
                bmp.Image.Save(string.Format(@"c:\test001_pdf_page_{0}.png", i++), ImageFormat.Png);
            }
        }
    }

}

というわけで、実際に、やってみようと考え、C# NET Framework 4.7 でプロジェクトを作成し、NuGet で PDFium.Net.SDK をインストールしました。

 一番不安だったのは、名前空間の指定でした。実際のところ、上のプログラムをコピペしてビルドしてみると、「これこれが、見当たらない」というエラー・メッセージがたくさん出ます。しかし、オブジェクト・ブラウザーで検索すると、その見当たらないものがどの名前空間に所属しているのがわかりました。だとすれば、using にその名前空間を追加すればよいわけです。このようにして、実際には次の4つの名前空間を追加しました。

using Patagames;
using Patagames.Pdf;
using Patagames.Pdf.Net;
using System.Drawing.Imaging;

 

このようにして、作成したプログラムが以下のとおりです。

    using Patagames;
using Patagames.Pdf;
using Patagames.Pdf.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test03_SDK
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }
        private void Form1_Click(object sender, EventArgs e) { RenderPage(); }

        public void RenderPage() {
            PdfCommon.Initialize(); // これは必ず必要
            using (var doc = PdfDocument.Load(@"c:\tmp\03.pdf")) // ファィルを開く
            {
                int i = 0;
                foreach (var page in doc.Pages) {
                    int width = (int)page.Width;   // 幅
                    int height = (int)page.Height; // 高さ

                    int nw = 1000;
                    int nh = height * 1000 / width;

                    var bmp = new PdfBitmap(nw, nh, true); // bitmap をつくる
                    bmp.FillRect(0, 0, nw, nh, FS_COLOR.White); // 背景は白

                    page.Render(bmp, 0, 0, nw, nh,
                        Patagames.Pdf.Enums.PageRotate.Normal,
                        Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT);

                    bmp.Image.Save(
                        string.Format(@"c:\tmp\test_{0}.png", i++),
                        ImageFormat.Png); // pngファイルに保存
                }
            }
        }


    }
}

これによって、PDFファイルを画像ファイルに変換することができました。大きな難関を、ひとつ超えることができました。この次は、データベースの取り扱いについてです。

f:id:takase_hiroyuki:20191123160855p:plain