跳過到頁腳內容
使用IRONPDF

如何在C#中裁剪PDF文件

使用 IronPDF 庫在 C# 中裁剪和修剪 PDF 頁面

對於 C# 開發者來說,裁剪和修剪 PDF 頁面始終是一項具有挑戰性的任務。 在 PDF 文檔中繪製一個裁剪框選擇所需的區域,然後僅保存該部分並不簡單。 幸運的是,IronPDF 庫在 C# 的 .NET 中提供了一個解決方案。

IronPDF .NET 庫

IronPDF .NET PDF Library 是一個 C# .NET 庫,允許開發者創建、編輯和操作 PDF 文件。 由於其 PDF 生成能力,使開發者在未安裝 Adobe Acrobat 的情況下也能處理 PDF 文件,因而在 C# 開發者中非常受歡迎。 IronPDF for .NET also allows conversion between different formats like HTML to PDF Conversion, Converting URLs to PDF, and Image to PDF Conversion.

It also supports adding Custom Headers and Footers, Digital Signatures in PDFs, annotations and Adding/Removing Attachments from PDFs, user and owner passwords, and other security options. IronPDF 具有快速的 Chromium 引擎,提供卓越的渲染體驗。 It also provides full Multithreading Support and Async Capabilities.

先決條件

在開始之前,需要下載並安裝最新版本的 Visual Studio 2022。 Visual Studio 是構建 C# 應用程序所必需的。安裝將建立 .NET 環境,之後本地系統將準備好製作 PDF 到 JPG 轉換器。 您可以在此 Visual Studio 下載頁面 下載 Visual Studio。

IronPDF 安裝

有多種方式可以安裝 IronPDF:

  1. 您可以從使用 Visual Studio 創建的 C# 項目中的 NuGet 程序包管理器中下載 IronPDF。 通過工具或右鍵單擊解決方案資源管理器訪問 NuGet 程序包管理器。 瀏覽 IronPDF 包並安裝它。
  2. 另一種安裝 IronPDF 的方法是直接從 IronPDF NuGet 頁面 下載。

使用 IronPDF 在 C# 中裁剪 PDF 文件

以下分步過程將幫助您裁剪 PDF 頁面。 這不是簡單的,但我們可以利用一些方法來完成這項任務。 讓我們開始吧!

步驟 1:加載 PDF 文件

To load a PDF file from a local location into this project, IronPDF provides a FromFile method present in the PdfDocument class. 以下代碼示例演示了如何打開現有的 PDF 文件:

// Load an existing PDF document from a file
PdfDocument pdf = PdfDocument.FromFile("Input.pdf");
// Load an existing PDF document from a file
PdfDocument pdf = PdfDocument.FromFile("Input.pdf");
' Load an existing PDF document from a file
Dim pdf As PdfDocument = PdfDocument.FromFile("Input.pdf")
$vbLabelText   $csharpLabel

加載的文件如下:

如何在 C# 中裁剪 PDF 文件,圖 1:示例 PDF 條形碼文件 示例 PDF 條形碼文件

步驟 2:從 PDF 文件中加載特定頁面

現在文件已打開以進行編輯,創建一個單獨的 PdfDocument 對象,並使用 CopyPage 方法存儲需要裁剪的特定頁面。 只需傳遞需要裁剪的頁面的索引即可。 此處,代碼樣本將裁剪 PDF 文檔的第一頁。

// Copy the first page of the loaded PDF document
PdfDocument loadedPage = pdf.CopyPage(0);
// Copy the first page of the loaded PDF document
PdfDocument loadedPage = pdf.CopyPage(0);
' Copy the first page of the loaded PDF document
Dim loadedPage As PdfDocument = pdf.CopyPage(0)
$vbLabelText   $csharpLabel

步驟 3:將加載的 PDF 頁面轉換為圖像

將 PDF 頁面轉換為高分辨率圖像 方法提供了將 PDF 頁面保存為高分辨率圖像文件的功能。以下代碼有助於將選擇的頁面轉換為圖像以進行裁剪。

// Convert the PDF page to a high-resolution PNG image
loadedPage.RasterizeToImageFiles(@"C:\Image\Page_to_be_Cropped.png");
// Convert the PDF page to a high-resolution PNG image
loadedPage.RasterizeToImageFiles(@"C:\Image\Page_to_be_Cropped.png");
' Convert the PDF page to a high-resolution PNG image
loadedPage.RasterizeToImageFiles("C:\Image\Page_to_be_Cropped.png")
$vbLabelText   $csharpLabel

現在頁面將被轉換為圖像文件。輸出是一個高質量的 PNG 圖像。

