在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PDF(可攜式文件格式)文件廣泛用於共享和展示文件,有時您可能需要將 PDF 拆分為多個文件。 無論您是想提取特定頁面、將大型文件分成較小的部分,還是為每個章節創建獨立的文件,拆分 PDF 在各種情境中都可能是一項有價值的任務。
在本文中,我們將學習如何使用 C# 分割 PDF。 C# 是一種多功能且強大的語言,並且有多個可用的庫使操控 PDF 相對簡單。 我們將探討以下兩個用於 C# 拆分 PDF 的程式庫。
首先安裝 iText7 庫
從輸入的 PDF 文件建立一個 PdfReader。
使用 PdfDocument 來處理 PDF 內容。
計算每個拆分檔案的頁數。
設定初始頁面範圍值。
使用迴圈處理每個分割檔案。
為當前拆分的文件創建一個新的 PdfDocument。
從原始文件中複製頁面到新文件。
更新下一次迭代的頁面範圍值。
將完成的輸出PDF保存。
重複直到所有檔案建立完成:
IronPDF是一個強大的 C# 程式庫,專為處理 PDF 文件而設計。 它提供的功能用于創建, 修改,和提取PDF 文件的內容。 開發者可以從頭生成 PDF、編輯現有的 PDF,並且合併或拆分他們。 此外,IronPDF 在將 HTML 內容轉換為 PDF 格式方面表現出色,非常適合生成報告或文檔。 IronPDF 支援數位簽章、安全功能和高品質輸出,簡化了 .NET 應用程式中的 PDF 相關任務。
iTextSharp (iText 7)是一個廣泛使用的庫,用於在.NET框架中處理PDF文件。 它提供了強大的功能,用於以程式方式創建、修改和提取PDF文件的內容。 開發人員可以使用 iTextSharp 向 PDF 添加文字、圖像、表格和其他圖形元素。 此外,它支持文件組裝、數字簽名,以及符合檔案和無障礙標準的規範。 起初作為 Java 庫,iTextSharp 被移植到 .NET 並擁有一個活躍的開發人員和用戶社群。
若要安裝IronPDF NuGet 套件在 Visual Studio 的套件管理器主控台中,按照以下步驟進行:
在 Visual Studio 中,依次選擇工具 -> NuGet 套件管理員 -> 套件管理控制台。
:ProductInstall
這將下載並安裝 IronPDF 套件及其相依套件到您的項目中。 安裝完成後,您可以在您的 C# 專案中開始使用 IronPDF 來進行 PDF 相關任務。
或者,您可以在 Visual Studio 中使用 NuGet 套件管理器安裝 IronPDF,或直接將套件新增到您的專案檔案中。另一個選擇是從官方網站並手動將其添加到您的專案中。 每個方法都提供了一種簡單的方法,將 IronPDF 集成到您的 C# 專案中,以實現與 PDF 有關的功能。
安裝iTextSharp在 Visual Studio 中使用 Package Manager Console,您可以按照以下步驟進行:
在 Visual Studio 中,依次選擇工具 -> NuGet 套件管理員 -> 套件管理控制台。
Install-Package itext7
這將下載並安裝iTextSharp套件及其相依性到您的專案中。 安裝完成後,您可以在您的C#專案中開始使用iTextSharp來處理PDF檔案。
我們可以使用IronPDF將一個PDF文件拆分為多個PDF文件。 這提供了一種簡單的實現方式。 以下程式碼將以來源 PDF 文件作為輸入,並將其分割成多個 PDF 文件。
static void Main(string [] args)
{
string file = "input.pdf"'
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
int firstPage = 1;
int lastPage = 2;
int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles;
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
// Copy multiple pages into a new document
PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage);
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
newSplitPDF.SaveAs(name);
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
static void Main(string [] args)
{
string file = "input.pdf"'
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
int firstPage = 1;
int lastPage = 2;
int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles;
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
// Copy multiple pages into a new document
PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage);
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
newSplitPDF.SaveAs(name);
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
此程式碼的目的是使用 IronPDF 程式庫將給定的 PDF 文件分割成多個較小的 PDF 文件。 SplitPdfUsingIronPDF 方法已定義以實現此功能。
inputPdfPath:表示輸入 PDF 文件路徑的字串(例如,「input.pdf」).
outputFolder:表示將拆分的 PDF 文件儲存位置的輸出資料夾字串(例如,「output_split」).
在 SplitPdfUsingIronPDF 方法中:
名為 sourceFile 的 PdfDocument 物件是通過從指定的 inputPdfPath 加載 PDF 創建的。
兩個整數變量,firstPage 和 lastPage,被初始化。 這些代表了拆分的頁面範圍。
totalPageInOneFile 是通過將源 PDF 的總頁數除以指定的 NumberOfSplitFiles 計算得出的。
一個迴圈從 1 遍歷到 NumberOfSplitFiles:
一個名為 newSplitPDF 的新 PdfDocument 對象已創建。
從第一頁到最後一頁的頁面(包容)從 sourceFile 複製到 newSplitPDF。
生成的小型 PDF 會以類似 "SplitPDF_IronPDF_1.pdf" 的檔名儲存。(對於第一次分割)在指定的輸出資料夾中。
您應該將 "input.pdf" 替換為實際的輸入 PDF 文件的路徑。確保您的項目中已正確安裝並引用了 IronPDF 庫。
輸出文件創建為:
現在,我們將使用iTextSharp將PDF文件分割成多個PDF文件。 以下程式碼將以原始檔案作為輸入,並將該 PDF 文件拆分為多個較小的檔案。
static void Main(string [] args)
{
string inputPath = "input.pdf";
// Output PDF files path (prefix for the generated files)
string outputPath = "output_split";
int NumberOfSplitFiles = 3;
// Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles);
}
static void SplitPdfUsingiTextSharp(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
using (PdfReader Reader = new PdfReader(inputPdfPath))
{
using (PdfDocument doc = new PdfDocument(Reader))
{
int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile; // int pagenumber
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
{
//pdfDocument.get
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
static void Main(string [] args)
{
string inputPath = "input.pdf";
// Output PDF files path (prefix for the generated files)
string outputPath = "output_split";
int NumberOfSplitFiles = 3;
// Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles);
}
static void SplitPdfUsingiTextSharp(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
{
using (PdfReader Reader = new PdfReader(inputPdfPath))
{
using (PdfDocument doc = new PdfDocument(Reader))
{
int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile; // int pagenumber
for (int i = 1; i <= NumberOfSplitFiles; i++)
{
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
{
//pdfDocument.get
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
上述程式碼演示了如何使用 iTextSharp 將大型 PDF 文件拆分為較小的部分。每個較小的 PDF 文件將包含原始文件的一部分。
主要功能封裝在 SplitPdfUsingiTextSharp 方法中。
在 SplitPdfUsingiTextSharp 方法中:
一個名為 Reader 的 PdfReader 物件是透過從指定的 inputPdfPath 加載 PDF 文件來創建的。
一個名為 DOC 的 PdfDocument 物件被使用 Reader 初始化。
一個迴圈從 1 遍歷到 NumberOfSplitFiles:
一個新的較小 PDF 檔案已創建,文件名類似於 "SplitPDF_iTextSharp_1.pdf"。(對於第一次分割)在指定的輸出資料夾中。
在這個新的 PDF 文件中(pdf文件),頁面是從原始文件複製的:
firstPage 到 lastPage(包容)被複製。
較小的 PDF 已保存。
確保在您的專案中正確安裝並引用了 iTextSharp 庫。 將 "input.pdf" 替換為您輸入 PDF 文件的實際路徑。
為了比較使用 iTextSharp 和 IronPDF 的兩種分割方法,我們基於幾個因素來評估它們:
易於使用:
iTextSharp: iTextSharp 方法涉及建立 PdfReader、PdfDocument 和 PdfWriter。 它計算頁數範圍並將頁面複製到新文檔。 此方法需要了解iTextSharp API。
程式碼可讀性:
iTextSharp:代碼涉及更多步驟,使其稍微長一些,可能更難以閱讀。
效能:
iTextSharp:重複創建和處置PdfDocument實例可能會影響性能。
記憶體使用量:
iTextSharp: 建立多個 PdfDocument 實例將消耗更多記憶體。
總結而言,iTextSharp 和 IronPDF 都提供了穩健的解決方案來用 C# 分割 PDF 文件,各有其優勢。 IronPDF 因其簡單性、可讀性以及由於更直接的方法可能帶來的更佳效能而脫穎而出。
尋求多功能性與易用性之間平衡的開發人員可能會發現IronPDF是一個具有吸引力的選擇。此外,IronPDF提供一個免費試用. 最終,選擇 iTextSharp 或 IronPDF 取決於個別專案需求和偏好的開發風格。