跳至頁尾內容
產品對比

iTextSharp 和 IronPDF 在 C# 中拆分 PDF 的比較

PDF (Portable Document Format,可攜式文件格式) 檔案廣泛用於分享和展示文件,有時您可能需要將一個 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 以及 merge or split PDF。 此外,IronPDF 擅長將 HTML 內容轉換為 PDF 格式,因此對於產生報告或文件非常有用。 IronPDF 支援數位簽章、安全功能和高品質的輸出,可簡化 .NET 應用程式中與 PDF 相關的工作。

iTextSharp。

A Comparison of Splitting PDF in C# Between iTextSharp and IronPDF:圖 2 - iTextSharp 網頁

iTextSharp (iText 7) 是一個廣泛使用的函式庫,用於在 .NET Framework 中處理 PDF 檔案。 它提供了強大的功能,可程式化地建立、修改和擷取 PDF 文件中的內容。 開發人員可使用 iTextSharp 將文字、圖片、表格和其他圖形元素新增至 PDF。 此外,它還支援文件組合、數位簽署,並符合歸檔和可讀性標準。 iTextSharp 原本是 Java 函式庫,後來移植到 .NET,並擁有活躍的開發者與使用者社群。

安裝 IronPdf 函式庫

若要使用 Visual Studio 中的套件管理員控制台安裝 IronPDF NuGet 套件,請遵循下列步驟:

1.在 Visual Studio 中,前往 Tools -> NuGet Package Manager -> Package Manager Console。 2.使用以下指令安裝 IronPDF NuGet 套件:

```shell
:ProductInstall
```

這將會下載 IronPDF 套件及其相依性,並將其安裝到您的專案中。 安裝完成後,您就可以開始在 C# 專案中使用 IronPDF 執行 PDF 相關任務。

另外,您也可以使用 Visual Studio 中的 NuGet Package Manager 安裝 IronPdf,或直接將套件新增至專案檔案。另一種方法是從 官方網站 下載套件,然後手動將其新增至專案。 每種方法都提供了一種直接的方式,將 IronPDF 整合到您的 C# 專案中,以獲得 PDF 相關的功能。

安裝 iTextSharp 函式庫。

若要使用 Visual Studio 中的套件管理員控制台安裝 iTextSharp ,您可以遵循下列步驟:

1.在 Visual Studio 中,前往 Tools -> NuGet Package Manager -> Package Manager Console。 2.使用下列指令安裝 iTextSharp NuGet 套件:

```shell
Install-Package itext7
```

這將會下載 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,會建立一個名為 sourceFilePdfDocument 物件。 2.兩個整數變數 firstPagelastPage 已被初始化。 這些代表分割的頁面範圍。 3.totalPageInOneFile 的計算方式是將來源 PDF 的總頁數除以指定的 numberOfSplitFiles 。 4.從 1 到 numberOfSplitFiles 的循環迭代: 5.建立一個名為 newSplitPDF 的新 PdfDocument 物件。 6.從 firstPagelastPage (含) 的頁面會從 sourceFile 複製到 newSplitPDF 中。 7.所產生的較小 PDF 將以類似"SplitPDF_IronPDF_1.pdf"的檔名儲存於指定的輸出資料夾(針對第一次分割)。 8.firstPagelastPage 值會在下次迭代時更新。

注意事項

您應該使用輸入 PDF 檔案的實際路徑取代 "input.pdf"。確保 IronPDF 函式庫已正確安裝,並在專案中引用。

輸出檔案建立為

A Comparison of Splitting PDF in C# Between iTextSharp and IronPDF:圖 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.上述方法需要三個參數:inputPdfPath, outputFolder, 和 numberOfSplitFiles.

使用 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 函式庫已正確地安裝,並在您的專案中引用。 用輸入 PDF 檔案的實際路徑取代 "input.pdf"。

A Comparison of Splitting PDF in C# Between iTextSharp and IronPDF:圖 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 檔案保留原始格式。

在 C# 中,iTextSharp 和 IronPDF 在分割 PDF 方面有哪些主要差異?

主要區別在於易用性和性能。 IronPDF 提供更簡潔的 API,步驟更少,更容易在不遺失格式的情況下分割 PDF 檔案。而 iTextSharp 則需要建立多個實例,例如PdfReaderPdfDocumentPdfWriter ,可能更複雜。

我可以使用 C# 從 PDF 提取特定頁面嗎?

是的,使用 IronPDF,您可以輕鬆地在 C# 中從 PDF 文件中提取特定頁面。只需載入 PDF 檔案並指定所需的頁碼,IronPDF 即可將這些頁面提取並儲存為新的 PDF 文件。

與其他PDF庫相比,IronPDF是如何提高程式碼可讀性的?

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擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。