如何在 C# 中裁剪 PDF 文件,圖 2:輸出高質量 PNG 圖像文件 輸出高質量 PNG 圖像文件

現在,特定頁面已從原始文件中分離,準備好被裁剪。

步驟 4:檢索加載頁面的尺寸

為了裁剪 PDF,有必要創建具有特定寬度和高度的裁剪框。 為此目的,將使用 ChromePdfRenderer 類創建新文件。 它提供了根據需要自定義 PDF 頁面大小的選項,數據在頁面中平均分配。

在創建 ChromePdfRenderer 之前,首先獲取在步驟 2 中加載頁面的尺寸。然後,在設置自定義頁面大小以創建裁剪框時使用這些尺寸。 以下代碼樣本將幫助您獲取頁面的寬度和高度:

// Retrieve dimensions of the loaded PDF page
PdfPagesCollection pages = loadedPage.Pages;
PdfPage pdfPage = pages[0];

// Dimensions retrieved in mm
float width = pdfPage.Width;
float height = pdfPage.Height;
// Retrieve dimensions of the loaded PDF page
PdfPagesCollection pages = loadedPage.Pages;
PdfPage pdfPage = pages[0];

// Dimensions retrieved in mm
float width = pdfPage.Width;
float height = pdfPage.Height;
' Retrieve dimensions of the loaded PDF page
Dim pages As PdfPagesCollection = loadedPage.Pages
Dim pdfPage As PdfPage = pages(0)

' Dimensions retrieved in mm
Dim width As Single = pdfPage.Width
Dim height As Single = pdfPage.Height
$vbLabelText   $csharpLabel

首先,使用 PdfPagesCollection 檢索加載的 PDF 文件中的總頁數。 然後,將該頁面傳遞給 PdfPage 實例,以從頁面的 WidthHeight 屬性中獲取頁面尺寸值。 全部完成! 現在,讓我們進入下一步創建自定義裁剪框。

步驟 5:設置自定義 PDF 頁面大小

以下代碼將幫助創建一個自定義 PDF 紙張大小,這將用作裁剪框,以在不同頁面段中裁剪內容。

// Create a ChromePdfRenderer to set up a custom paper size
ChromePdfRenderer pdfRenderer = new ChromePdfRenderer();
pdfRenderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Custom;
pdfRenderer.RenderingOptions.SetCustomPaperSizeInMillimeters(width, height / 4);
pdfRenderer.RenderingOptions.ForcePaperSize = true;
// Create a ChromePdfRenderer to set up a custom paper size
ChromePdfRenderer pdfRenderer = new ChromePdfRenderer();
pdfRenderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Custom;
pdfRenderer.RenderingOptions.SetCustomPaperSizeInMillimeters(width, height / 4);
pdfRenderer.RenderingOptions.ForcePaperSize = true;
' Create a ChromePdfRenderer to set up a custom paper size
Dim pdfRenderer As New ChromePdfRenderer()
pdfRenderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Custom
'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:
pdfRenderer.RenderingOptions.SetCustomPaperSizeInMillimeters(width, height / 4)
pdfRenderer.RenderingOptions.ForcePaperSize = True
$vbLabelText   $csharpLabel

在上述代碼中,創建了一個 ChromePdfRenderer,用於創建新的 PDF 文件。 然後,將PdfPaperSize 屬性值設置為 Custom。 最後,使用在步驟 4 中檢索的尺寸設置自定義頁邊距。

設置寬度為原始頁面寬度,將高度減少到原始頁面長度的 1/4。 這使得頁面作為矩形形狀的媒體框工作於內容。

注意:您可以使用 ForcePaperSize = true 確保應用自定義大小。 有關設置自定義邊距,請訪問此 自定義 PDF 邊距指南

步驟 6:使用 HTML 創建新文件

現在,這最後一步將使用自定義頁面大小 PDF 和從加載頁面保存的圖像創建新文件。

