2
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#で1つのPDFページを複数のページに分割する

Posted at

1つのPDFページに異なるトピックや章、節など複数の種類のコンテンツが含まれている場合、ページを分割することでコンテンツを分類・整理することができます。この記事では、C#を使ってPDFページを水平または垂直に2つ以上のページに分割する方法を紹介します。

無料の.NET PDFライブラリ

この記事で使用しているPDFコンポーネントは Free Spire.PDF for .NETです。以下のリンクからPDFライブラリをダウンロードし、参照としてSpire.Pdf.dllを手動で追加することができます。または、Nuget経由で直接インストールすることもできます。

C#でPDFページを水平に分割する

主な手順:

  1. 元のPDF文書を読み込み、分割するページを取得します。
  2. 新しい PDF 文書を作成し、そのページの余白を 0 に設定します。
  3. 新しい文書の高さを元の文書の半分に設定します。
  4. PdfDocument.Pages.Add() メ ソ ッ ド を使っ て新 し い PDF 文書にページ を追加 し ます。
  5. PdfPageBase.CreateTemplate() メ ソ ッ ド を使っ て、 元の PDF ページに基づ く テ ンプ レー ト を作成 し ます。
  6. PdfTemplate.Draw() メ ソ ッ ド を使っ て、 元の PDF ページの内容を新 し いページに描 き ます。
  7. 結果文書を保存します。
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace SplitPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // PDF文書を読み込む
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("鳥.pdf");

            // 最初のページを見る
            PdfPageBase page = pdf.Pages[0];

            // 新しいPDF文書を作成する
            PdfDocument newPdf = new PdfDocument();

            // 新規作成したPDF文書から余白を削除
            newPdf.PageSettings.Margins.All = 0;

            // 新しいPDFのページ幅を元のPDF文書と同じに設定し、高さを元のPDFページの半分に設定する。
            newPdf.PageSettings.Width = page.Size.Width;
            newPdf.PageSettings.Height = page.Size.Height / 2;

            // 新しいPDF文書に新しいページを追加する
            PdfPageBase newPage = newPdf.Pages.Add();

            // テキストレイアウトフォーマットの設定
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            // 元のPDF文書の最初のページの内容を、新しく作成されたPDFのページに描画します
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            // ドキュメントを保存する
            newPdf.SaveToFile("HorizontallySplit.pdf");
            newPdf.Close();
            pdf.Close();
        }
    }
}

HorizontallySplitPDF.png

C#でPDFページを縦に分割する

PDFのページを垂直に分割する手順は、新しい文書の幅を元の文書の半分に設定する必要があることを除いて、水平分割の手順と似ています。

サンプルコード:

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

namespace SplitPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // PDF文書を読み込む
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("鳥.pdf");

            // 最初のページを見る
            PdfPageBase page = pdf.Pages[0];

            // 新しいPDF文書を作成する
            PdfDocument newPdf = new PdfDocument();

            // 新規作成したPDF文書から余白を削除
            newPdf.PageSettings.Margins.All = 0;

            // 新しいPDFのページ幅を元のPDF文書の半分に設定し、高さを元のPDFページと同じに設定します
            newPdf.PageSettings.Width = page.Size.Width / 2;
            newPdf.PageSettings.Height = page.Size.Height;

            // 新しいPDF文書に新しいページを追加する
            PdfPageBase newPage = newPdf.Pages.Add();

            // テキストレイアウトフォーマットの設定
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            // 元のPDF文書の最初のページの内容を、新しく作成されたPDFのページに描画します
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            // ドキュメントを保存する
            newPdf.SaveToFile("VerticallySplit.pdf");
            newPdf.Close();
            pdf.Close();
        }
    }
}

VerticallySplitPDF.png

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