Q: I am using the PDFNet SDK for Windows Store apps. I am opening an image file with the file picker and now I want to add it to the PDFDoc. When I try to use Image.Create(Pdfdoc,file.path) I get an exception as follows:
‘System.Runtime.InteropServices.COMException’ occurred in poc_pdfTron.exe WinRT information: Exception:
Message: Unable to open the file
Conditional expression: m_stream != NULL
Filename : StdFile.cpp
Function : trn::Filters::StdFile::InitW
Linenumber : 210
How do I get around this?
So, what you would have to do is to copy the file to the apps sandbox before you attempt to use it. We suggest using Windows.Storage.ApplicationData.Current.TemporaryFolder, and then using the path of the image there.
Here’s some sample code:
// Assume you already have a PDFDoc called Pdfdoc
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.ViewMode = PickerViewMode.List;
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
string tempName = “tempfile” + System.IO.Path.GetExtension(file.Name);
string fullFileName = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.TemporaryFolder.Path, tempName);
await file.CopyAsync(Windows.Storage.ApplicationData.Current.TemporaryFolder, tempName, NameCollisionOption.ReplaceExisting);
Image image= Image.Create(Pdfdoc,fullFileName);
A: In Windows Store apps, files outside the apps sandbox can not be accessed through paths, even if they were previously opened with the file picker. The StorageFile is the only interface by which to read any files not in the sandbox.