跳過到頁腳內容
產品比較

在C#中拆分PDF的比較iTextSharp與IronPDF之間

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 的程式庫。

  1. iTextSharp
  2. IronPDF

如何使用 iTextSharp 在 C# 分割 PDF

  1. 首先,安裝 iText7 函式庫。
  2. 從輸入的 PDF 檔案建立 PdfReader。
  3. 使用 PdfDocument 處理 PDF 內容。
  4. 計算每個拆分文件的頁數。
  5. 設定初始頁面範圍值。
  6. 使用循環處理每個分割的檔案。
  7. 為目前拆分的檔案建立一個新的 PdfDocument。
  8. 將原文檔中的頁面複製到新文件中。
  9. 更新下次迭代的頁面範圍值。
  10. 儲存完成的輸出 PDF 檔案。
  11. 重複以上步驟,直到建立所有檔案。
  12. 對指定數量的拆分檔案重複此程序。

IronPDF

iTextSharp 和IronPDF在 C# 中拆分 PDF 的比較:圖 1 - IronPDF網頁

IronPDF是一個功能強大的 C# 庫,專為處理 PDF 文件而設計。 它提供了創建修改提取PDF 文件內容的功能。 開發人員可以從頭開始產生 PDF,編輯現有 PDF,以及合併或分割它們。 此外, IronPDF在將 HTML 內容轉換為 PDF 格式方面表現出色,因此可用於產生報告或文件。 IronPDF支援數位簽章、安全功能和高品質輸出,簡化了.NET應用程式中與 PDF 相關的任務。

iTextSharp

iTextSharp 與IronPDF在 C# 中拆分 PDF 的比較:圖 2 - iTextSharp 網頁

iTextSharp (iText 7)是.NET Framework中廣泛使用的用於處理 PDF 檔案的函式庫。 它提供了強大的功能,可以透過程式設計方式建立、修改和提取 PDF 文件中的內容。 開發人員可以使用 iTextSharp 為 PDF 添加文字、圖像、表格和其他圖形元素。 此外,它還支援文件組裝、數位簽名,並符合歸檔和無障礙標準。 iTextSharp 最初是一個 Java 函式庫,後來移植到.NET ,並擁有活躍的開發者和使用者社群。

安裝IronPDF庫

若要使用 Visual Studio 中的套件管理器控制台安裝IronPDF NuGet程式包,請依照下列步驟操作:

  1. 在 Visual Studio 中,前往"工具"-> "NuGet套件管理器"->"套件管理器控制台"。
  2. 使用以下指令安裝IronPDF NuGet套件:

    Install-Package IronPdf

這將下載IronPDF軟體包及其相依性並將其安裝到您的專案中。 安裝完成後,您就可以在 C# 專案中使用IronPDF來處理與 PDF 相關的任務了。

或者,您可以使用 Visual Studio 中的NuGet套件管理器安裝IronPDF ,或直接將套件新增至專案文件。另一種方法是從官方網站下載套件並手動將其添加到專案中。 每種方法都提供了一種直接的方式,可以將IronPDF整合到您的 C# 專案中,以實現與 PDF 相關的功能。

安裝 iTextSharp 函式庫

若要在 Visual Studio 中使用套件管理器控制台安裝iTextSharp ,您可以依照下列步驟操作:

  1. 在 Visual Studio 中,前往"工具"-> "NuGet套件管理器"->"套件管理器控制台"。
  2. 使用下列指令安裝 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 方法來實現此功能。

方法參數

  1. inputPdfPath: 表示輸入 PDF 檔案路徑的字串(例如,"input.pdf")。
  2. outputFolder: 表示將保存拆分後的 PDF 檔案的輸出資料夾的字串(例如,"output_split")。
  3. numberOfSplitFiles: 一個整數,表示原始 PDF 檔案將被拆分成多少個較小的 PDF 檔案。

分割過程

SplitPdfUsingIronPDF 方法內部:

  1. 透過從指定的 inputPdfPath 載入 PDF,建立名為 PdfDocument 的物件。
  2. 初始化兩個整數變數 firstPagelastPage。 這些代表要拆分的頁面範圍。
  3. totalPageInOneFile 是將來源 PDF 的總頁數除以指定的 numberOfSplitFiles 來計算的。
  4. 循環從 1 迭代到 numberOfSplitFiles:
  5. 建立了一個名為 newSplitPDF 的新 PdfDocument 物件。
  6. firstPagelastPage(含)的頁是從 sourceFilenewSplitPDF 複製的。
  7. 產生的較小 PDF 檔案將以類似"SplitPDF_IronPDF_1.pdf"(第一次分割)的檔案名稱保存在指定的輸出資料夾中。
  8. 下次迭代將更新 firstPagelastPage 的值。

筆記

您應該將"input.pdf"替換為您的輸入PDF文件的實際路徑。請確保IronPDF庫已正確安裝並在您的專案中引用。

輸出檔案建立如下:

C# 中 iTextSharp 與IronPDF 的PDF 分割比較:圖 3 - 建立的輸出檔案

使用 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

  1. 主要功能封裝在 SplitPdfUsingiTextSharp 方法中。
  2. 上述方法接受三個參數:outputFoldernumberOfSplitFiles

使用 PdfReader 和 PdfDocument

SplitPdfUsingiTextSharp 方法內部:

  1. 透過從指定的 inputPdfPath 載入 PDF,建立名為 readerPdfReader 物件。
  2. 使用 reader 初始化名為 docPdfDocument 物件。
  3. 將來源 PDF 中的總頁數除以 numberOfSplitFiles,以決定每個較小的 PDF 應該包含多少頁。

分割過程

循環從 1 迭代到 numberOfSplitFiles:

  1. 在指定的輸出資料夾中建立一個新的較小的 PDF 文件,文件名稱類似於"SplitPDF_iTextSharp_1.pdf"(對於第一次拆分)。
  2. 在這個新的 PDF 文件(pdfDocument)中,頁面是從原始文件 doc 複製而來的:
  3. 複製 firstPagelastPage(含)。
  4. 儲存較小的 PDF 檔案。
  5. 下次迭代將更新 firstPagelastPage 的值。

筆記

請確保 iTextSharp 庫已正確安裝並已在您的專案中引用。 將"input.pdf"替換為您的輸入PDF檔案的實際路徑。

C# 中 iTextSharp 與IronPDF 的PDF 分割比較:圖 4 - 建立的輸出檔案

比較

為了比較使用 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 需要創建 PdfReaderPdfDocumentPdfWriter 等多個實例,這可能更複雜。

我可以使用 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 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me