C#で複数ページのPDFを単一ページのドキュメントに分割する
IronPDF を使用すると、CopyPage メソッドを利用して、複数ページの PDF 文書を個別の 1 ページ PDF に分割することができます。 このアプローチにより、開発者は各ページを繰り返し読み、わずか数行のコードで別々のファイルとして保存することができます。 スキャンした文書やレポート、複数ページのPDFを扱う場合でも、IronPDFは文書管理と処理タスクのための効率的なソリューションを提供します。
PDFの分割機能は、個々のページを異なる受信者に配布したり、ページを個別に処理したり、単一ページの入力を必要とする文書管理システムと統合したりする必要がある場合に特に便利です。 IronPDFの堅牢なChromeレンダリングエンジンは、分割されたページが元のフォーマット、画像、テキストの品質を維持することを保証します。
クイックスタート: 複数ページの PDF を単一ページに分割する
IronPDFを使って、複数ページのPDFを単一ページのドキュメントに分割することができます。 CopyPage メソッドを利用することで、PDFの各ページを効率的に順次処理し、個別のファイルとして保存することができます。 この合理化されたプロセスは、PDF文書を管理するための高速で信頼性の高いソリューションを求める開発者に最適です。 まず、NuGet経由でIronPDFをインストールしていることを確認してください。
-
IronPDF をNuGetパッケージマネージャでインストール
PM > Install-Package IronPdf -
このコード スニペットをコピーして実行します。
var pdf = new IronPdf.PdfDocument("multipage.pdf"); for (int i = 0; i < pdf.PageCount; i++) { var singlePagePdf = pdf.CopyPage(i); singlePagePdf.SaveAs($"page_{i + 1}.pdf"); } -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronPDF を使い始めましょう無料トライアル
複数ページのPDFを分割するにはどうすればよいですか?
PDF分割にCopyPageメソッドを使用する理由とは?
CopyPage IronPDF があれば、複数ページのドキュメントを単一ページのドキュメントファイルに分割することができます。 複数ページからなるPDFを分割するという考え方は、CopyPagesメソッドを使用して、1ページまたは複数のページをコピーすることを含みます。 これらのメソッドは、指定されたページのみを含む新しい PdfDocument インスタンスを作成し、元のドキュメントのすべての書式、注釈、およびインタラクティブな要素を保持します。
CopyPages CopyPage メソッドは、IronPDFにおけるPDF分割操作の基盤となるものです。 複雑な操作を必要としたり、データ損失のリスクを伴う他の手法とは異なり、CopyPageは指定されたページの完全な複製を作成し、すべての視覚的要素、テキストの書式、および埋め込まれたリソースを維持します。 このため、法的文書、請求書、アーカイブ記録など、文書の完全性が重要なシナリオに最適です。
各ページを分割する手順は何ですか?
CopyPages
より高度なシナリオの場合、エラー処理を実装し、出力形式をカスタマイズすることもできます。 以下は、バリデーションとカスタムネーミングを含む包括的な例です:
using IronPdf;
using System;
using System.IO;
public class PdfSplitter
{
public static void SplitPdfWithValidation(string inputPath, string outputDirectory)
{
try
{
// Validate input file exists
if (!File.Exists(inputPath))
{
throw new FileNotFoundException("Input PDF file not found.", inputPath);
}
// Create output directory if it doesn't exist
Directory.CreateDirectory(outputDirectory);
// Load the PDF document
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Get the file name without extension for naming split files
string baseFileName = Path.GetFileNameWithoutExtension(inputPath);
Console.WriteLine($"Splitting {pdf.PageCount} pages from {baseFileName}...");
for (int idx = 0; idx < pdf.PageCount; idx++)
{
// Copy individual page
PdfDocument singlePagePdf = pdf.CopyPage(idx);
// Create descriptive filename with zero-padding for proper sorting
string pageNumber = (idx + 1).ToString().PadLeft(3, '0');
string outputPath = Path.Combine(outputDirectory, $"{baseFileName}_Page_{pageNumber}.pdf");
// Save the single page PDF
singlePagePdf.SaveAs(outputPath);
Console.WriteLine($"Created: {outputPath}");
}
Console.WriteLine("PDF splitting completed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error splitting PDF: {ex.Message}");
throw;
}
}
}
using IronPdf;
using System;
using System.IO;
public class PdfSplitter
{
public static void SplitPdfWithValidation(string inputPath, string outputDirectory)
{
try
{
// Validate input file exists
if (!File.Exists(inputPath))
{
throw new FileNotFoundException("Input PDF file not found.", inputPath);
}
// Create output directory if it doesn't exist
Directory.CreateDirectory(outputDirectory);
// Load the PDF document
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Get the file name without extension for naming split files
string baseFileName = Path.GetFileNameWithoutExtension(inputPath);
Console.WriteLine($"Splitting {pdf.PageCount} pages from {baseFileName}...");
for (int idx = 0; idx < pdf.PageCount; idx++)
{
// Copy individual page
PdfDocument singlePagePdf = pdf.CopyPage(idx);
// Create descriptive filename with zero-padding for proper sorting
string pageNumber = (idx + 1).ToString().PadLeft(3, '0');
string outputPath = Path.Combine(outputDirectory, $"{baseFileName}_Page_{pageNumber}.pdf");
// Save the single page PDF
singlePagePdf.SaveAs(outputPath);
Console.WriteLine($"Created: {outputPath}");
}
Console.WriteLine("PDF splitting completed successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error splitting PDF: {ex.Message}");
throw;
}
}
}
Imports IronPdf
Imports System
Imports System.IO
Public Class PdfSplitter
Public Shared Sub SplitPdfWithValidation(inputPath As String, outputDirectory As String)
Try
' Validate input file exists
If Not File.Exists(inputPath) Then
Throw New FileNotFoundException("Input PDF file not found.", inputPath)
End If
' Create output directory if it doesn't exist
Directory.CreateDirectory(outputDirectory)
' Load the PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile(inputPath)
' Get the file name without extension for naming split files
Dim baseFileName As String = Path.GetFileNameWithoutExtension(inputPath)
Console.WriteLine($"Splitting {pdf.PageCount} pages from {baseFileName}...")
For idx As Integer = 0 To pdf.PageCount - 1
' Copy individual page
Dim singlePagePdf As PdfDocument = pdf.CopyPage(idx)
' Create descriptive filename with zero-padding for proper sorting
Dim pageNumber As String = (idx + 1).ToString().PadLeft(3, "0"c)
Dim outputPath As String = Path.Combine(outputDirectory, $"{baseFileName}_Page_{pageNumber}.pdf")
' Save the single page PDF
singlePagePdf.SaveAs(outputPath)
Console.WriteLine($"Created: {outputPath}")
Next
Console.WriteLine("PDF splitting completed successfully!")
Catch ex As Exception
Console.WriteLine($"Error splitting PDF: {ex.Message}")
Throw
End Try
End Sub
End Class
ページ反復はどのように機能しますか?
CopyPage 上記のコードを見ると、for ループを使用して現在の PDF ドキュメントのページを反復処理し、その後 CopyPage メソッドを使用して各ページを新しい PdfDocument オブジェクトにコピーしていることがわかります。 最終的に、各ページは順番に名前を付けられた新しいドキュメントとしてエクスポートされます。 IronPDFは複雑なPDF構造の操作をすべて内部で処理するため、反復プロセスは簡単で効率的です。
PageCount プロパティは、ドキュメントの総ページ数を返します。これにより、インデックスの範囲外エラーのリスクを冒すことなく、安全に反復処理を行うことができます。 各反復は完全に独立したPDFドキュメントを作成します。つまり、元のドキュメントや他の分割ページに影響を与えることなく、各ページを個別に処理、修正、配布することができます。 このアプローチは、特定のページを抜き出したり、ページを並行して処理する必要があるような大規模なドキュメントを扱う場合に特に有益です。
CopyPages はいつ CopyPage の代わりに使用すべきですか?
CopyPagesメソッドも提供しています。 これは、個々のページではなく、特定のページ範囲でPDF文書を作成したい場合に特に役立ちます:
using IronPdf;
using System.Collections.Generic;
public class MultiPageExtraction
{
public static void ExtractPageRanges(string inputPath)
{
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Extract pages 1-5 (0-indexed, so pages 0-4)
List<int> firstChapter = new List<int> { 0, 1, 2, 3, 4 };
PdfDocument chapterOne = pdf.CopyPages(firstChapter);
chapterOne.SaveAs("Chapter_1.pdf");
// Extract every other page (odd pages)
List<int> oddPages = new List<int>();
for (int i = 0; i < pdf.PageCount; i += 2)
{
oddPages.Add(i);
}
PdfDocument oddPagesDoc = pdf.CopyPages(oddPages);
oddPagesDoc.SaveAs("Odd_Pages.pdf");
// Extract specific non-consecutive pages
List<int> selectedPages = new List<int> { 0, 4, 9, 14 }; // Pages 1, 5, 10, 15
PdfDocument customSelection = pdf.CopyPages(selectedPages);
customSelection.SaveAs("Selected_Pages.pdf");
}
}
using IronPdf;
using System.Collections.Generic;
public class MultiPageExtraction
{
public static void ExtractPageRanges(string inputPath)
{
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Extract pages 1-5 (0-indexed, so pages 0-4)
List<int> firstChapter = new List<int> { 0, 1, 2, 3, 4 };
PdfDocument chapterOne = pdf.CopyPages(firstChapter);
chapterOne.SaveAs("Chapter_1.pdf");
// Extract every other page (odd pages)
List<int> oddPages = new List<int>();
for (int i = 0; i < pdf.PageCount; i += 2)
{
oddPages.Add(i);
}
PdfDocument oddPagesDoc = pdf.CopyPages(oddPages);
oddPagesDoc.SaveAs("Odd_Pages.pdf");
// Extract specific non-consecutive pages
List<int> selectedPages = new List<int> { 0, 4, 9, 14 }; // Pages 1, 5, 10, 15
PdfDocument customSelection = pdf.CopyPages(selectedPages);
customSelection.SaveAs("Selected_Pages.pdf");
}
}
Imports IronPdf
Imports System.Collections.Generic
Public Class MultiPageExtraction
Public Shared Sub ExtractPageRanges(inputPath As String)
Dim pdf As PdfDocument = PdfDocument.FromFile(inputPath)
' Extract pages 1-5 (0-indexed, so pages 0-4)
Dim firstChapter As New List(Of Integer) From {0, 1, 2, 3, 4}
Dim chapterOne As PdfDocument = pdf.CopyPages(firstChapter)
chapterOne.SaveAs("Chapter_1.pdf")
' Extract every other page (odd pages)
Dim oddPages As New List(Of Integer)()
For i As Integer = 0 To pdf.PageCount - 1 Step 2
oddPages.Add(i)
Next
Dim oddPagesDoc As PdfDocument = pdf.CopyPages(oddPages)
oddPagesDoc.SaveAs("Odd_Pages.pdf")
' Extract specific non-consecutive pages
Dim selectedPages As New List(Of Integer) From {0, 4, 9, 14} ' Pages 1, 5, 10, 15
Dim customSelection As PdfDocument = pdf.CopyPages(selectedPages)
customSelection.SaveAs("Selected_Pages.pdf")
End Sub
End Class
CopyPages メソッドは、カスタムコンパイルの作成、特定のセクションの抽出、またはドキュメントコンテンツの再編成に最適です。 また、数ページ分が必要な場合に CopyPage を複数回呼び出すよりも効率的です。なぜなら、1回の呼び出しで処理を完了できるからです。 包括的なPDF操作機能のために、分割とマージ操作を組み合わせて、洗練されたドキュメントワークフローを作成することができます。
次に何ができるのかを見てみましょうか? こちらのチュートリアルページをご覧ください:PDFを整理する. また、分割した PDF にページ番号を追加する方法や、文書管理ワークフローを強化するための PDF メタデータの管理についても説明します。 高度なPDF操作技術については、当社の包括的なAPIリファレンスをご覧ください。
よくある質問
C#で複数ページのPDFを個々の単一ページのPDFに分割するには?
IronPDFのCopyPageメソッドを使って複数ページのPDFを分割することができます。PDFドキュメントを読み込み、forループを使って各ページを繰り返し処理し、各ページを別々のファイルとして保存するだけです。IronPDFは数行のコードでこのプロセスを簡単にし、オリジナルのフォーマットと品質を維持します。
PDFから個々のページを抽出するには、どのような方法を使用すればよいですか?
IronPDFはPDFドキュメントから個々のページを抽出するCopyPageメソッドを提供します。このメソッドは、指定されたページの完全な複製を新しいPdfDocumentインスタンスとして作成し、元のドキュメントからのすべての書式、注釈、インタラクティブ要素を保持します。
PDFを分割しても、元の書式や品質は維持されますか?
IronPDFのCopyPageメソッドを使ってPDFを分割した場合、すべてのビジュアル要素、テキストフォーマット、埋め込みリソース、インタラクティブ要素は保持されます。IronPDFのChromeレンダリングエンジンは分割されたページが元の書式、画像、テキストの品質を維持することを保証します。
一度に1ページではなく、複数のページを一度に分割することはできますか?
はい、IronPDFは単一ページ用のCopyPageと複数ページ用のCopyPagesの両方を提供しています。CopyPagesメソッドにより、一度に複数のページを新しいPdfDocumentインスタンスに抽出することができます。
PDF文書を分割する一般的な使用例とは?
IronPDFの分割機能は、個々のページを異なる受信者に配布したり、ページを別々に処理したり、単一ページの入力を必要とする文書管理システムと統合したり、文書の整合性が重要な法的文書や請求書、アーカイブされた記録を扱ったりするのに理想的です。

