Skip to content

Commit fe9779e

Browse files
committed
Improve CompareTool: compare whole catalogs instead of just selected predefined entries
1 parent 0961cf1 commit fe9779e

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfCatalog.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ PdfOutline getOutlines(boolean updateOutlines) {
363363
return outlines;
364364
}
365365

366+
/**
367+
* Indicates if the catalog has any outlines
368+
* @return {@code true}, if there are outlines and {@code false} otherwise.
369+
*/
370+
boolean hasOutlines() {
371+
return getPdfObject().containsKey(PdfName.Outlines);
372+
}
373+
366374
/**
367375
* This method removes all outlines associated with a given page
368376
*
@@ -373,10 +381,12 @@ void removeOutlines(PdfPage page) {
373381
if (getDocument().getWriter() == null) {
374382
return;
375383
}
376-
getOutlines(false);
377-
if (!pagesWithOutlines.isEmpty()) {
378-
for (PdfOutline outline : pagesWithOutlines.get(page.getPdfObject())) {
379-
outline.removeOutline();
384+
if (hasOutlines()) {
385+
getOutlines(false);
386+
if (!pagesWithOutlines.isEmpty()) {
387+
for (PdfOutline outline : pagesWithOutlines.get(page.getPdfObject())) {
388+
outline.removeOutline();
389+
}
380390
}
381391
}
382392
}

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,11 @@ public String[] getPageLabels() {
11611161
}
11621162

11631163
/**
1164-
* indicates if the document has any outlines
1165-
* @return
1164+
* Indicates if the document has any outlines
1165+
* @return {@code true}, if there are outlines and {@code false} otherwise.
11661166
*/
11671167
public boolean hasOutlines() {
1168-
return catalog.getPdfObject().containsKey(PdfName.Outlines);
1168+
return catalog.hasOutlines();
11691169
}
11701170

11711171
/**

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@
2626
import com.itextpdf.kernel.xmp.XMPUtils;
2727
import 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;
3829
import java.io.BufferedReader;
3930
import java.io.ByteArrayInputStream;
4031
import java.io.File;
@@ -49,12 +40,22 @@
4940
import java.util.Arrays;
5041
import java.util.Comparator;
5142
import java.util.LinkedHashMap;
43+
import java.util.LinkedHashSet;
5244
import java.util.List;
5345
import java.util.Map;
5446
import java.util.Set;
5547
import java.util.Stack;
5648
import java.util.StringTokenizer;
5749
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;
5859

5960
import org.w3c.dom.Document;
6061
import 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)) {

kernel/src/test/resources/com/itextpdf/kernel/utils/CompareToolTest/cmp_report02.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
<report>
33
<errors count="2">
44
<error>
5-
<message>PdfString. Characters differ at position 0. Expected: P (Page 1). Found: W (Wage 1).</message>
6-
<path>
7-
<base cmp="12 0 obj" out="12 0 obj"/>
8-
<dictKey>/T</dictKey>
9-
<offset>0</offset>
10-
</path>
11-
</error>
12-
<error>
135
<message>PdfString. Characters differ at position 6. Expected: r (Courier Std). Found: w (Couriew Std).</message>
146
<path>
157
<base cmp="3 0 obj" out="3 0 obj"/>
168
<dictKey>/FontFamily</dictKey>
179
<offset>6</offset>
1810
</path>
1911
</error>
12+
<error>
13+
<message>PdfString. Characters differ at position 0. Expected: P (Page 1). Found: W (Wage 1).</message>
14+
<path>
15+
<base cmp="12 0 obj" out="12 0 obj"/>
16+
<dictKey>/T</dictKey>
17+
<offset>0</offset>
18+
</path>
19+
</error>
2020
</errors>
2121
</report>

layout/src/main/java/com/itextpdf/layout/Document.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public void flush() {
127127
* Do not use when you have set {@link #immediateFlush} to <code>true</code>.
128128
*/
129129
public void relayout() {
130-
131130
if (immediateFlush) {
132131
throw new IllegalStateException("Operation not supported with immediate flush");
133132
}

0 commit comments

Comments
 (0)