使用 IronPDF for .NET:快速指南
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")
使用 using 声明,您可以选择何时立即释放资源。 所有代码示例无论是否使用 using 语句都能正常运行,是否要以这种方式使用它以更好地进行资源管理完全取决于您自己。
请注意,使用 dispose() 方法释放 PdfDocument 对象并不会终止 Chrome 渲染引擎。IronPdfIronPDFChrome 渲染引擎后(通常在首次渲染 PDF 之前),会占用一些内存来维持 Chrome 的运行。 Chrome不允许其进程被多次停止和启动。 因此,它将在后台继续运行,直到进程被终止。
处理PDF对象将释放这些PDF文档的内存(通常数量很小),但Chromium嵌入式框架(CEF)仍将在后台加载,准备好进行下一次渲染。
当进程被终止时,CEF会自动关闭,内存被释放,我们的内部IronPdf单体会被处理掉。
预期的行为是您会看到一些开销,但后续的渲染在长时间内不应显著增加内存使用。

