Skip to content

Commit 68f0516

Browse files
committed
PDFBOX-5943: add a method to remove all page resources from the cache
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1923474 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8c2393b commit 68f0516

File tree

1 file changed

+61
-0
lines changed
  • pdfbox/src/main/java/org/apache/pdfbox/pdmodel

1 file changed

+61
-0
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.util.ArrayList;
23+
import java.util.Collections;
2324
import java.util.Iterator;
2425
import java.util.List;
2526
import java.util.stream.Collectors;
@@ -46,6 +47,8 @@
4647
import org.apache.pdfbox.pdmodel.common.PDMetadata;
4748
import org.apache.pdfbox.pdmodel.common.PDRectangle;
4849
import org.apache.pdfbox.pdmodel.common.PDStream;
50+
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
51+
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
4952
import org.apache.pdfbox.pdmodel.interactive.action.PDPageAdditionalActions;
5053
import org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter;
5154
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
@@ -114,6 +117,64 @@ public PDPage(COSDictionary pageDictionary)
114117
this.resourceCache = resourceCache;
115118
}
116119

120+
/**
121+
* Remove all page resources from the cache to avoid relying on the implementation of the Cache. Does make sense
122+
* after processing a page.
123+
*/
124+
public void removePageResourceFromCache()
125+
{
126+
if (resourceCache == null)
127+
{
128+
return;
129+
}
130+
// limit purge operation to page resources, don't remove inherited resources
131+
removeResources(page.getCOSDictionary(COSName.RESOURCES));
132+
}
133+
134+
private void removeResources(COSDictionary resources)
135+
{
136+
if (resources == null)
137+
{
138+
return;
139+
}
140+
getIndirectResourceObjects(resources, COSName.COLORSPACE)
141+
.forEach(resourceCache::removeColorSpace);
142+
getIndirectResourceObjects(resources, COSName.EXT_G_STATE)
143+
.forEach(resourceCache::removeExtState);
144+
getIndirectResourceObjects(resources, COSName.FONT)
145+
.forEach(resourceCache::removeFont);
146+
getIndirectResourceObjects(resources, COSName.PATTERN)
147+
.forEach(resourceCache::removePattern);
148+
getIndirectResourceObjects(resources, COSName.PROPERTIES)
149+
.forEach(resourceCache::removeProperties);
150+
getIndirectResourceObjects(resources, COSName.SHADING)
151+
.forEach(resourceCache::removeShading);
152+
for (COSObject cosObject : getIndirectResourceObjects(resources, COSName.XOBJECT))
153+
{
154+
PDXObject removedXObject = resourceCache.removeXObject(cosObject);
155+
// clean up the resources of the XFormObject
156+
if (removedXObject instanceof PDFormXObject)
157+
{
158+
COSStream cosStream = removedXObject.getCOSObject();
159+
removeResources(cosStream.getCOSDictionary(COSName.RESOURCES));
160+
}
161+
}
162+
}
163+
164+
private List<COSObject> getIndirectResourceObjects(COSDictionary pageResources, COSName kind)
165+
{
166+
COSDictionary resourcesDictionary = pageResources.getCOSDictionary(kind);
167+
if (resourcesDictionary == null)
168+
{
169+
return Collections.emptyList();
170+
}
171+
return resourcesDictionary.getValues().stream() //
172+
.filter(f -> f instanceof COSObject) //
173+
.map(f -> (COSObject) f) //
174+
.filter(COSObject::isDereferenced) //
175+
.collect(Collectors.toList());
176+
}
177+
117178
/**
118179
* Convert this standard java object to a COS object.
119180
*

0 commit comments

Comments
 (0)