Skip to content

Commit f6501d7

Browse files
committed
PDFBOX-6009: remove structure elements without /Pg entry if there is at least one MCID
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1925586 13f79535-47bb-0310-9956-ffa450edef68
1 parent d277088 commit f6501d7

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.pdfbox.cos.COSArray;
3232
import org.apache.pdfbox.cos.COSBase;
3333
import org.apache.pdfbox.cos.COSDictionary;
34+
import org.apache.pdfbox.cos.COSInteger;
3435
import org.apache.pdfbox.cos.COSName;
3536
import org.apache.pdfbox.cos.COSObject;
3637
import org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction;
@@ -408,23 +409,45 @@ private COSBase createDictionaryClone(COSBase src, COSBase dstParent, COSDiction
408409
{
409410
return dstDict;
410411
}
412+
COSDictionary srcPageDict = srcDict.getCOSDictionary(COSName.PG);
411413
COSDictionary dstPageDict = null;
412-
if (srcDict.containsKey(COSName.PG))
414+
COSBase kid = srcDict.getDictionaryObject(COSName.K);
415+
if (srcPageDict != null)
413416
{
414-
COSDictionary srcPageDict = srcDict.getCOSDictionary(COSName.PG);
415-
if (srcPageDict == null)
416-
{
417-
return null;
418-
}
419417
dstPageDict = pageDictMap.get(srcPageDict);
420-
if (dstPageDict == null)
418+
if (dstPageDict != null)
421419
{
422-
return null;
420+
PDPage dstPage = new PDPage(dstPageDict);
421+
if (dstPageTree.indexOf(dstPage) == -1)
422+
{
423+
return null;
424+
}
423425
}
424-
PDPage dstPage = new PDPage(dstPageDict);
425-
if (dstPageTree.indexOf(dstPage) == -1)
426+
else
426427
{
427-
return null;
428+
// PDFBOX-6009: quit if MCIDs because these need a /Pg entry
429+
boolean hasMCIDs = false;
430+
if (kid instanceof COSInteger)
431+
{
432+
hasMCIDs = true;
433+
}
434+
else if (kid instanceof COSArray)
435+
{
436+
COSArray ar = (COSArray) kid;
437+
for (int i = 0; i < ar.size(); ++i)
438+
{
439+
if (ar.getObject(i) instanceof COSInteger)
440+
{
441+
hasMCIDs = true;
442+
break;
443+
}
444+
}
445+
}
446+
if (hasMCIDs)
447+
{
448+
return null;
449+
}
450+
// else keep this as an intermediate element for now
428451
}
429452
}
430453

@@ -466,7 +489,6 @@ private COSBase createDictionaryClone(COSBase src, COSBase dstParent, COSDiction
466489
}
467490

468491
dstDict.setItem(COSName.PG, dstPageDict);
469-
COSBase kid = srcDict.getDictionaryObject(COSName.K);
470492

471493
// stack overflow here with 207658.pdf, too complex
472494
COSBase cloneKid = createClone(kid, dstDict, dstPageDict != null ? dstPageDict : currentPageDict);

0 commit comments

Comments
 (0)