產品比較

比較在C#中使用iTextSharp和IronPDF進行PDF拆分

Chipego
奇佩戈·卡林达
2024年3月6日
分享:

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

C#中iTextSharp與IronPDF分割PDF的比較:圖1 - IronPDF網頁

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

iTextSharp

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

iTextSharp (iText 7) 是一個廣泛使用的庫,用於在 .NET 框架下處理 PDF 文件。 它提供了強大的功能,用於以程式方式創建、修改和提取PDF文件的內容。 開發人員可以使用 iTextSharp 向 PDF 添加文字、圖像、表格和其他圖形元素。 此外,它支持文件組裝、數字簽名,以及符合檔案和無障礙標準的規範。 起初作為 Java 庫,iTextSharp 被移植到 .NET 並擁有一個活躍的開發人員和用戶社群。

安裝 IronPDF 函式庫

要在 Visual Studio 中使用套件管理器主控台安裝IronPDF NuGet 套件,請遵循以下步驟:

  1. 在 Visual Studio 中,依次選擇工具 -> NuGet 套件管理員 -> 套件管理控制台。

  2. 使用以下命令安裝 IronPDF NuGet 套件:
    :ProductInstall

這將下載並安裝 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

這將下載並安裝iTextSharp套件及其相依性到您的專案中。 安裝完成後,您可以在您的C#專案中開始使用iTextSharp來處理PDF檔案。

使用 IronPDF 在 C# 中拆分 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;
     }
 }
Shared Sub Main(ByVal args() As String)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'	string file = "input.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++) { PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage); string name = string.Format("{0}\SplitPDF_IronPDF_{1}.pdf", outputFolder, i); newSplitPDF.SaveAs(name); 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. 名為 sourceFile 的 PdfDocument 物件是通過從指定的 inputPdfPath 加載 PDF 創建的。

  2. 兩個整數變量,firstPage 和 lastPage,被初始化。 這些代表了拆分的頁面範圍。

  3. totalPageInOneFile 是通過將源 PDF 的總頁數除以指定的 NumberOfSplitFiles 計算得出的。

  4. 一個迴圈從 1 遍歷到 NumberOfSplitFiles:

  5. 一個名為 newSplitPDF 的新 PdfDocument 對象已創建。

  6. 從 firstPage 到 lastPage(包括) 的頁面從 sourceFile 複製到 newSplitPDF。

  7. 結果較小的 PDF 將以「SplitPDF_IronPDF_1.pdf」(對於第一次拆分)作為檔案名,儲存在指定的輸出資料夾中。

  8. firstPage 和 lastPage 的值會在下一次迭代中更新。

注意

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

輸出文件創建為:

在 C# 中使用 iTextSharp 和 IronPDF 拆分 PDF 的比較:圖 3 - 生成的輸出文件

在 C# 中使用 iTextSharp 拆分 PDF 文件

現在,我們將使用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;
             }
         }
     }
 }
Shared Sub Main(ByVal args() As String)
	 Dim inputPath As String = "input.pdf"
	 ' Output PDF files path (prefix for the generated files)
	 Dim outputPath As String = "output_split"
	 Dim NumberOfSplitFiles As Integer = 3
	 ' Call the SplitPdf method to split the PDF
	 SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles)
End Sub
Shared Sub SplitPdfUsingiTextSharp(ByVal inputPdfPath As String, ByVal outputFolder As String, ByVal NumberOfSplitFiles As Integer)
	 Using Reader As New PdfReader(inputPdfPath)
		 Using doc As New PdfDocument(Reader)
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
			 Dim totalPageInOneFile As Integer = doc.GetNumberOfPages() / NumberOfSplitFiles
			 Dim firstPage As Integer = 1
			 Dim lastPage As Integer = totalPageInOneFile '  int pagenumber
			 For i As Integer = 1 To NumberOfSplitFiles
				 Dim filename As String = $"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"
				 Using pdfDocument As New PdfDocument(New PdfWriter(filename)) ' create output file
					 'pdfDocument.get
					 doc.CopyPagesTo(firstPage, lastPage, pdfDocument)
				 End Using
				 firstPage = lastPage + 1
				 lastPage += totalPageInOneFile
			 Next i
		 End Using
	 End Using
End Sub
$vbLabelText   $csharpLabel

程式碼說明

上述程式碼演示了如何使用 iTextSharp 將大型 PDF 文件拆分為較小的部分。每個較小的 PDF 文件將包含原始文件的一部分。

使用 iTextSharp 拆分 PDF文件

  1. 主要功能封裝在 SplitPdfUsingiTextSharp 方法中。

  2. 上述方法接受三個參數:inputPdfPath,outputFolder,以及NumberOfSplitFiles。

使用 PdfReader 和 PdfDocument

在 SplitPdfUsingiTextSharp 方法中:

  1. 一個名為 Reader 的 PdfReader 物件是透過從指定的 inputPdfPath 加載 PDF 文件來創建的。

  2. 一個名為 DOC 的 PdfDocument 物件被使用 Reader 初始化。

  3. 源 PDF 的總頁數除以 NumberOfSplitFiles,以確定每個較小的 PDF 應包含多少頁。

拆分過程

一個迴圈從 1 遍歷到 NumberOfSplitFiles:

  1. 在指定的輸出資料夾中會建立一個新的較小 PDF 檔案,檔名類似於 "SplitPDF_iTextSharp_1.pdf"(對於第一個拆分)。

  2. 在這個新的 PDF 文件(pdfDocument)中,頁面是從原始文件複製來的:

  3. 從 firstPage 到 lastPage(含)被複製。

  4. 較小的 PDF 已保存。

  5. firstPage 和 lastPage 的值會在下一次迭代中更新。

注意

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

在 C# 中拆分 PDF 的比較:iTextSharp 與 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 取決於個別專案需求和偏好的開發風格。

Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
PDFsharp 與 iTextSharp (C# PDF 函式庫比較)
下一個 >
IronPDF 與 PDFSharpCore 的比較