使用 C# 將多頁 PDF 分割為單頁文件
IronPDF 讓您能夠使用 CopyPage 方法,將多頁 PDF 文件拆分為獨立的單頁 PDF 文件。 透過此方法,開發人員只需幾行程式碼,即可遍歷每個頁面並將其儲存為獨立檔案,節省了大量時間。 無論您處理的是掃描文件、報告,還是任何多頁面的 PDF 檔案,IronPDF 都能為文件管理與處理任務提供高效的解決方案。
PDF 分割功能在您需要將個別頁面分發給不同收件者、分別處理各頁面,或與需要單頁輸入的文件管理系統整合時,特別實用。 IronPDF 強大的 Chrome 渲染引擎可確保分割後的頁面能完整保留原始格式、圖片及文字品質。
快速入門:將多頁 PDF 分割為單頁
立即開始使用 IronPDF,將多頁 PDF 分割成單頁文件。 透過使用 CopyPage 方法,您可以高效地遍歷 PDF 的每一頁,並將其儲存為獨立檔案。 此簡化流程非常適合尋求快速且可靠解決方案來管理 PDF 文件的開發人員。 首先,請確認您已透過 NuGet 安裝 IronPDF。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf
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 檔案?
為何要使用 CopyPage 方法來分割 PDF?
CopyPage 既然您已擁有 IronPDF,現在可以將多頁文件拆分為單頁文件檔案。 分割多頁 PDF 的概念,涉及使用 CopyPage 或 CopyPages 方法複製單一或多頁內容。 這些方法會建立新的 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?
雖然 CopyPage 非常適合單頁擷取,但 IronPDF 也提供 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 更為高效,因為它僅需一次呼叫即可完成操作。 若需全面的 PDF 處理功能,您可以將分割與合併操作結合使用,以建立複雜的文件工作流程。
準備好探索更多可能性了嗎? 請點此查看我們的教學頁面:整理 PDF 檔案。 您亦可探索如何在分割後的 PDF 檔案中加入頁碼,或瞭解如何管理 PDF 元資料以優化文件管理工作流程。 如需進階的 PDF 處理技術,請參閱我們的完整 API 參考指南。
常見問題
如何在 C# 中將多頁 PDF 分割成獨立的單頁 PDF 檔案?
您可以使用 IronPDF 的 CopyPage 方法分割多頁 PDF 檔案。只需載入您的 PDF 文件,使用 for 迴圈遍歷每一頁,並將每頁另存為獨立檔案。IronPDF 讓這個過程變得簡單直觀,僅需幾行程式碼即可完成,同時完整保留原始的格式與品質。
我該使用什麼方法從 PDF 中擷取單一頁面?
IronPDF 提供 CopyPage 方法,用於從 PDF 文件中擷取單一頁面。此方法會建立指定頁面的精確複本,作為新的 PdfDocument 實例,並完整保留原始文件中的所有格式、註解及互動元素。
分割 PDF 檔案是否能維持原始的格式與品質?
是的,當您使用 IronPDF 的 CopyPage 方法分割 PDF 時,所有視覺元素、文字格式、嵌入資源及互動元素都會被完整保留。IronPDF 的 Chrome 渲染引擎可確保分割後的頁面維持原始格式、圖片及文字品質。
我可以一次分割多頁,而不是一次只分割一頁嗎?
是的,IronPDF 同時提供用於單一頁面的 CopyPage 以及用於多頁面的 CopyPages。CopyPages 方法可讓您一次將多頁內容擷取至新的 PdfDocument 實例中,為各種分割情境提供靈活性。
分割 PDF 文件的常見應用情境有哪些?
IronPDF 的拆分功能非常適合將單一頁面分發給不同收件者、分別處理各頁面、與需要單頁輸入的文件管理系統整合,以及處理法律文件、發票或存檔記錄等文件完整性至關重要的情境。

