產品比較 在C#中拆分PDF的比較iTextSharp與IronPDF之間 Curtis Chau 更新:2025年10月16日 下載 IronPDF NuGet 下載 DLL 下載 Windows Installer 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 Full Comparison Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing. View Full Comparison PDF(便攜式文檔格式)文件廣泛用於共用和簡報文檔,有時您可能需要將一個 PDF 文件分割成多個文件。 無論您是想提取特定頁面、將大型文件分成較小的部分,還是為每個章節創建單獨的文件,在各種情況下,拆分 PDF 都是一項有價值的任務。 本文將介紹如何使用 C# 分割 PDF 檔案。 C# 是一種功能強大且用途廣泛的語言,並且有多種庫可供使用,使得操作 PDF 文件變得相對簡單。 我們將探索以下兩個用於在 C# 中分割 PDF 的程式庫。 iTextSharp IronPDF 如何使用 iTextSharp 在 C# 分割 PDF 首先,安裝 iText7 函式庫。 從輸入的 PDF 檔案建立 PdfReader。 使用 PdfDocument 處理 PDF 內容。 計算每個拆分文件的頁數。 設定初始頁面範圍值。 使用循環處理每個分割的檔案。 為目前拆分的檔案建立一個新的 PdfDocument。 將原文檔中的頁面複製到新文件中。 更新下次迭代的頁面範圍值。 儲存完成的輸出 PDF 檔案。 重複以上步驟,直到建立所有檔案。 對指定數量的拆分檔案重複此程序。 IronPDF IronPDF是一個功能強大的 C# 庫,專為處理 PDF 文件而設計。 它提供了創建、修改和提取PDF 文件內容的功能。 開發人員可以從頭開始產生 PDF,編輯現有 PDF,以及合併或分割它們。 此外, IronPDF在將 HTML 內容轉換為 PDF 格式方面表現出色,因此可用於產生報告或文件。 IronPDF支援數位簽章、安全功能和高品質輸出,簡化了.NET應用程式中與 PDF 相關的任務。 iTextSharp iTextSharp (iText 7)是.NET Framework中廣泛使用的用於處理 PDF 檔案的函式庫。 它提供了強大的功能,可以透過程式設計方式建立、修改和提取 PDF 文件中的內容。 開發人員可以使用 iTextSharp 為 PDF 添加文字、圖像、表格和其他圖形元素。 此外,它還支援文件組裝、數位簽名,並符合歸檔和無障礙標準。 iTextSharp 最初是一個 Java 函式庫,後來移植到.NET ,並擁有活躍的開發者和使用者社群。 安裝IronPDF庫 若要使用 Visual Studio 中的套件管理器控制台安裝IronPDF NuGet程式包,請依照下列步驟操作: 在 Visual Studio 中,前往"工具"-> "NuGet套件管理器"->"套件管理器控制台"。 使用以下指令安裝IronPDF NuGet套件: Install-Package IronPdf 這將下載IronPDF軟體包及其相依性並將其安裝到您的專案中。 安裝完成後,您就可以在 C# 專案中使用IronPDF來處理與 PDF 相關的任務了。 或者,您可以使用 Visual Studio 中的NuGet套件管理器安裝IronPDF ,或直接將套件新增至專案文件。另一種方法是從官方網站下載套件並手動將其添加到專案中。 每種方法都提供了一種直接的方式,可以將IronPDF整合到您的 C# 專案中,以實現與 PDF 相關的功能。 安裝 iTextSharp 函式庫 若要在 Visual Studio 中使用套件管理器控制台安裝iTextSharp ,您可以依照下列步驟操作: 在 Visual Studio 中,前往"工具"-> "NuGet套件管理器"->"套件管理器控制台"。 使用下列指令安裝 iTextSharp NuGet套件: Install-Package itext7 Install-Package itext7 SHELL 這將下載 iTextSharp 軟體包及其相依性並將其安裝到您的專案中。 安裝完成後,您就可以在 C# 專案中使用 iTextSharp 來處理 PDF 檔案了。 使用IronPDF在 C# 中拆分 PDF 文檔 我們可以使用IronPDF將一個 PDF 檔案拆分成多個 PDF 檔案。 它提供了一種實現這一目標的簡單方法。 以下程式碼將以來源 PDF 檔案作為輸入,並將其拆分為多個 PDF 檔案。 using IronPdf; class Program { static void Main(string[] args) { string file = "input.pdf"; // The folder to save the split PDFs string outputFolder = "output_split"; int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into // Call the SplitPdf method to split the PDF SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles); } static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int numberOfSplitFiles) { // Load the input PDF PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath); // Initialize page range values 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); // Generate the output file path string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf"; // Save the new split PDF newSplitPDF.SaveAs(name); // Update page range values for the next iteration firstPage = lastPage + 1; lastPage += totalPageInOneFile; } } } using IronPdf; class Program { static void Main(string[] args) { string file = "input.pdf"; // The folder to save the split PDFs string outputFolder = "output_split"; int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into // Call the SplitPdf method to split the PDF SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles); } static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int numberOfSplitFiles) { // Load the input PDF PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath); // Initialize page range values 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); // Generate the output file path string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf"; // Save the new split PDF newSplitPDF.SaveAs(name); // Update page range values for the next iteration firstPage = lastPage + 1; lastPage += totalPageInOneFile; } } } $vbLabelText $csharpLabel 程式碼解釋 這段程式碼的目的是使用IronPDF庫將給定的 PDF 檔案拆分成多個較小的 PDF 檔案。 定義了 SplitPdfUsingIronPDF 方法來實現此功能。 方法參數 inputPdfPath: 表示輸入 PDF 檔案路徑的字串(例如,"input.pdf")。 outputFolder: 表示將保存拆分後的 PDF 檔案的輸出資料夾的字串(例如,"output_split")。 numberOfSplitFiles: 一個整數,表示原始 PDF 檔案將被拆分成多少個較小的 PDF 檔案。 分割過程 在 SplitPdfUsingIronPDF 方法內部: 透過從指定的 inputPdfPath 載入 PDF,建立名為 PdfDocument 的物件。 初始化兩個整數變數 firstPage 和 lastPage。 這些代表要拆分的頁面範圍。 totalPageInOneFile 是將來源 PDF 的總頁數除以指定的 numberOfSplitFiles 來計算的。 循環從 1 迭代到 numberOfSplitFiles: 建立了一個名為 newSplitPDF 的新 PdfDocument 物件。 從 firstPage 到 lastPage(含)的頁是從 sourceFile 到 newSplitPDF 複製的。 產生的較小 PDF 檔案將以類似"SplitPDF_IronPDF_1.pdf"(第一次分割)的檔案名稱保存在指定的輸出資料夾中。 下次迭代將更新 firstPage 和 lastPage 的值。 筆記 您應該將"input.pdf"替換為您的輸入PDF文件的實際路徑。請確保IronPDF庫已正確安裝並在您的專案中引用。 輸出檔案建立如下: 使用 iTextSharp 在 C# 中拆分 PDF 現在,我們將使用 iTextSharp 將我們的 PDF 文件拆分成多個 PDF 文件。 以下程式碼將以來源檔案為輸入,並將該 PDF 文件拆分為多個較小的檔案。 using System; using iText.Kernel.Pdf; class Program { 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; // Specify how many parts you want to split the PDF into // 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)) { // Calculate the number of pages for each split file int totalPageInOneFile = doc.GetNumberOfPages() / numberOfSplitFiles; int firstPage = 1; int lastPage = totalPageInOneFile; for (int i = 1; i <= numberOfSplitFiles; i++) { // Generate the output file path string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"; // Create a new document and attach a writer for the specified output file using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) { // Copy pages from the original document to the new document doc.CopyPagesTo(firstPage, lastPage, pdfDocument); } // Update page range values for the next iteration firstPage = lastPage + 1; lastPage += totalPageInOneFile; } } } } } using System; using iText.Kernel.Pdf; class Program { 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; // Specify how many parts you want to split the PDF into // 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)) { // Calculate the number of pages for each split file int totalPageInOneFile = doc.GetNumberOfPages() / numberOfSplitFiles; int firstPage = 1; int lastPage = totalPageInOneFile; for (int i = 1; i <= numberOfSplitFiles; i++) { // Generate the output file path string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"; // Create a new document and attach a writer for the specified output file using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) { // Copy pages from the original document to the new document doc.CopyPagesTo(firstPage, lastPage, pdfDocument); } // Update page range values for the next iteration firstPage = lastPage + 1; lastPage += totalPageInOneFile; } } } } } $vbLabelText $csharpLabel 程式碼解釋 這段程式碼示範如何使用 iTextSharp 將大型 PDF 檔案分割成多個較小的部分。每個較小的 PDF 文件將包含原始文件的一部分。 使用 iTextSharp 分割 PDF 主要功能封裝在 SplitPdfUsingiTextSharp 方法中。 上述方法接受三個參數:outputFolder 和 numberOfSplitFiles。 使用 PdfReader 和 PdfDocument 在 SplitPdfUsingiTextSharp 方法內部: 透過從指定的 inputPdfPath 載入 PDF,建立名為 reader 的 PdfReader 物件。 使用 reader 初始化名為 doc 的 PdfDocument 物件。 將來源 PDF 中的總頁數除以 numberOfSplitFiles,以決定每個較小的 PDF 應該包含多少頁。 分割過程 循環從 1 迭代到 numberOfSplitFiles: 在指定的輸出資料夾中建立一個新的較小的 PDF 文件,文件名稱類似於"SplitPDF_iTextSharp_1.pdf"(對於第一次拆分)。 在這個新的 PDF 文件(pdfDocument)中,頁面是從原始文件 doc 複製而來的: 複製 firstPage 至 lastPage(含)。 儲存較小的 PDF 檔案。 下次迭代將更新 firstPage 和 lastPage 的值。 筆記 請確保 iTextSharp 庫已正確安裝並已在您的專案中引用。 將"input.pdf"替換為您的輸入PDF檔案的實際路徑。 比較 為了比較使用 iTextSharp 和IronPDF 的兩種拆分方法,讓我們根據以下幾個因素對它們進行評估: 1.易用性: iTextSharp: iTextSharp 方法涉及建立 PdfReader、PdfDocument 和 PdfWriter。 它會計算頁面範圍並將頁面複製到新文件中。 這種方法需要了解 iTextSharp API。 IronPDF: IronPDF方法涉及從來源檔案建立 PdfDocument,複製頁面,然後將其儲存到新檔案中。它的 API 更簡單易用。 2.程式碼可讀性: iTextSharp:程式碼涉及更多步驟,因此程式碼稍長,閱讀起來也可能更複雜。 IronPDF:由於步驟和方法呼叫較少,程式碼更簡潔、更易讀。 3.性能: iTextSharp:重複建立和銷毀 PdfDocument 實例可能會影響效能。 IronPDF:此方法步驟較少,由於頁面複製效率高,因此效能較好。 4.記憶體使用情況: iTextSharp:建立多個 PdfDocument 實例會消耗更多記憶體。 IronPDF:由於其簡化的方法,此方法更節省記憶體。 結論 總之,iTextSharp 和IronPDF都為在 C# 中分割 PDF 檔案提供了強大的解決方案,各有其優勢。 IronPDF因其簡潔性、易讀性以及更直接的方法所帶來的潛在更佳性能而脫穎而出。 對於追求功能全面性和易用性的開發者而言, IronPDF或許是個極具吸引力的選擇。此外, IronPDF也提供免費試用。 最終,iTextSharp 和IronPDF之間的選擇取決於特定的專案需求和偏好的開發風格。 請注意iTextSharp 是其各自所有者的註冊商標。 本網站與iTextSharp無任何關聯,亦未獲得其認可或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,反映的是撰寫本文時可公開取得的資訊。 常見問題解答 如何在 C# 中拆分 PDF 並保持原始格式? 您可以使用 IronPDF 在 C# 中拆分 PDF 同時保留其格式。IronPDF 提供了一個簡單的 API,允許您加載 PDF 並指定要拆分的頁面範圍,確保原始格式在生成的 PDF 中保持不變。 iTextSharp 和 IronPDF 在 C# 中用於拆分 PDF 的主要區別是什麼? 主要差異在於易用性和性能。IronPDF 提供更簡單的 API,步驟更少,使拆分 PDF 更容易且不丟失格式。另一方面,iTextSharp 需要創建 PdfReader、PdfDocument 和 PdfWriter 等多個實例,這可能更複雜。 我可以使用 C# 從 PDF 中提取特定頁面嗎? 是的,使用 IronPDF,您可以輕鬆從 PDF 中提取特定頁面。通過加載 PDF 並指定所需的頁碼,IronPDF 允許您提取並將這些頁面保存為新的 PDF 文件。 IronPDF 如何改善與其他 PDF 庫相比的代碼可讀性? IronPDF 通過提供清晰簡潔的 API 改善代碼可讀性,減少對多個類和對象的需求。這簡化了拆分之類的 PDF 操作任務所需的代碼,導致代碼更清晰且更易於維護。 在購買庫之前,是否可以在 C# 中測試 PDF 分割功能? 是的,IronPDF 提供免費試用,允許開發人員測試其 PDF 分割功能和其他功能。這一試用期使您能夠在做出購買決定之前評估其功能和性能。 選擇用於拆分文檔的 PDF 庫時應考慮哪些因素? 選擇 PDF 庫時,應考慮易用性、API 簡單性、性能以及維護原始文件格式的能力等因素。IronPDF 由於其易於使用的 API 和高效性能而常被優先選擇。 如何在 Visual Studio C# 項目中安裝 IronPDF? 要在 Visual Studio C# 項目中安裝 IronPDF,請使用 NuGet 包管理器控制台,輸入命令 Install-Package IronPDF。您還可以通過 NuGet 包管理器 UI 添加或從 IronPDF 的官方網站下載。 拆分 PDF 會影響其質量或格式嗎? 使用 IronPDF 拆分 PDF 可確保保持原始文件的質量和格式。IronPDF 的高效處理保持了原始內容的完整性。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新2026年3月1日 在 ASP.NET MVC 中生成 PDF:iTextSharp vs IronPDF 指南 比較在 ASP.NET MVC 中使用 iTextSharp vs IronPDF 的 PDF 產生方法。發現哪個函式庫能提供更好的 HTML 呈現以及更容易的實作。 閱讀更多 更新2026年2月1日 Ghostscript GPL vs IronPDF:技術比較指南 探索 Ghostscript GPL 與 IronPDF 的主要差異。比較 AGPL 授權與商業授權、指令列開關與本機 .NET API,以及 HTML-to-PDF 功能。 閱讀更多 更新2026年3月1日 ASP PDF 庫:比較 IronPDF、Aspose 和 Syncfusion 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 PDFsharp與iTextSharp(C# PDF庫比較)IronPDF與PDFSharpCore:2025年應...
更新2026年3月1日 在 ASP.NET MVC 中生成 PDF:iTextSharp vs IronPDF 指南 比較在 ASP.NET MVC 中使用 iTextSharp vs IronPDF 的 PDF 產生方法。發現哪個函式庫能提供更好的 HTML 呈現以及更容易的實作。 閱讀更多
更新2026年2月1日 Ghostscript GPL vs IronPDF:技術比較指南 探索 Ghostscript GPL 與 IronPDF 的主要差異。比較 AGPL 授權與商業授權、指令列開關與本機 .NET API,以及 HTML-to-PDF 功能。 閱讀更多
更新2026年3月1日 ASP PDF 庫:比較 IronPDF、Aspose 和 Syncfusion 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多