[
https://issues.apache.org/jira/browse/PDFBOX-4669?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17003519#comment-17003519
]
Maruan Sahyoun commented on PDFBOX-4669:
----------------------------------------
Ok - let me describe in more detail what happens in current code
when I create a list of annotations like this
{code:java}
ArrayList<PDAnnotation> pageAnnots = new ArrayList<>();
PDAnnotationHighlight txtMark = new PDAnnotationHighlight();
PDAnnotationLink txtLink = new PDAnnotationLink();
pageAnnots.add(txtMark);
pageAnnots.add(txtMark);
pageAnnots.add(txtMark);
pageAnnots.add(txtLink);
{code}
the number of entries is 4
Removing entries from the list
{code:java}
pageAnnots.removeAll(Collections.singleton(pageAnnots.get(0)));
{code}
the number of entries is now 1 as txtMark is the same object
Adding the original list to the page and retrieving that
{code:java}
page.setAnnotations(pageAnnots);
List annotsFromPage = page.getAnnotations();
{code}
the number of entries is 4 as one would expect.
Removing from that list similar to above code
{code:java}
PDAnnotation toBeRemoved = (PDAnnotation) annotsFromPage.get(0);
annotsFromPage.removeAll(Collections.singletonList(toBeRemoved));
{code}
The number of entries is 3 as theses are newly created instances from the
COSArray entries and they are treated independently.
**but**
{code:java}
PDAnnotation instance0 = (PDAnnotation) annotsFromPage.get(0);
PDAnnotation instance1 = (PDAnnotation) annotsFromPage.get(1);
instance0.setRectangle(new PDRectangle(10f, 10f));
System.out.println(instance1.getRectangle());
{code}
prints {{[0.0,0.0,10.0,10.0]}} for {{instance2}} as they share the same
COSObject.
Although something like that might not be used in practice and currently only
happens in my unit tests I created for COSArrayList I think we should ensure
that this is handled consistently and preferably consistent to
{{java.util.List}}
IMHO as changing one instance does also change the other they should be treated
as the same mutable object.
WDYT?
Side note - longer term (very long) we might want to think about no longer
directly manipulating the COS model from PD but only create it as we save to
disk. Would also help with issues where one could directly manipulate COS which
does not update PD e.g. what happens if one removes an entry from a COSArray.
We would then have have two independent models where one could choose to use
COS or PD with the typical user using PD.
> Ensure proper functionality of COSArrayList
> -------------------------------------------
>
> Key: PDFBOX-4669
> URL: https://issues.apache.org/jira/browse/PDFBOX-4669
> Project: PDFBox
> Issue Type: Sub-task
> Components: PDModel
> Affects Versions: 2.0.17, 3.0.0 PDFBox
> Reporter: Maruan Sahyoun
> Assignee: Maruan Sahyoun
> Priority: Major
>
> The current COSArrayList implementation has some glitches as pointed out at
> https://stackoverflow.com/questions/45812696/
> In addition when working with {{AnnotationFilter}} as the
> adding/update/removal is index based the wron index of the underlying
> {{COSArray}} is affected.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]