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);
}
}
}
}