Why PDFTron PDFViewCtrl.Update() throws AccessViolationException

944 views Asked by At

I'm using PDFTron PDF annotation library in my windows store app project. Sometimes when I tried to load previously done annotations (save as a separate file) into the PDFView Control it throws AccessViolationException. Please help.

On my page this is the code:

await ImportAnnotations(param.Doc, agendaItem);
this.PDFViewCtrl.Update(); //this is where the exception throws

This is ImportAnnotations function.

private async Task<PDFDoc> ImportAnnotations(PDFDoc doc, AgendaItem GlobalSelectdAgendaItem)
    {
        try
        {
            if (doc != null && GlobalSelectdAgendaItem != null)
            {
                StorageFolder documentsFolder = await StorageFolder.GetFolderFromPathAsync(Path.Combine(ApplicationData.Current.LocalFolder.Path, Global.UserId.ToString(), ApplicationConstants.PDF));
                string annotationFileName = GlobalSelectdAgendaItem.ID + "_" + Global.UserId.ToString() + "_" + GlobalSelectdAgendaItem.VersionId + ".xfdf";

                // load XFDF annotations
                var anntotationFile = await documentsFolder.TryGetItemAsync(annotationFileName) as IStorageFile;
                FDFDoc fdfDoc = null;
                if (anntotationFile != null)
                {
                    fdfDoc = await FDFDoc.CreateFromXFDFAsync(Path.Combine(documentsFolder.Path, annotationFileName));
                }
                else
                {
                    return doc;
                }

                // load PDF with which to merge the annotations
                doc.InitSecurityHandler();

                // merge in the annotations
                doc.FDFMerge(fdfDoc);
                doc.RefreshFieldAppearances();
            }
        }
        catch (Exception)
        {
        }
        return doc;
    }
1

There are 1 answers

0
Ryan On BEST ANSWER

You are modifying the document while it is being rendered in a background thread. Writing to shared data across threads, while also reading them, can cause random errors as you encountered.

You need to start and finish your ImportAnnotations function with this.PDFViewCtrl.DocLock(true) and this.PDFViewCtrl.DocUnlock()

See this forum post for more details on this topic