stephanrauh / ngx-extended-pdf-viewer

A full-blown PDF viewer for Angular 16, 17, and beyond
https://pdfviewer.net
Apache License 2.0
449 stars 167 forks source link

Saving and reapplying annotations inconsistency #2355

Open DayibBaba opened 3 weeks ago

DayibBaba commented 3 weeks ago

I use getSerializedAnnotations() to save all annotations to a annotations: EditorAnnotation[] variable. In turn, I save this as a JSON file on my database (firebase storage). After fetching it back and using addEditorAnnotation() to reapply the annotations, they always seem to have moved just a little upwards. All annotations are pasted back and look to be higher in position then beforehand. This is especially noticeable when highlighting a specific line in the document. Looking into the save data and comparing the values does show differences in the coordinates of the annotations. So somewhere in between saving and reapplying, I am getting inconsistent values in a consistent manner (it always moves slightly upwards).

Before reapplication: image

After: image

EDIT: Not saving and simply reapplying seems to work correctly. When an annotation is serialized again, and saved for reapplication, is where the issue seems to occur.

DayibBaba commented 3 weeks ago

@stephanrauh nuisance? Is there a simple work-around in which I can adjust for the apparent offsets that occur within getSerializedAnnotations()?

stephanrauh commented 3 weeks ago

OK, you're right - it's more than a nuisance. It's not as bad as many other bug because it almost works, but you're right, "almost" isn't good enough.

Your bug rings a bell. Coordinates are notoriously difficult with the annotation API. In particular, I noticed that there seems to be an offset shifting annotations upward - pretty much as you've reported. I assume that simply means I haven't understood the coordinates yet. Adding insult to injury, we're talking about an internal API of pdf.js that may change at any point in time. I hope it doesn't, but it happened in the past.

At the moment, I don't know how to proceed. Can I talk you into helping me? I know that's mean because it's you who's asking me for help, but if I give you some clues how to debug, would you like to do it?

DayibBaba commented 3 weeks ago

OK, you're right - it's more than a nuisance. It's not as bad as many other bug because it almost works, but you're right, "almost" isn't good enough.

Your bug rings a bell. Coordinates are notoriously difficult with the annotation API. In particular, I noticed that there seems to be an offset shifting annotations upward - pretty much as you've reported. I assume that simply means I haven't understood the coordinates yet. Adding insult to injury, we're talking about an internal API of pdf.js that may change at any point in time. I hope it doesn't, but it happened in the past.

At the moment, I don't know how to proceed. Can I talk you into helping me? I know that's mean because it's you who's asking me for help, but if I give you some clues how to debug, would you like to do it?

I am in the middle of a project but, maybe I could look into it. I have already cloned your repo to check your code, haven't looked too deep yet.