Issue re-opening a PDF document too quickly.

Hi all,

I'm trying to create a react native wrapper for PDFNet and have run into an issue on UWP. If I try to reopen the same PDF document quickly I am getting an access exception when I try to create the stream to pass to the PDFDoc() constructor. When the component unmounts, I am trying to close and clean up the PDFDoc and PDFViewCtrl as completely as possible. It definitely seems to be timing related as delays will always allow it to succeed. I'm wondering if there is something that is in PdfViewCtrl.closeDoc() or PDFDoc.Dispose that is holding the stream for just long enough to be an issue?

Code is shown below. I'm aware that I don't have error handling in the code yet. That will be added but right now I'm just trying to figure out the basics of integrating it with React Native. Any ideas how I might be able to avoid the issue? Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using ReactNative.UIManager;
using ReactNative.UIManager.Annotations;
using pdftron.PDF;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media;

namespace Com.ObjectPartners.OPIPdfNet
{
    public class OPIPdfNetViewManager : SimpleViewManager<Border>
    {
        public override string Name => "OPIPdfNet";

        public OPIPdfNetViewManager() : base() => pdftron.PDFNet.Initialize();

        public override void OnDropViewInstance(ThemedReactContext reactContext, Border view)
        {
            base.OnDropViewInstance(reactContext, view);
            PDFViewCtrl pdfView = (PDFViewCtrl)view.Child;
            PDFDoc doc = pdfView.GetDoc();
            pdfView.CloseDoc();
            doc.Dispose();
            pdfView.FreeResources();
        }

        protected override Border CreateViewInstance(ThemedReactContext reactContext) => new Border
        {
            Background = new SolidColorBrush(Colors.Green),
            Child = new PDFViewCtrl()
        };

        [ReactProp("src")]
        public async void SetSourceAsync(Border view, string source)
        {

            StorageFile file = await StorageFile.GetFileFromPathAsync(source);

            // Create a PDFDocument and use it as the source for the PDFViewCtrl
            if (file != null)
            {
                Windows.Storage.Streams.IRandomAccessStream stream = await
                    file.OpenAsync(FileAccessMode.ReadWrite);
                PDFDoc doc = new PDFDoc(stream);
                PDFViewCtrl pdfView = (PDFViewCtrl)view.Child;
                pdfView.SetDoc(doc);
            }

        }
    }
}

Hi Steve,

Your code looks good. Can you reproduce this issue with our sample code? Or does this only happen in your project?

Shakthi,

Thanks Shakthi, I can’t really reproduce the problem with the sample code because I have to go through the dialog box to reopen the file and it takes enough time that I am not able to cause the issue to happen. My code above is being used along with a grid of icons for the PDF documents so I can close and re-open the file rapidly. I did find if I skip straight to passing the StorageFile reference to the PDFDoc() constructor it makes the issue impossible to reproduce on my code as well. Maybe that helps shed some light on what might be going on?

Thanks,
Steve

So you don’t get the issue if you use the StorageFile object directly with the PDFDoc constructor?

PDFDoc(StorageFile)

And that the issue occurs with

PDFDoc(IRandomAccessStream)

If so, does anything prevent you from using the former?

Yes, that’s right. I don’t see the issue when using the constructor with the StorageFile directly. There’s nothing that stops me from using it since the files are local on the device. I had just not found that signature and tried it out before I posted initially. I had just gotten it switched and tested a few hours before I read your post and replied. Thanks!