1

The following code causes a System.InvalidOperationException because the file in question is still in use. There isn't clear guidance in the PDFTron documentation on how one should close a file, but the following yields the exception:

// in my dispose method. _pdfViewWpf is a pdftron.PDF.PDFViewWPF, and _pdfPath is the filepath I set the doc to. 
_pdfViewWpf.CloseDoc();
_pdfViewWpf.Dispose();

File.Move(_pdfPath, @"C:\myFilePath\Test.pdf");

What am I missing so that PDFTron properly releases and closes the file?

2 Answers2

2

I was missing a Dispose() call on the PDFDoc I had set as the Doc on the PDFViewWPF. I saved that in a private member variable:

_pdfDoc = new PDFDoc(pdfPath);

And added this line to my dispose method:

_pdfViewWpf.CloseDoc();
_pdfViewWpf.Dispose();
if (_pdfDoc != null) _pdfDoc.Dispose();

File.Move(_pdfPath, @"C:\myFilePath\Test.pdf");

Now the File.Move method works without throwing an exception.

1

Thank you for the feedback on our guides, we will look to improve this part. We do have a lot of sample code in our sample projects, so there you could also see how to do things in practice.

For non-interactive PDF processing, a using statement is the easiest way to ensure file handles and memory are released right away, when done with a PDFDoc instance.

For interactive viewing, you can do the following.

PDFDoc oldDoc = _pdfViewWpf.GetDoc();
_pdfViewWpf.CloseDoc();
// or instead of above line, if you want to reuse the viewer call 
//_pdfViewWpf.SetDoc(otherPDFDoc);
if(oldDoc != null) oldDoc.Close();
Ryan
  • 2,473
  • 1
  • 11
  • 14