Closed pedrodemaria closed 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?
I have encountered the same problem, how did you solve it?
@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(); } }
Thank you. I'll try.
@pedrohas2000 I tried, but I didn't solve it.
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:
Document
property, make sure that you Dispose
whatever's there before you assign it;Dispose
the contents of the Document
property;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.
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).