4

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;
    }
SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
  • 1
    what is the exception call stack? – Matt Jun 23 '15 at 14:19
  • Msg: {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} – SurenSaluka Jun 23 '15 at 14:21
  • 1
    This is the exception message, not the call stack. – Matt Jun 23 '15 at 14:22
  • Stacktrace: at pdftron.PDF.PDFViewCtrl.Update() at com.IronOne.BoardPACWinApp.View.Page.NewsAndShardDocsPage.d__4d.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) – SurenSaluka Jun 23 '15 at 14:23

1 Answers1

4

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

Ryan
  • 2,473
  • 1
  • 11
  • 14