ArgumentOutOfRange Exception in Comment.AddReply

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

ArgumentOutOfRange Exception in Comment.AddReply

Post by IntegraMike »

Hello, I'm currently working with adding replies to annotations and in the scenario where I create an annotation, then without saving the document add a reply to it one of the internal collections is throwing.

The stack trace is:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.ObjectModel.Collection`1.Insert(Int32 index, T item)
at Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationViewCollection.?(Object A_0, CollectionChangeEventArgs`1 A_1)
at Vintasoft.Imaging.CollectionChangeEventHandler`1.Invoke(Object sender, CollectionChangeEventArgs`1 e)
at Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeList`1.OnChanged(CollectionChangeEventArgs`1 e)
at Vintasoft.Imaging.Pdf.Tree.PdfTreeNodeList`1.Insert(Int32 index, T item)
at Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList.Insert(Int32 index, PdfAnnotation item)
at Vintasoft.Imaging.Annotation.Comments.Pdf.PdfAnnotationCommentCollection.Add(Color color, String userName, String stateModel, String parentState)
at Vintasoft.Imaging.Annotation.Comments.Comment.AddReply(Color color, String userName)

and the code I'm running is:
Comment reply = comment.AddReply(Color.Green, "MyUserName");

Do you have any idea as to why this might be happening? It seems to be fine if the annotation being replied to has been saved and the document reloaded after that save.

Thanks!
Alex
Site Admin
Posts: 2313
Joined: Thu Jul 10, 2008 2:21 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by Alex »

Hello Mike,

For understanding the problem we need to reproduce the problem on our side. Please send us (to support@vintasoft.com) a small project, which allows to reproduce the problem.

Best regards, Alexander
Alex
Site Admin
Posts: 2313
Joined: Thu Jul 10, 2008 2:21 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by Alex »

Hello Mike,

Have you solved the problem? We still not received project from you.

Best regards, Alexander
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by IntegraMike »

Not yet, I have been unable to reproduce the issue in a small project.

It looks like the following is involved though:
Adding an annotation to the page via: page.Annotations.Add(PdfAnnotation); (or some kind of error condition when doing this).
Possibly an issue creating the Comment for the new annotation when it is added this way.

I haven't had time the last couple days to try and build an independent project reproducing this issue, but that's what research has shown so far.
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by IntegraMike »

A minor update on this, it may have something to do with threading and creating the comment for a new annotation.

Calling
AnnotationTool.FocusedPage.Annotations.Add(annotation);
annotation.UpdateAppearance();

seems to trigger this issue for us. It isn't 100% of the time and the faster you add annotations the more likely it is to happen. We didn't see this before implementing Comments/Popups, and it appears one is built in the background for every new annotation, so I'm guessing this is where we're running into trouble.

I am not seeing it in your demo app however and I now have time to dig into this so I will be today/tomorrow and will hopefully have something soon. In the meantime if you have any guidance it would be appreciated.

Thanks,
Mike
Alex
Site Admin
Posts: 2313
Joined: Thu Jul 10, 2008 2:21 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by Alex »

Hello Mike,

Thank you for information.

For suggesting the solution we need to understand the problem. For understanding the problem we need to reproduce the problem on our side. Please send us (to support@vintasoft.com) a small project, which allows to reproduce the problem, or let us know how to reproduce the problem using our demos.

Best regards, Alexander
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by IntegraMike »

Unfortunately I don't think I'm going to be able to provide you with the requested project in a timely matter. However, the relevant code is related to the fact that we're adding annotations to the document in 2 different ways.

1) Create something like an Ink annotation in the same way it's done in your demo app and call:
AnnotationTool.AddAndBuildAnnotation(annotation);

2) Create something like a FreeText annotation and add it directly to the collection:
AnnotationTool.FocusedPage.Annotations.Add(annotation);
annotation.UpdateAppearance();

When sticking to annotating using only one method, everything is fine.
When using the 2nd method and then the first, everything is fine.
When using the 1st method and then the second, we get the index out of range issue every time.

The problem is, I cannot reproduce this behavior in your demo application code, and the project I am reproducing it in is too large and proprietary to pass along.

As stated before I suspect this has to do with some kind of back end threading code that is related to creating the comment annotations for things created using the first method, but that's all the information I can provide you with at this time (aside from the stack trace which should get you close to the issue). This is pure speculation, but since it's an index out of bounds range in one of your collection classes I suspect that one of your collections is referencing another collection that doesn't have the referenced entry added yet because the addition is happening on another thread and it isn't complete.

If you have any guidance on how to reproduce the issue I will look into doing so further, but thus far I haven't had any luck. It is worth noting that I'm not modifying any collection in this process with the exception of adding the annotation to the collection in method 2, and the error occurs when calling UpdateAppearance in method 2.

Another possible solution for us would be not using AnnotationTool.AddAndBuildAnnotation, so if you could point me in the direction of an example of using the pencil tool to draw and then adding the resulting annotation to the document with the second method that would at least give us a workaround.
Ex.
PdfInkAnnotation annotation = new PdfInkAnnotation(AnnotationTool.FocusedPage);
<Fill In Here with how to draw the annotation>
AnnotationTool.FocusedPage.Annotations.Add(annotation);
annotation.UpdateAppearance();

I hope what I have provided is helpful and I appreciate any help you can provide us.

Thanks,
Mike
Alex
Site Admin
Posts: 2313
Joined: Thu Jul 10, 2008 2:21 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by Alex »

Hello Mike,

We tried to reproduce the problem without success. For understanding the problem we need to reproduce the problem on our side.

Best regards, Alex
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by IntegraMike »

I still haven't been able to get this to repro in a shareable project, but I do have some additional information that may help down the line.

We're suspecting this might be a threading issue on our side (we're trying to deduce where) but what's happening is we're using a CompositeVisualTool to combine a PdfAnnotationTool and a CommentVisualTool to fulfill our needs and somehow the AnnotationCollection and the AnnotationViewCollection on the annotation tool are getting out of sync. When I check the counts when we manage to get into the bad state they are not equivalent. When I attempt to add the missing entries, it throws an "item is already in the list". So what I think is going on is the two lists are dependent on each other and when one updates, sometimes the other is being on a thread blocked and doesn't update until that clears or it is specifically accessed and does a refresh.

I have a hack in our code that works around this be comparing size and if they're different I try and add an element and then am swallowing the error that throws which gets us around the issue for now but isn't an ideal solution.

When I get some time I'll try again to get you guys a project, but this is what I know right now. That might be a little while though because we're in crunch time before a release currently.

Thanks for all the help!
Alex
Site Admin
Posts: 2313
Joined: Thu Jul 10, 2008 2:21 pm

Re: ArgumentOutOfRange Exception in Comment.AddReply

Post by Alex »

Hello Mike,

Thank you for information. We will wait for a project from you.

Best regards, Alexander
Post Reply