用PdfiumViewer打开C#close pdf

时间:2017-04-23 12:09:13

标签: c# pdf

我正在使用PdfiumViewer将pdf转换为图像

var document = PdfiumViewer.PdfDocument.Load(pdfoutputFilePath + TafCode + ".pdf");  
var image = document.Render(0, 842, 595, true);  
image.Save(imageoutputFilePath + TafCode + ".png", ImageFormat.Png);

当我这样做时,删除pdf

打开PDF时出错!

pdf有多接近?

2 个答案:

答案 0 :(得分:1)

将图片代码保存到使用中,以确保' Dispose'被调用,资源被解锁。

using (var document = PdfiumViewer.PdfDocument.Load(pdfoutputFilePath + TafCode + ".pdf"))
{
    var image = document.Render(0, 842, 595, true);  
    image.Save(imageoutputFilePath + TafCode + ".png", ImageFormat.Png);
}

你可以通过查看定义' Dispose'的PdfDocument类的代码来说明这一点。相应地释放文件的方法。

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
    /// <param name="disposing">Whether this method is called from Dispose.</param>
    protected void Dispose(bool disposing)
    {
        if (!_disposed && disposing)
        {
            if (_file != null)
            {
                _file.Dispose();
                _file = null;
            }

            _disposed = true;
        }
    }

答案 1 :(得分:1)

关闭打开的文档


    if (document.Document != null)
    {
      document.Document.Dispose();
    }