使用IronPDF與.NET:快速指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF會自動處理using聲明。 然而,如果您希望對這些儲存物件有更多控制權,可以使用using聲明,如下所示:

// Create a PDF document from HTML content and automatically manage resource disposal
using var pdfdoc = Renderer.RenderHtmlAsPdf(ImgHtml);

// Perform operations on the PDF document (e.g., editing)
pdfdoc.SaveAs("output.pdf");
// Create a PDF document from HTML content and automatically manage resource disposal
using var pdfdoc = Renderer.RenderHtmlAsPdf(ImgHtml);

// Perform operations on the PDF document (e.g., editing)
pdfdoc.SaveAs("output.pdf");
' Create a PDF document from HTML content and automatically manage resource disposal
Dim pdfdoc = Renderer.RenderHtmlAsPdf(ImgHtml)

' Perform operations on the PDF document (e.g., editing)
pdfdoc.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

使用using聲明後,您可以選擇立即釋放資源。 所有程式碼範例無論有無using聲明均可運行,是否以此方式使用以改善資源管理完全由您決定。

請注意,使用PdfDocument物件不會終止Chrome渲染引擎。在IronPDF初始化Chrome渲染引擎後(通常在第一次PDF渲染之前),會有一些記憶體開銷以保持Chrome運行。 Chrome不允許其進程停止並重新啟動多次。 因此,它將繼續在背景運行,直到進程被終止。

處理PDF物件將釋放這些PDF文件的記憶體(通常是少量的),但Chromium嵌入式框架(CEF)仍將在背景載入,準備進行下一次渲染。

當進程被終止時,我們的內部IronPDF單例將被處理,CEF將自動關閉,並釋放記憶體。

預期行為是您會看到一些開銷,但後續的渲染不應該在長時間內顯著增加記憶體使用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 20,296,129 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想快速獲得證明嗎? PM > Install-Package IronPdf
執行範例 看您的HTML變成PDF。