26
26
import com .itextpdf .kernel .xmp .XMPUtils ;
27
27
import com .itextpdf .kernel .xmp .options .SerializeOptions ;
28
28
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 ;
38
29
import java .io .BufferedReader ;
39
30
import java .io .ByteArrayInputStream ;
40
31
import java .io .File ;
49
40
import java .util .Arrays ;
50
41
import java .util .Comparator ;
51
42
import java .util .LinkedHashMap ;
43
+ import java .util .LinkedHashSet ;
52
44
import java .util .List ;
53
45
import java .util .Map ;
54
46
import java .util .Set ;
55
47
import java .util .Stack ;
56
48
import java .util .StringTokenizer ;
57
49
import 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 ;
58
59
59
60
import org .w3c .dom .Document ;
60
61
import org .w3c .dom .Element ;
@@ -285,7 +286,6 @@ public String compareLinkAnnotations(String outPdf, String cmpPdf) throws IOExce
285
286
return message ;
286
287
}
287
288
288
-
289
289
public String compareTagStructures (String outPdf , String cmpPdf ) throws IOException , ParserConfigurationException , SAXException {
290
290
System .out .print ("[itext] INFO Comparing tag structures......" );
291
291
@@ -573,12 +573,11 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
573
573
equalPages .add (i );
574
574
}
575
575
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 );
582
581
583
582
outDocument .close ();
584
583
cmpDocument .close ();
@@ -606,14 +605,6 @@ private String compareByContent(String outPath, String differenceImagePrefix, Ma
606
605
}
607
606
}
608
607
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
-
617
608
private void loadPagesFromReader (PdfDocument doc , List <PdfDictionary > pages , List <PdfIndirectReference > pagesRef ) {
618
609
int numOfPages = doc .getCatalog ().getNumberOfPages ();
619
610
for (int i = 0 ; i < numOfPages ; ++i ) {
@@ -640,8 +631,11 @@ private boolean compareStreams(InputStream is1, InputStream is2) throws IOExcept
640
631
return true ;
641
632
}
642
633
643
-
644
634
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 {
645
639
if (cmpDict != null && outDict == null || outDict != null && cmpDict == null ) {
646
640
compareResult .addError (currentPath , "One of the dictionaries is null, the other is not." );
647
641
return false ;
@@ -651,6 +645,9 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
651
645
Set <PdfName > mergedKeys = new TreeSet <>(cmpDict .keySet ());
652
646
mergedKeys .addAll (outDict .keySet ());
653
647
for (PdfName key : mergedKeys ) {
648
+ if (excludedKeys != null && excludedKeys .contains (key )) {
649
+ continue ;
650
+ }
654
651
if (key .equals (PdfName .Parent ) || key .equals (PdfName .P ) || key .equals (PdfName .ModDate )) continue ;
655
652
if (outDict .isStream () && cmpDict .isStream () && (key .equals (PdfName .Filter ) || key .equals (PdfName .Length ))) continue ;
656
653
if (key .equals (PdfName .BaseFont ) || key .equals (PdfName .FontName )) {
0 commit comments