How to open a pdf a second time?

62 views
Skip to first unread message

Smurfie

unread,
Jan 13, 2015, 10:33:31 AM1/13/15
to pdfne...@googlegroups.com
How do I have to close a document? Because if I open the same pdf-file twice I get an System.UnauthorizedAccessException. (Doc A --> Doc B works, but Doc A --> Doc A crashes).

The crash happens on StorageFile.GetFileFromPathAsync(path) because the file is still open.

Here is my Code

private async void OpenPage(String path)
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);

if (file != null)
{
IRandomAccessStream stream = await
file.OpenAsync(FileAccessMode.ReadWrite);
if (doc != null) {
doc.Dispose();
}
doc = new pdftron.PDF.PDFDoc(stream);
MyPDFViewCtrl.SetDoc(doc);

}
}

Ryan

unread,
Jan 13, 2015, 1:48:52 PM1/13/15
to pdfne...@googlegroups.com
Since PDFDoc has a handle on a system file resource, you cannot rely on the GC to release that for you, since it might be a long time before that happens. Instead you need to force the PDFDoc.Dispose(), by either calling it explicitly, or through a 'using' statement.

You should look the PDFViewCtrl sample that comes with the SDK, and look at the Close Doc method (not sure the exact name), it has the code you want.

It is something along these lines.

auto oldDoc = MyPDFViewCtrl.GetDoc();
MyPDFViewCtrl.SetDoc(newDoc);
if(oldDoc != null)
{
oldDoc.Dispose();
}


Reply all
Reply to author
Forward
0 new messages