pvginkel / PdfiumViewer

PDF viewer based on Google's PDFium.
Apache License 2.0
967 stars 418 forks source link

Close the opened document #110

Closed pedrodemaria closed 7 years ago

pedrodemaria commented 7 years ago

Create a method to close and free the resources os the current renderized document. I can`t exclude the pdf file after load in the pdfviewer. (this file is being used by another process).

pedrodemaria commented 7 years ago

To really free the resources, i have to use the method dispose of the PDFViewer and the Renderer, like this:

pdfViewer.Document?.Dispose(); pdfViewer..Renderer.Document?.Dispose();

Would be correctly run the dispose method of Document inside the renderer, when the dispose method of the Document inside the PDFViewer is called?

Shenyz163 commented 7 years ago

I have encountered the same problem, how did you solve it?

pedrohas2000 commented 7 years ago

@Shenyz163 To close the document on demand, i used: pdfViewer1.Document?.Dispose(); pdfViewer1.Renderer.Document?.Dispose();

Also, I put this code on the form_closing event like this:

private void frmIncluirF_FormClosing(object sender, FormClosingEventArgs e) { if (!e.Cancel) { pdfViewer1.Document?.Dispose(); pdfViewer1.Renderer.Document?.Dispose(); } }

Shenyz163 commented 7 years ago

Thank you. I'll try.

Shenyz163 commented 7 years ago

@pedrohas2000 I tried, but I didn't solve it.

pvginkel commented 7 years ago

This really should solve the issue of the PDF document being kept open.

Please note that pdfViewer1.Document and pdfViewer1.Render.Document always refer to the same document. You only have to dispose one.

The PdfiumViewer control does not dispose the PdfDocument itself. This is expected behavior and e.g. the same as what the built in .NET control PictureBox does. If you open an image from file using that control, it will also not close it when the control is disposed. As such you need to make sure that you dispose the PdfDocument yourself.

The advise is:

pdfViewer1.Document = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(form.FileName)));

Please note that this should solve your issue. PdfiumViewer does not keep a reference to the PdfDocument or file in any other place. Properly closing the PdfDocument instances you create should solve your issue.