Get an exception when opening a file
Here's an interesting problem I encountered in SfPdfViewer: When I use the FileOpenPicker object to pick a file and open (using VS 2013, Windows 8.1), I am able to open the file and view it in the component as expected. However, if I pass a filename to the same function and do not use the FileOpenPicker, and instead, create the file object by using the GetFileAsync function, I get an unhandled exception *after* the file is loaded, with the exception message of "An item with the same key has already been added." The only difference in the code is how I get access to the file (GetFileAsync vs PickSingleFileAsync methods).
Not sure what's going on here, but below, please find my XAML and C# code pasted, for illustrating the problem. If I comment out the GetfileAsync line, and remove the comments from other lines, I can open and see the PDF contents w/ out a problem...
---START C# CODE-----
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.UI.Input;
using Windows.UI.Input.Inking;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Storage.Provider;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Popups;
using Windows.Storage.Pickers;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.UI.Input;
using Windows.UI.Input.Inking;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Storage.Provider;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Popups;
using Windows.Storage.Pickers;
using Syncfusion.Windows.PdfViewer;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace PDFTest
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class SyncfusionViewerTestPage : Page
{
public SyncfusionViewerTestPage()
{
this.InitializeComponent();
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class SyncfusionViewerTestPage : Page
{
public SyncfusionViewerTestPage()
{
this.InitializeComponent();
string fileName = "TestFile.pdf";
OpenFile(fileName);
}
OpenFile(fileName);
}
private async void OpenFile(string fileName)
{
//var picker = new FileOpenPicker();
//picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
//picker.ViewMode = PickerViewMode.List;
{
//var picker = new FileOpenPicker();
//picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
//picker.ViewMode = PickerViewMode.List;
//picker.FileTypeFilter.Add(".pdf");
StorageFolder folder = KnownFolders.DocumentsLibrary;
var file = await folder.GetFileAsync(fileName);
//var file = await picker.PickSingleFileAsync();
if (file == null) return;
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
Stream fileStream = stream.AsStreamForRead();
syncFusionPDFViewer.LoadDocument(fileStream);
var file = await folder.GetFileAsync(fileName);
//var file = await picker.PickSingleFileAsync();
if (file == null) return;
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
Stream fileStream = stream.AsStreamForRead();
syncFusionPDFViewer.LoadDocument(fileStream);
}
}
}
}
}
-----END C# CODE
---------------------------------------------------------
-----START XAML PAGE CODE----
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PDFTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sync="using:Syncfusion.Windows.PdfViewer"
x:Class="PDFTest.SyncfusionViewerTestPage"
mc:Ignorable="d">
--------END XAML PAGE CODE------------------------------
SIGN IN To post a reply.
3 Replies
BO
Bulent Ozbilgin
October 3, 2014 02:24 PM UTC
Interestingly, I found the answer on one of the other threads in the forum. Looks like somehow, timings are messed up when you try to open the file from the Initialize (or OnNavigatedTo) events; I've been testing a number of different PDF components the past week and had not come across this error before. Here's the solution I refactored from a previous post (changing the initialize function and adding a timer tick):
DispatcherTimer t;
public SyncfusionViewerTestPage()
{
this.InitializeComponent();
public SyncfusionViewerTestPage()
{
this.InitializeComponent();
t = new DispatcherTimer();
t.Tick += t_Tick;
t.Interval = new TimeSpan(0, 0, 1);
t.Start();
}
t.Tick += t_Tick;
t.Interval = new TimeSpan(0, 0, 1);
t.Start();
}
private void t_Tick(object sender, object e)
{
string fileName = "TestFile.pdf";
OpenFile(fileName);
t.Stop();
}
{
string fileName = "TestFile.pdf";
OpenFile(fileName);
t.Stop();
}
.....Rest of the code is same as above.
Interestingly, I found the answer on one of the other threads in the forum. Looks like somehow, timings are messed up when you try to open the file from the Initialize (or OnNavigatedTo) events; I've been testing a number of different PDF components the past week and had not come across this error before. Here's the solution I refactored from a previous post (changing the initialize function and adding a timer tick):DispatcherTimer t;
public SyncfusionViewerTestPage()
{
this.InitializeComponent();t = new DispatcherTimer();
t.Tick += t_Tick;
t.Interval = new TimeSpan(0, 0, 1);
t.Start();
}private void t_Tick(object sender, object e)
{
string fileName = "TestFile.pdf";
OpenFile(fileName);
t.Stop();
}.....Rest of the code is same as above.
UPDATE: Something weird is going on in this component. I added margin information to the XAML file, for the PDF Viewer element: Margin="0,92,0,0". Other than that, I did not change anything else in the code. With that margin information in place, the PDF viewer crashes the application (everything else is constant). When I remove the margin, the PDF is loaded and becomes visible, as expected.
What is the issue here? Seems like a pretty big bug.
SM
Suresh M
Syncfusion Team
October 7, 2014 07:11 AM UTC
Hi Bulent,
Thank you
for your interest in Syncfusion products.
We are able
to reproduce the issue “Document could not be loaded into the Viewer during
initialization”. We have created a direct trac incident (#130472) for this
issue, please follow up the incident for its resolution.
Regarding
setting the margin, we are not able to reproduce the issue, herewith we have
attached a sample with which we tried to reproduce the issue. Please modify
this sample or provide us a simple sample with which this issue could be
reproduced.
http://www.syncfusion.com/downloads/support/directtrac/130472/PDFViewerWintRT_2013-159545140.zip
Please let
us know if you need any further assistance.
Regards,
Suresh
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
BO Bulent Ozbilgin
- Oct 3, 2014 02:10 PM UTC
- Oct 7, 2014 07:11 AM UTC