How do I add an image from the file picker to a PDFDoc in Windows Store apps

83 views
Skip to first unread message

Tomas Hofmann

unread,
Dec 4, 2013, 2:02:11 PM12/4/13
to pdfne...@googlegroups.com
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?


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.
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;
fileOpenPicker.FileTypeFilter.Clear();
fileOpenPicker.FileTypeFilter.Add(".png");
StorageFile file = await fileOpenPicker.PickSingleFileAsync();

if(file!=null){
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);
}
Reply all
Reply to author
Forward
0 new messages