0
0

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#コードを使用してPDFファイルのページ数を取得する

0
Posted at

PDFファイルのページ数を取得することは、ドキュメントの長さの把握、内容構造の整理、印刷要件の評価など、多くの場面で重要です。PDFリーダーでページ数を確認する方法に加えて、プログラムによって自動的に取得することも可能です。本記事では、C#を使用し、Spire.PDF for .NET を利用してPDFファイルのページ数を取得する方法を紹介します。

Spire.PDF for .NET のインストール

まず、.NETプロジェクトに Spire.PDF for .NET パッケージに含まれるDLLファイルを参照として追加する必要があります。これらのDLLファイルは、指定されたリンクからダウンロードするか、NuGet を使用してインストールできます。

PM> Install-Package Spire.PDF

C#でPDFファイルのページ数を取得する方法

Spire.PDF for .NET は、PdfDocument.Pages.Count プロパティを提供しており、PDFファイルを開かずにページ数を迅速に取得できます。具体的な手順は以下の通りです。

  1. PdfDocument オブジェクトを作成します
  2. PdfDocument.LoadFromFile() メソッドを使用して、サンプルPDFファイルを読み込みます
  3. PdfDocument.Pages.Count プロパティを使用して、PDFファイルのページ数を取得します
  4. 結果を出力し、PDFドキュメントを閉じます

サンプルコードは以下の通りです:

using Spire.Pdf;

namespace GetNumberOfPages
{
    class Program
    {
        static void Main(string[] args)
        {
            // PdfDocument オブジェクトを作成する
            PdfDocument pdf = new PdfDocument();

            // サンプルPDFファイルを読み込む
            pdf.LoadFromFile("Contract.pdf");

            // PDFファイルのページ数を取得する
            int PageNumber = pdf.Pages.Count;
            Console.WriteLine("このPDFファイルは合計 {0} ページです", PageNumber);

            // PDFドキュメントを閉じる
            pdf.Close();
        }
    }
}

一時ライセンスの申請

生成されたドキュメントから評価版のメッセージを削除したい場合、または機能制限を解除したい場合は、30日間有効の試用ライセンスを申請してください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?