// Render HTML to a PDF document with a custom paper size using the cropped image
var croppedPdf = pdfRenderer.RenderHtmlAsPdf("<img src='Page_to_be_Cropped.png'/>", @"C:\Image\");
// Render HTML to a PDF document with a custom paper size using the cropped image
var croppedPdf = pdfRenderer.RenderHtmlAsPdf("<img src='Page_to_be_Cropped.png'/>", @"C:\Image\");
' Render HTML to a PDF document with a custom paper size using the cropped image
Dim croppedPdf = pdfRenderer.RenderHtmlAsPdf("<img src='Page_to_be_Cropped.png'/>", "C:\Image\")
$vbLabelText   $csharpLabel

現在讓我們使用 SaveAs 方法保存該文件。

// Save the newly cropped document
croppedPdf.SaveAs("Cropped.pdf");
// Save the newly cropped document
croppedPdf.SaveAs("Cropped.pdf");
' Save the newly cropped document
croppedPdf.SaveAs("Cropped.pdf")
$vbLabelText   $csharpLabel

輸出

如何在 C# 中裁剪 PDF 文件,圖 3:裁剪後的 PDF 文件 裁剪後的 PDF 文件

從輸出中可以看出,單個圖像現在被切割成多個頁面,並創建了自定義修剪框。 您可以使用以下代碼複製所需的特定頁面:

// Copy and save a specific page from the cropped document
croppedPdf.CopyPage(1).SaveAs("Cropped_Page1.pdf");
// Copy and save a specific page from the cropped document
croppedPdf.CopyPage(1).SaveAs("Cropped_Page1.pdf");
' Copy and save a specific page from the cropped document
croppedPdf.CopyPage(1).SaveAs("Cropped_Page1.pdf")
$vbLabelText   $csharpLabel

結論

本文演示了如何通過使用 IronPDF for .NET Framework 創建虛擬矩形裁剪框按頁裁剪 PDF 文件。 RasterizeToImageFiles 方法有助於將頁面轉換為圖像,然後用於創建像素完美的 PDF 文件。

IronPDF 還提供其他 PDF 工具,可以旋轉 PDF 頁面、更改 PDF 文字、設置邊距、格式化 PDF、轉換等。 To learn more about IronPDF for .NET and to access additional features to Manipulate PDF Files with IronPDF or how to Customize PDF Paper Size.

IronPDF .NET 庫可以免費用於開發,但需要授權商業用途。 從此 IronPDF ZIP 下載 中下載功能強大的 .NET 鐵 PDF 庫並試用!

常見問題解答

如何在C#中裁切PDF頁面而不遺失格式?

您可以使用 IronPDF 在 C# 中裁切 PDF 頁面。具體操作方法是:載入 PDF 文檔,將所需頁面轉換為高解析度影像,然後使用影像尺寸透過 `ChromePdfRenderer` 類別設定裁切框。這樣,您就可以在不遺失格式的情況下渲染裁剪後的 PDF。

使用 C# 裁切 PDF 檔案需要哪些步驟?

要使用 C# 裁剪 PDF,首先使用 `PdfDocument.FromFile` 載入 PDF,提取要裁剪的特定頁面,使用 `RasterizeToImageFiles` 將其轉換為圖像,然後使用 `ChromePdfRenderer` 應用裁剪框並渲染最終裁剪後的 PDF 頁面。

我可以使用 IronPDF 將 HTML 轉換為 PDF 嗎?

是的,IronPDF 允許您使用 `RenderHtmlAsPdf`(用於 HTML 字串)和 `RenderHtmlFileAsPdf`(用於 HTML 檔案)等方法將 HTML 轉換為 PDF。這對於從網頁或 HTML 內容產生 PDF 非常有用。

我使用 IronPDF 進行 PDF 處理需要任何特定的軟體嗎?

若要使用 IronPDF,您需要安裝 Visual Studio 2022 來設定 C# 應用程式所需的 .NET 環境。您還需要透過 NuGet 套件管理器安裝 IronPDF。

是否可以使用 IronPDF 為 PDF 檔案添加數位簽章?

是的,IronPDF 支援為 PDF 添加數位簽名,從而增強文件的安全性和真實性。此功能是該程式庫強大的 PDF 處理功能的一部分。

使用 C# 編寫 IronPDF 時,有哪些故障排除技巧?

如果您在使用 IronPDF 時遇到問題,請確保所有依賴項已透過 NuGet 正確安裝,以驗證您的 Visual Studio 環境是否已設定為 .NET 開發,並查閱 IronPDF 官方文件以取得特定方法和類別的指導。

在 IronPDF 中使用 `ChromePdfRenderer` 類別的目的是什麼?

IronPDF 中的 `ChromePdfRenderer` 類別用於渲染具有特定配置的 PDF 文檔,例如設定頁面大小和裁剪框。當您需要自訂輸出 PDF 的外觀或尺寸時,它尤其有用。

使用 IronPDF 時,如何確保我的 PDF 檔案安全?

IronPDF 可讓您透過新增使用者密碼和所有者密碼以及套用數位簽章來增強 PDF 安全性。這些功能有助於保護您的文件免受未經授權的存取和篡改。

IronPDF 是否完全相容於 .NET 10?這對於 C# 中的 PDF 裁剪有什麼好處?

是的,IronPDF 與 .NET 10 完全相容。它支援所有主流平台上的 .NET 10,透過增強效能、更好的記憶體使用和更新的 C# 語言特性,使 PDF 操作(如渲染、裁剪、影像處理等)受益。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。