Skip to content

Commit d1fb502

Browse files
Add new overload for CompareTool#compareByContent which generates diff preffix based off output file name
ITXT-CR-749
1 parent a69a813 commit d1fb502

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,34 @@ public String compareVisually(String outPdf, String cmpPdf, String outPath, Stri
362362
return compareVisually(outPath, differenceImagePrefix, ignoredAreas);
363363
}
364364

365+
/**
366+
* Compares two PDF documents by content starting from page dictionaries and then recursively comparing
367+
* corresponding objects which are referenced from them. You can roughly imagine it as depth-first traversal
368+
* of the two trees that represent pdf objects structure of the documents.
369+
* <p>
370+
* Unlike {@link #compareByCatalog(PdfDocument, PdfDocument)} this method performs content comparison page by page
371+
* and doesn't compare the tag structure, acroforms and all other things that doesn't belong to specific pages.
372+
* <br>
373+
* When comparison by content is finished, if any differences were found, visual comparison is automatically started.
374+
* For more info see {@link #compareVisually(String, String, String, String)}.
375+
* For this overload, differenceImagePrefix value is generated using diff_%outPdfFileName%_ format.
376+
* <p>
377+
* For more explanations about what is outPdf and cmpPdf see last paragraph of the {@link CompareTool}
378+
* class description.
379+
* @param outPdf the absolute path to the output file, which is to be compared to cmp-file.
380+
* @param cmpPdf the absolute path to the cmp-file, which is to be compared to output file.
381+
* @param outPath the absolute path to the folder, which will be used to store image files for visual comparison.
382+
* @return string containing text report of the encountered content differences and also list of the pages that are
383+
* visually different, or null if there are no content and therefore no visual differences.
384+
* @throws InterruptedException if the current thread is interrupted by another thread while it is waiting
385+
* for ghostscript or imagemagic processes, then the wait is ended and an {@link InterruptedException} is thrown.
386+
* @throws IOException is thrown if any of the input files are missing or any of the auxiliary files
387+
* that are created during comparison process wasn't possible to be created.
388+
*/
389+
public String compareByContent(String outPdf, String cmpPdf, String outPath) throws InterruptedException, IOException {
390+
return compareByContent(outPdf, cmpPdf, outPath, null, null, null, null);
391+
}
392+
365393
/**
366394
* Compares two PDF documents by content starting from page dictionaries and then recursively comparing
367395
* corresponding objects which are referenced from them. You can roughly imagine it as depth-first traversal
@@ -381,8 +409,10 @@ public String compareVisually(String outPdf, String cmpPdf, String outPath, Stri
381409
* @param differenceImagePrefix file name prefix for image files with marked visual differences if there is any.
382410
* @return string containing text report of the encountered content differences and also list of the pages that are
383411
* visually different, or null if there are no content and therefore no visual differences.
384-
* @throws InterruptedException
385-
* @throws IOException
412+
* @throws InterruptedException if the current thread is interrupted by another thread while it is waiting
413+
* for ghostscript or imagemagic processes, then the wait is ended and an {@link InterruptedException} is thrown.
414+
* @throws IOException is thrown if any of the input files are missing or any of the auxiliary files
415+
* that are created during comparison process wasn't possible to be created.
386416
*/
387417
public String compareByContent(String outPdf, String cmpPdf, String outPath, String differenceImagePrefix) throws InterruptedException, IOException {
388418
return compareByContent(outPdf, cmpPdf, outPath, differenceImagePrefix, null, null, null);
@@ -472,8 +502,10 @@ public String compareByContent(String outPdf, String cmpPdf, String outPath, Str
472502
* @param cmpPass password for the encrypted document specified by the cmpPdf absolute path.
473503
* @return string containing text report of the encountered content differences and also list of the pages that are
474504
* visually different, or null if there are no content and therefore no visual differences.
475-
* @throws InterruptedException
476-
* @throws IOException
505+
* @throws InterruptedException if the current thread is interrupted by another thread while it is waiting
506+
* for ghostscript or imagemagic processes, then the wait is ended and an {@link InterruptedException} is thrown.
507+
* @throws IOException is thrown if any of the input files are missing or any of the auxiliary files
508+
* that are created during comparison process wasn't possible to be created.
477509
*/
478510
public String compareByContent(String outPdf, String cmpPdf, String outPath, String differenceImagePrefix, Map<Integer, List<Rectangle>> ignoredAreas, byte[] outPass, byte[] cmpPass) throws InterruptedException, IOException {
479511
init(outPdf, cmpPdf);
@@ -830,8 +862,17 @@ private String compareVisually(String outPath, String differenceImagePrefix, Map
830862
if (!(new File(gsExec).canExecute())) {
831863
throw new CompareToolExecutionException(new File(gsExec).getAbsolutePath() + " is not an executable program");
832864
}
833-
if (!outPath.endsWith("/"))
865+
if (!outPath.endsWith("/")) {
834866
outPath = outPath + "/";
867+
}
868+
if (differenceImagePrefix == null) {
869+
String fileBasedPrefix = "";
870+
if (outPdfName != null) { // should always be initialized by this moment
871+
fileBasedPrefix = outPdfName + "_";
872+
}
873+
differenceImagePrefix = "diff_" + fileBasedPrefix;
874+
}
875+
835876
prepareOutputDirs(outPath, differenceImagePrefix);
836877

837878
System.out.println("Comparing visually..........");
@@ -1092,7 +1133,7 @@ private void loadPagesFromReader(PdfDocument doc, List<PdfDictionary> pages, Lis
10921133
}
10931134
}
10941135

1095-
private void compareDocumentsEncryption(PdfDocument outDocument, PdfDocument cmpDocument, CompareResult compareResult) throws IOException {
1136+
private void compareDocumentsEncryption(PdfDocument outDocument, PdfDocument cmpDocument, CompareResult compareResult) {
10961137
PdfDictionary outEncrypt = outDocument.getTrailer().getAsDictionary(PdfName.Encrypt);
10971138
PdfDictionary cmpEncrypt = cmpDocument.getTrailer().getAsDictionary(PdfName.Encrypt);
10981139

kernel/src/test/java/com/itextpdf/kernel/utils/CompareToolTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void compareToolErrorReportTest01() throws InterruptedException, IOExcept
7171
compareTool.setGenerateCompareByContentXmlReport(true);
7272
String outPdf = sourceFolder + "simple_pdf.pdf";
7373
String cmpPdf = sourceFolder + "cmp_simple_pdf.pdf";
74-
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "difference");
74+
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder);
7575
System.out.println(result);
7676
Assert.assertNotNull("CompareTool must return differences found between the files", result);
7777
// Comparing the report to the reference one.
@@ -85,7 +85,7 @@ public void compareToolErrorReportTest02() throws IOException, InterruptedExcept
8585
compareTool.setGenerateCompareByContentXmlReport(true);
8686
String outPdf = sourceFolder + "tagged_pdf.pdf";
8787
String cmpPdf = sourceFolder + "cmp_tagged_pdf.pdf";
88-
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "difference");
88+
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder);
8989
System.out.println(result);
9090
Assert.assertNotNull("CompareTool must return differences found between the files", result);
9191
// Comparing the report to the reference one.
@@ -99,7 +99,7 @@ public void compareToolErrorReportTest03() throws InterruptedException, IOExcept
9999
compareTool.setGenerateCompareByContentXmlReport(true);
100100
String outPdf = sourceFolder + "screenAnnotation.pdf";
101101
String cmpPdf = sourceFolder + "cmp_screenAnnotation.pdf";
102-
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "difference");
102+
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder);
103103
System.out.println(result);
104104
Assert.assertNotNull("CompareTool must return differences found between the files", result);
105105
// Comparing the report to the reference one.
@@ -115,7 +115,7 @@ public void compareToolErrorReportTest04() throws InterruptedException, IOExcept
115115
compareTool.setGenerateCompareByContentXmlReport(true);
116116
String outPdf = sourceFolder + "simple_pdf.pdf";
117117
String cmpPdf = sourceFolder + "cmp_simple_pdf_with_space .pdf";
118-
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "difference");
118+
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder);
119119
System.out.println(result);
120120
Assert.assertNotNull("CompareTool must return differences found between the files", result);
121121
// Comparing the report to the reference one.
@@ -130,7 +130,7 @@ public void imgFilterDiffTest() throws InterruptedException, IOException {
130130
compareTool.setGenerateCompareByContentXmlReport(true);
131131
String outPdf = sourceFolder + "imgFilterDiff.pdf";
132132
String cmpPdf = sourceFolder + "cmp_imgFilterDiff.pdf";
133-
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "difference");
133+
String result = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder);
134134
// test that compareByContent doesn't fail with error
135135
System.out.println(result);
136136
Assert.assertNotNull(result);

0 commit comments

Comments
 (0)