產品比較

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

發佈 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 是一個強大的 C# 程式庫,專門用於處理 PDF 文件。它提供了功能來 創建, 修改,和 提取 從 PDF 文件中提取內容。開發人員可以從頭開始生成 PDF,編輯現有的 PDF,並 合併或拆分 此外,IronPDF 在將 HTML 內容轉換為 PDF 格式方面表現優異,這對於生成報告或文檔非常有用。憑藉對數位簽名、安全功能和高品質輸出的支持,IronPDF 簡化了在 .NET 應用程式中的 PDF 相關任務。

iTextSharp

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

iTextSharp (iText 7) 是用於處理 .NET 框架中的 PDF 文件的廣泛使用的庫。它提供強大的功能來以程式方式創建、修改和提取 PDF 文件的內容。開發人員可以使用 iTextSharp 向 PDF 添加文本、圖像、表格和其他圖形元素。此外,它支持文件組裝、數字簽名以及遵循存檔和可訪問性標準。iTextSharp 最初是一個 Java 庫,後來被移植到 .NET 並擁有活躍的開發者和用戶社群。

安裝 IronPDF 程式庫

要安裝 IronPDF NuGet 套件 在 Visual Studio 中使用套件管理員主控台,請按以下步驟操作:

  1. 在 Visual Studio 中,前往工具 -> NuGet 套件管理員 -> 套件管理員主控台。

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

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

或者,您可以使用Visual Studio中的NuGet套件管理器安裝IronPDF,或者將套件直接添加到您的專案檔案中。另一個選項是從 官方網站 並將其手動添加到您的項目中。每種方法都提供了一種將IronPDF集成到您的C#項目中的簡單方法,以實現PDF相關功能。

安裝 iTextSharp Library

安裝 iTextSharp 在 Visual Studio 的套件管理主控台中,請按照以下步驟操作:

  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; } }
VB   C#

代碼說明

此代碼的目的是使用 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. 通過將來源 PDF 的總頁數除以指定的 NumberOfSplitFiles 計算 totalPageInOneFile。

  4. 一個迴圈從 1 到 NumberOfSplitFiles 進行迭代:

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

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

  7. 生成的較小 PDF 會儲存為像 "SplitPDF_IronPDF_1.pdf" 這樣的檔名 (對於第一次分割) 在指定的輸出資料夾中。

  8. 第一頁和最後一頁的值更新,用於下一次迭代。

注意事項

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

輸出文件創建為:

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

使用 iTextSharp 在C# 中拆分 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
VB   C#

代碼說明

上面的代碼展示了如何使用 iTextSharp 將大型 PDF 文件拆分成較小的部分。每個較小的 PDF 文件將包含原始文件的一部分。

使用 iTextSharp 分割 PDF

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

使用 PdfReader 和 PdfDocument

在 SplitPdfUsingiTextSharp 方法中:

  1. 透過從指定的 inputPdfPath 加載 PDF,創建一個名為 Reader 的 PdfReader 對象。

  2. 使用 Reader 初始化一個名為 DOC 的 PdfDocument 對象。

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

拆分過程

一個循環從 1 到 NumberOfSplitFiles 迭代:

  1. 創建一個新的較小的 PDF 文件,其文件名類似於 "SplitPDF_iTextSharp_1.pdf" (對於第一次分割) 在指定的輸出資料夾中。

  2. 在這個新的PDF檔案內 (pdf文件), 頁面從原始文件中複製:

  3. firstPage 到 lastPage (包容) 被複製。

  4. 較小的PDF已儲存。

  5. 更新firstPage和lastPage值以進行下一次迭代。

注意

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

使用 iTextSharp 與 IronPDF 在 C# 中分割 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 之間的選擇取決於個別專案的需求和偏好的開發風格。

< 上一頁
PDFsharp 與 iTextSharp (C# PDF 函式庫比較)
下一個 >
IronPDF 與 PDFSharpCore 的比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >