LoginSignup
1
1

More than 3 years have passed since last update.

同階層以下のPDFファイルを全件読んで、PDFのページ数合計を書き出すC#のコマンドラインアプリ

Last updated at Posted at 2019-09-28

概要

同階層以下のPDFファイルを全件読んで、
PDFのページ数合計を書き出すC#のコマンドラインアプリです。
ページ数のカウントは、それなりなので、「概数の把握」と思ってください。

使い方

CountPDFPages.exeをダブルクリックするだけで使えます。

ページ数カウントの説明

PDFのページ数は、ファイルをテキストとして読み込んで、

<</Type/Pages/Count 1/Kids[3 0 R]/MediaBox[0 0 595 842]>>

というように、「/Count 1」と文字列で入っているので、この部分だけを探して読んでいます。(この文字列がない場合は、不明(Unknown)と出力します)

コンパイルの仕方

以下のソースを、「CountPDFPages.cs」の名で保存し、コマンドラインから、
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:exe CountPDFPages.cs
と実行し、コンパイルしてください(csc.exeのパスが異なる場合は適宜修正)

実行イメージ

exeをダブルクリック

キャプチャ2.PNG

黒い画面が出て、ファイル名と、各ファイルのページ数、
合計が表示されます。何か押せば画面は消えます。

キャプチャ.PNG

ソース

CountPDFPages.cs


//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:exe CountPDFPages.cs

using System;
using System.Text.RegularExpressions;

namespace CountPDFPages
{
    class Program
    {

    static int Cnt;

    static void Main(string[] args){
        Cnt = 0;

        string appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(appPath);
        System.IO.FileInfo[] files =
            di.GetFiles("*.pdf", System.IO.SearchOption.AllDirectories);


        foreach (System.IO.FileInfo f in files)
        {

            getPageCount(f.FullName,appPath);
        }

        System.Console.WriteLine("------------------------------");

        System.Console.Write(" Total \t");
        System.Console.WriteLine(Cnt.ToString());
        Console.ReadLine();

    }

    static void getPageCount(string fname, string ptname){

        try{
            Regex rgx = new Regex(@"/Count ", RegexOptions.IgnoreCase);
            using (System.IO.StreamReader file = new System.IO.StreamReader(fname, System.Text.Encoding.ASCII))
                {
                    string line = "";
                    bool b = true;

                    while ((line = file.ReadLine()) != null)
                    {
                        //System.Console.WriteLine(line);
                        if (rgx.Match(line).Success)
                        {

                        string fn;
                        fn = fname.Replace(ptname, ""); 

                        System.Console.Write(fn);
                        System.Console.Write("\t");

                        line = getCountStr(line);

                        System.Console.WriteLine(line);

                        int c = Int32.Parse(line);
                        Cnt = Cnt + c;
                        b = false;
                        return;

                        }
                    }
                    if(b){
                        string fn;
                        fn = fname.Replace(ptname, ""); 
                        System.Console.Write(fn);
                        System.Console.Write("\t");
                        System.Console.WriteLine("Unknown");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

    }


    static string getCountStr(string line){

        string rtLine = "";
        string searchWord = "/Count ";
        int i0 = line.IndexOf("/Count ");
        if (i0>=0){
            int ii0 = line.IndexOf("/",i0+searchWord.Length);
            if(ii0<0){ii0 = line.IndexOf(" ",i0+searchWord.Length);}
            if(ii0<0){ii0 = line.IndexOf(">>",i0+searchWord.Length);}
            if(ii0>=0){
                line =  line.Substring(i0,ii0-i0);
            }else{
                line =  line.Substring(i0);
            }
            line = line.Replace("/Count ", "");
            rtLine =  line;
        }

        return rtLine;

    }

    }
}


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