3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#にてPDFファイルをExcelに変換ツール作成

Last updated at Posted at 2021-09-30

概要

業務中にPDFファイルをExcelに変換をするツールを作ってみたかったので、
調べてみたら下記のライブラリで簡単なPDFファイルをExcelに変換ができましたので、
C#の言語でサンプルをメモします。
Qiitaにも調べてみたのですが、あんまり無料で変換できるライブラリが見つからなかったので、
前回使っていたライブラリがあったので、皆さんに紹介したいと思います。

使用するライブラリ名「FreeSpire.PDF」

■変換可能なフォーマット
PDF→Word
PDF→HTML
PDF→Excel
PDF→JPEG、TIFF
PDF→Text

PDFをWord及びExcel保存

その他のライブラリも事前にインストールしましょう。
拡張をdoc,xlsをdocx,xlsxに変換するためです。
下記のソースをVSに入れてビルドします。

using Spire.Pdf;
using System;
using System.IO;

namespace PDFToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            convertFile();
        }

        static void convertFile()
        {
            try
            {
                string[] filelist = Directory.GetFiles(@"./", "*.pdf", SearchOption.AllDirectories);

                foreach (string fn in filelist)
                {
                    FileInfo fi = new FileInfo(fn);
                    string pdfname = fi.DirectoryName + @"\" + fi.Name;
                    string xlsxname = fi.DirectoryName + @"\" + fi.Name.Replace(fi.Extension, ".xlsx");
                    string docname = fi.DirectoryName + @"\" + fi.Name.Replace(fi.Extension, ".doc");

                    Console.WriteLine("pdf=" + pdfname);
                    Console.WriteLine("excel=" + xlsxname);
                    Console.WriteLine("doc=" + docname);

                    //PdfDocument objectを作成します。
                    PdfDocument pdf = new PdfDocument();

                    //PDFファイルをロードします。
                    pdf.LoadFromFile(pdfname);

                    //Excelで保存します。
                    pdf.SaveToFile(xlsxname, FileFormat.XLSX);
                    pdf.SaveToFile(docname, FileFormat.DOCX);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.ToString());
            }
        }
    }
}


テスト結果

次のフォルダでビルドしたファイルを実行します。

Microsoft Windows [Version 10.0.19043.1237]
(c) Microsoft Corporation. All rights reserved.

C:\PDFToExcel\PDFToExcel\bin\Debug>PDFToExcel.exe
pdf=C:\PDFToExcel\PDFToExcel\bin\Debug\table.pdf
excel=C:\PDFToExcel\PDFToExcel\bin\Debug\table.xlsx
doc=C:\PDFToExcel\PDFToExcel\bin\Debug\table.doc
pdf=C:\PDFToExcel\PDFToExcel\bin\Debug\table2.pdf
excel=C:\PDFToExcel\PDFToExcel\bin\Debug\table2.xlsx
doc=C:\PDFToExcel\PDFToExcel\bin\Debug\table2.doc

C:\PDFToExcel\PDFToExcel\bin\Debug>

・原本のPDF
image.png
image.png

・変換したXLSX
image.png
image.png
※複数ページも変換が可能でした。

・変換したDOC
image.png
image.png
image.png
※複数ページも変換が可能でした。

参照サイト

・Convert PDF to Excel in C#, VB.NET
https://www.e-iceblue.com/Tutorials/Spire.PDF/Program-Guide/Conversion/Convert-PDF-to-Excel-in-C-VB.NET.html

・How to convert PDF to Doc in C#/VB.NET
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Conversion/How-to-convert-PDF-to-Doc-in-C-VB.NET.html

今回のライブラリバージョン

FreeSpire.XLSも追加はしましたが、ソース上では未使用でした。
image.png
※下記の制限があったらしいですが、今は問題ないように見えますね。もっと確認は必要です。^^

↓↓↓

※注意事項
・無料版は、ブックあたり5シート、シートあたり200行に制限されています。この制限は、XLSまたはPDFファイルの読み取りまたは書き込み中に適用されます。 Free Spire.XLS v7.8以降、.xlsxファイル形式の読み込みと保存に制限はありません。 ExcelファイルをPDFファイルに変換する場合、PDFファイルの最初の3ページしか取得できません。

・フレンドリーリマインダー:
無料版は500段落と25テーブルに制限されています。この制限は、ファイルの読み取りまたは書き込み中に適用されます。ワードドキュメントをPDFおよびXPSファイルに変換する場合、PDFファイルの最初の3ページしか取得できません。 Spire.Docの商用版にアップグレード

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?