Skip to content

Commit 42a5522

Browse files
Fix CompareTool#compareByCatalog method: add page dictionaries comparison
1 parent b1ec385 commit 42a5522

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

kernel/src/main/java/com/itextpdf/kernel/utils/CompareTool.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ public CompareResult compareByCatalog(PdfDocument outDocument, PdfDocument cmpDo
187187
Set<PdfName> ignoredCatalogEntries = new LinkedHashSet<>(Arrays.asList(PdfName.Metadata));
188188
compareDictionariesExtended(outDocument.getCatalog().getPdfObject(), cmpDocument.getCatalog().getPdfObject(),
189189
catalogPath, compareResult, ignoredCatalogEntries);
190+
191+
// Method compareDictionariesExtended eventually calls compareObjects method which doesn't compare page objects.
192+
// At least for now compare page dictionaries explicitly here like this.
193+
if (outPagesRef.size() != cmpPagesRef.size()) {
194+
compareResult.addError(catalogPath, "Documents have different numbers of pages.");
195+
}
196+
for (int i = 0; i < Math.min(cmpPagesRef.size(), outPagesRef.size()); i++) {
197+
ObjectPath currentPath = new ObjectPath(cmpPagesRef.get(i), outPagesRef.get(i));
198+
PdfDictionary outPageDict = (PdfDictionary) outPagesRef.get(i).getRefersTo();
199+
PdfDictionary cmpPageDict = (PdfDictionary) cmpPagesRef.get(i).getRefersTo();
200+
compareDictionariesExtended(outPageDict, cmpPageDict, currentPath, compareResult);
201+
}
190202
return compareResult;
191203
}
192204

kernel/src/test/java/com/itextpdf/kernel/pdf/ParentTreeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ private boolean checkParentTree(String outFileName, String cmpFileName) throws I
296296
PdfReader cmpReader = new PdfReader(cmpFileName);
297297
PdfDocument cmpDocument = new PdfDocument(cmpReader);
298298
CompareResult result = new CompareTool().compareByCatalog(outDocument, cmpDocument);
299+
if (!result.isOk()) {
300+
System.out.println(result.getReport());
301+
}
299302
return result.isOk();
300303
}
301304
}

0 commit comments

Comments
 (0)