@@ -749,7 +749,8 @@ void checkWithNumberTree(PDDocument document) throws IOException
749749 }
750750 }
751751 // actual count may be larger if last element is null, e.g. PDFBOX-4408
752- assertTrue (set .last () <= array .size () - 1 );
752+ // set can be empty, see last page of pdf_32000_2008.pdf
753+ assertTrue (set .isEmpty () || set .last () <= array .size () - 1 );
753754 }
754755 for (PDAnnotation ann : page .getAnnotations ())
755756 {
@@ -874,6 +875,34 @@ private void checkForPageOrphans(PDDocument doc) throws IOException
874875 assertNotNull (structureTreeRoot .getK ());
875876 checkElement (pageTree , structureTreeRoot .getK (), structureTreeRoot .getCOSObject ());
876877 checkForIDTreeOrphans (pageTree , structureTreeRoot );
878+ checkParentTreeAgainstK (structureTreeRoot );
879+ }
880+
881+ private void checkParentTreeAgainstK (PDStructureTreeRoot structureTreeRoot ) throws IOException
882+ {
883+ // check that elements in the /ParentTree are in the /K tree
884+ ElementCounter elementCounter = new ElementCounter ();
885+ elementCounter .walk (structureTreeRoot .getK ());
886+ Map <Integer , COSObjectable > numberTreeAsMap = PDFMergerUtility .getNumberTreeAsMap (structureTreeRoot .getParentTree ());
887+ for (Map .Entry <Integer , COSObjectable > entry : numberTreeAsMap .entrySet ())
888+ {
889+ PDParentTreeValue val = (PDParentTreeValue ) entry .getValue (); // array or dictionary
890+ COSBase base = val .getCOSObject ();
891+ if (base instanceof COSArray )
892+ {
893+ COSArray array = (COSArray ) base ;
894+ for (int i = 0 ; i < array .size (); ++i )
895+ {
896+ COSBase arrayElement = array .getObject (i );
897+ if (arrayElement instanceof COSDictionary )
898+ {
899+ assertTrue (elementCounter .set .contains (arrayElement ),
900+ "Element " + entry .getKey () + ":" + i + " from /ParentTree missing in /K " );
901+ }
902+ }
903+ }
904+ // can't check this COSDictionary; ElementsCounter only counts those with a /Pg entry
905+ }
877906 }
878907
879908 private void checkForIDTreeOrphans (PDPageTree pageTree , PDStructureTreeRoot structureTreeRoot )
0 commit comments