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

More than 1 year has passed since last update.

.NETでPDFのページを削除

Last updated at Posted at 2023-11-03

PDF文書は、テキスト、画像やその他の要素を提示するためにページを通して、PDF文書は、通常、複数のページを持っています。 特にネットワークから取得したPDF文書には、空白のページや不要なページ、無関係なページが存在することがあります。 この記事では、.NET Frameworkを使用してPDFページを削除する方法をお教えします。

.NET標準ライブラリに加えて、この記事で説明するアプローチでは、無料のAPIを使用する必要があります。このAPIは、Free Spire.PDF for .NETのウェブサイトからダウンロードするか、NuGetコマンドを介してインストールすることができます:

PM> Install-Package FreeSpire.PDF

PDF文書からページを削除

このAPIの PdfDocument.Pages.RemoveAt() メソッドを使用して、指定されたページ内のPDFファイルをページパラメータ(0 から開始)を通して削除できます。サンプルコードと主な手順があります。

  1. PdfDocument のオブジェクトを作成します。
  2. PdfDocument.LoadFromFile() メソッドを使用して PDF 文書を読み込みます。
  3. PdfDocument.Pages.RemoveAt() メソッドで3ページ目と2ページ目を削除します。
  4. PdfDocument.SaveToFile() メソッドでPDF文書を保存します。

コード
C#

using System;
using Spire.Pdf;

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

            // PDF文書を読み込む
            string input = "サンプル.pdf";
            pdf.LoadFromFile(input);

            // 3ページ目と2ページ目を削除する
            pdf.Pages.RemoveAt(1);
            pdf.Pages.RemoveAt(2);

            // ファイルを保存する
            string output = "output/ページ削除.pdf";
            pdf.SaveToFile(output);
        }
    }
}

PDF文書からページを削除

この記事では、.NET Frameworkを使用してPDF文書からページを削除する方法について説明します。 この記事では、API Free Spire.PDF for .NETを使用しています。他にも多くのPDF操作機能がありますので、Spire.PDF for .NETチュートリアルで紹介されている操作を見てください。

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