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

C# で PDF のページ構造と内容を柔軟に制御する

Posted at

日常の文書処理では、PDF ファイルのページ管理はよくあるニーズです。たとえば、レポート作成時に補足説明のために新しいページを挿入したり、資料を整理する際にページを削除・並べ替えたり、特定のページを新しいファイルに抽出する場合があります。C# を用いることで、API を活用して PDF ページを柔軟に操作でき、作業効率を向上させることが可能です。

本記事では、1つのプログラム内で以下の操作を実現する方法を紹介します。

  • ページの追加・挿入
  • 指定ページの削除
  • ページのコピー・移動
  • ページ順序の変更
  • ページの新しい PDF への抽出
  • 別文書へのページコピー

本記事のサンプルでは Free Spire.PDF for .NET を使用します。NuGet から以下のコマンドでインストール可能です:

Install-Package FreeSpire.PDF

サンプルコード

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

class Program
{
    static void Main()
    {
        // 1. PDF ドキュメントの作成と読み込み
        PdfDocument doc = new PdfDocument();

        // 初期ページを追加しタイトルを書き込む
        PdfPageBase initPage = doc.Pages.Add();
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("微软雅黑", 14f), true);
        initPage.Canvas.DrawString("元の1ページ目", font, PdfBrushes.Black, 50, 50);

        // 2. 文書末尾に新しいページを追加
        PdfPageBase addedPage = doc.Pages.Add();
        addedPage.Canvas.DrawString("末尾に追加したページ", font, PdfBrushes.DarkBlue, 50, 50);

        // 3. 文書先頭にページを挿入
        PdfPageBase insertedPage = doc.Pages.Insert(0);
        insertedPage.Canvas.DrawString("先頭に挿入したページ", font, PdfBrushes.Green, 50, 50);

        // 4. 2ページ目を削除
        doc.Pages.RemoveAt(1);

        // 5. 1ページ目を末尾にコピー
        PdfPageBase copySource = doc.Pages[0];
        doc.InsertPage(doc, copySource);

        // 6. 最後のページを先頭に移動
        PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];
        PdfPageBase newPage = doc.Pages.Insert(0, lastPage.Size);
        newPage.Canvas.DrawTemplate(lastPage.CreateTemplate(), PointF.Empty);
        doc.Pages.RemoveAt(doc.Pages.Count - 1);

        // 7. 前2ページを新しい PDF に抽出
        PdfDocument extractedDoc = new PdfDocument();
        extractedDoc.InsertPage(doc, 0, 0);
        extractedDoc.InsertPage(doc, 1, 1);
        extractedDoc.SaveToFile("ExtractedPages.pdf");

        // 8. ページを他の文書にコピー
        PdfDocument targetDoc = new PdfDocument();
        targetDoc.InsertPage(doc, 0, 0);
        PdfPageBase targetPage = targetDoc.Pages[0];
        targetPage.Canvas.DrawString("元文書からコピーしたページ",
            new PdfTrueTypeFont(new Font("微软雅黑", 12f), true), PdfBrushes.Red, 50, 80);
        targetDoc.SaveToFile("CopiedToNewDoc.pdf");

        // 元の文書を保存
        doc.SaveToFile("PageOperations.pdf");
        doc.Close();
    }
}

プログラム実行後、以下のファイルが生成されます:

  • PageOperations.pdf:ページの追加・挿入・削除・コピー・移動などの操作結果を含む
  • ExtractedPages.pdf:抽出した前2ページのみ
  • CopiedToNewDoc.pdf:別文書にコピーして生成したファイル

ファイルプレビュー:

PageOperations.pdf:

PageOperations.pdf

ExtractedPages.pdf:

ExtractedPages.pdf

CopiedToNewDoc.pdf:

CopiedToNewDoc.pdf


主要クラスとメソッドの解説

1. PdfDocument クラス

PDF 文書オブジェクトを表し、ページ操作の入口となるクラスです。
主なメソッド:

  • Pages.Add():末尾に新しいページを追加
  • Pages.Insert(int index):指定位置にページを挿入
  • Pages.RemoveAt(int index):指定位置のページを削除
  • InsertPage(PdfDocument srcDoc, int srcIndex, int destIndex):別文書からページを挿入
  • SaveToFile(string filename):ファイル保存

2. PdfPageBase クラス

文書内の個々のページを表します。
主なプロパティ・メソッド:

  • Canvas:描画領域を取得、ページにコンテンツを描画可能
  • Canvas.DrawString(string text, Font font, PdfBrush brush, float x, float y):文字列を書き込む

まとめ

本記事のサンプルを通じて、C# プログラム内で PDF ページの 追加・挿入・削除・コピー・移動・抽出・別文書へのコピー といった一般的な操作を実装できました。これらの手法を活用することで、PDF 文書の構造や内容を柔軟に管理することが可能です。

さらに詳しい PDF 文書処理の技術は、Spire.PDF for .NET 公式ドキュメント を参照してください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?