Skip to content

Commit

Permalink
PDFBOX-5481: wrap indirect objects whenever they are added directly t…
Browse files Browse the repository at this point in the history
…o a COSDictionary or COSArray

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1919511 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Jul 25, 2024
1 parent 54b327b commit b03d12d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
48 changes: 38 additions & 10 deletions pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,18 @@ private COSArray(ArrayList<COSBase> cosObjects, boolean direct)
*/
public void add( COSBase object )
{
objects.add( object );
getUpdateState().update(object);
if ((object instanceof COSDictionary || object instanceof COSArray) && !object.isDirect()
&& object.getKey() != null)
{
COSObject cosObject = new COSObject(object, object.getKey());
objects.add(cosObject);
getUpdateState().update(cosObject);
}
else
{
objects.add(object);
getUpdateState().update(object);
}
}

/**
Expand All @@ -98,8 +108,7 @@ public void add( COSObjectable object )
{
base = object.getCOSObject();
}
objects.add(base);
getUpdateState().update(base);
add(base);
}

/**
Expand All @@ -111,8 +120,18 @@ public void add( COSObjectable object )
*/
public void add( int i, COSBase object)
{
objects.add( i, object );
getUpdateState().update(object);
if ((object instanceof COSDictionary || object instanceof COSArray) && !object.isDirect()
&& object.getKey() != null)
{
COSObject cosObject = new COSObject(object, object.getKey());
objects.add(i, cosObject);
getUpdateState().update(cosObject);
}
else
{
objects.add(i, object);
getUpdateState().update(object);
}
}

/**
Expand Down Expand Up @@ -200,8 +219,18 @@ public void addAll( int i, Collection<COSBase> objectList )
*/
public void set( int index, COSBase object )
{
objects.set( index, object );
getUpdateState().update(object);
if ((object instanceof COSDictionary || object instanceof COSArray) && !object.isDirect()
&& object.getKey() != null)
{
COSObject cosObject = new COSObject(object, object.getKey());
objects.set(index, cosObject);
getUpdateState().update(cosObject);
}
else
{
objects.set(index, object);
getUpdateState().update(object);
}
}

/**
Expand Down Expand Up @@ -229,8 +258,7 @@ public void set( int index, COSObjectable object )
{
base = object.getCOSObject();
}
objects.set( index, base );
getUpdateState().update(base);
set(index, base);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,18 @@ public void setItem(COSName key, COSBase value)
}
else
{
items.put(key, value);
getUpdateState().update(value);
if ((value instanceof COSDictionary || value instanceof COSArray) && !value.isDirect()
&& value.getKey() != null)
{
COSObject cosObject = new COSObject(value, value.getKey());
items.put(key, cosObject);
getUpdateState().update(cosObject);
}
else
{
items.put(key, value);
getUpdateState().update(value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,7 @@ protected PDDocument createNewDocument() throws IOException
destCatalog.setViewerPreferences(sourceCatalog.getViewerPreferences());
destCatalog.setLanguage(sourceCatalog.getLanguage());
destCatalog.setMarkInfo(sourceCatalog.getMarkInfo());
// copy the COS-object itself instead of the PD-object to avoid a malformed result
COSObject metaData = sourceCatalog.getCOSObject().getCOSObject(COSName.METADATA);
if (metaData != null)
{
destCatalog.getCOSObject().setItem(COSName.METADATA, metaData);
}
destCatalog.setMetadata(sourceCatalog.getMetadata());
return document;
}

Expand Down

0 comments on commit b03d12d

Please sign in to comment.