2626import com .itextpdf .kernel .xmp .XMPUtils ;
2727import com .itextpdf .kernel .xmp .options .SerializeOptions ;
2828
29- import javax .xml .parsers .DocumentBuilder ;
30- import javax .xml .parsers .DocumentBuilderFactory ;
31- import javax .xml .parsers .ParserConfigurationException ;
32- import javax .xml .transform .OutputKeys ;
33- import javax .xml .transform .Transformer ;
34- import javax .xml .transform .TransformerException ;
35- import javax .xml .transform .TransformerFactory ;
36- import javax .xml .transform .dom .DOMSource ;
37- import javax .xml .transform .stream .StreamResult ;
3829import java .io .BufferedReader ;
3930import java .io .ByteArrayInputStream ;
4031import java .io .File ;
4940import java .util .Arrays ;
5041import java .util .Comparator ;
5142import java .util .LinkedHashMap ;
43+ import java .util .LinkedHashSet ;
5244import java .util .List ;
5345import java .util .Map ;
5446import java .util .Set ;
5547import java .util .Stack ;
5648import java .util .StringTokenizer ;
5749import java .util .TreeSet ;
50+ import javax .xml .parsers .DocumentBuilder ;
51+ import javax .xml .parsers .DocumentBuilderFactory ;
52+ import javax .xml .parsers .ParserConfigurationException ;
53+ import javax .xml .transform .OutputKeys ;
54+ import javax .xml .transform .Transformer ;
55+ import javax .xml .transform .TransformerException ;
56+ import javax .xml .transform .TransformerFactory ;
57+ import javax .xml .transform .dom .DOMSource ;
58+ import javax .xml .transform .stream .StreamResult ;
5859
5960import org .w3c .dom .Document ;
6061import org .w3c .dom .Element ;
@@ -285,7 +286,6 @@ public String compareLinkAnnotations(String outPdf, String cmpPdf) throws IOExce
285286 return message ;
286287 }
287288
288-
289289 public String compareTagStructures (String outPdf , String cmpPdf ) throws IOException , ParserConfigurationException , SAXException {
290290 System .out .print ("[itext] INFO Comparing tag structures......" );
291291
@@ -573,12 +573,11 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
573573 equalPages .add (i );
574574 }
575575
576-
577- compareCatalogEntry (outDocument , cmpDocument , compareResult , PdfName .StructTreeRoot );
578- compareCatalogEntry (outDocument , cmpDocument , compareResult , PdfName .OCProperties );
579- compareCatalogEntry (outDocument , cmpDocument , compareResult , PdfName .Names );
580- compareCatalogEntry (outDocument , cmpDocument , compareResult , PdfName .AcroForm );
581-
576+ ObjectPath catalogPath = new ObjectPath (cmpDocument .getCatalog ().getPdfObject ().getIndirectReference (),
577+ outDocument .getCatalog ().getPdfObject ().getIndirectReference ());
578+ Set <PdfName > ignoredCatalogEntries = new LinkedHashSet <>(Arrays .asList (PdfName .Pages , PdfName .Metadata ));
579+ compareDictionariesExtended (outDocument .getCatalog ().getPdfObject (), cmpDocument .getCatalog ().getPdfObject (),
580+ catalogPath , compareResult , ignoredCatalogEntries );
582581
583582 outDocument .close ();
584583 cmpDocument .close ();
@@ -606,14 +605,6 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
606605 }
607606 }
608607
609- private void compareCatalogEntry (PdfDocument outDocument , PdfDocument cmpDocument , CompareResult compareResult , PdfName entryName ) throws IOException {
610- PdfObject outEntry = outDocument .getCatalog ().getPdfObject ().get (entryName );
611- PdfObject cmpEntry = cmpDocument .getCatalog ().getPdfObject ().get (entryName );
612- PdfIndirectReference cmpStructTreeRef = cmpEntry == null ? null : cmpEntry .getIndirectReference ();
613- PdfIndirectReference outStructTreeRef = outEntry == null ? null : outEntry .getIndirectReference ();
614- compareObjects (outEntry , cmpEntry , new ObjectPath (cmpStructTreeRef , outStructTreeRef ), compareResult );
615- }
616-
617608 private void loadPagesFromReader (PdfDocument doc , List <PdfDictionary > pages , List <PdfIndirectReference > pagesRef ) {
618609 int numOfPages = doc .getCatalog ().getNumberOfPages ();
619610 for (int i = 0 ; i < numOfPages ; ++i ) {
@@ -640,8 +631,11 @@ private boolean compareStreams(InputStream is1, InputStream is2) throws IOExcept
640631 return true ;
641632 }
642633
643-
644634 private boolean compareDictionariesExtended (PdfDictionary outDict , PdfDictionary cmpDict , ObjectPath currentPath , CompareResult compareResult ) throws IOException {
635+ return compareDictionariesExtended (outDict , cmpDict , currentPath , compareResult , null );
636+ }
637+
638+ private boolean compareDictionariesExtended (PdfDictionary outDict , PdfDictionary cmpDict , ObjectPath currentPath , CompareResult compareResult , Set <PdfName > excludedKeys ) throws IOException {
645639 if (cmpDict != null && outDict == null || outDict != null && cmpDict == null ) {
646640 compareResult .addError (currentPath , "One of the dictionaries is null, the other is not." );
647641 return false ;
@@ -651,6 +645,9 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
651645 Set <PdfName > mergedKeys = new TreeSet <>(cmpDict .keySet ());
652646 mergedKeys .addAll (outDict .keySet ());
653647 for (PdfName key : mergedKeys ) {
648+ if (excludedKeys != null && excludedKeys .contains (key )) {
649+ continue ;
650+ }
654651 if (key .equals (PdfName .Parent ) || key .equals (PdfName .P ) || key .equals (PdfName .ModDate )) continue ;
655652 if (outDict .isStream () && cmpDict .isStream () && (key .equals (PdfName .Filter ) || key .equals (PdfName .Length ))) continue ;
656653 if (key .equals (PdfName .BaseFont ) || key .equals (PdfName .FontName )) {
0 commit comments