Using IronPDF with .NET: A Quick Guide

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

IronPDF会自动为您处理PdfDocument对象,无需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语句的情况下都可以工作,纯粹取决于您是否愿意以这种方式使用它来更好地管理资源。

请注意,使用dispose()方法来处理PdfDocument对象不会终止Chrome渲染引擎。在IronPdf初始化Chrome渲染引擎后(通常就在第一次PDF渲染之前),会有一些内存开销来保持Chrome运行。 Chrome不允许其进程被多次停止和启动。 因此,它将在后台继续运行,直到进程被终止。

处理PDF对象将释放这些PDF文档的内存(通常数量很小),但Chromium嵌入式框架(CEF)仍将在后台加载,准备好进行下一次渲染。

当进程被终止时,CEF会自动关闭,内存被释放,我们的内部IronPdf单体会被处理掉。

预期的行为是您会看到一些开销,但后续的渲染在长时间内不应显著增加内存使用。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布