Skip to content

Commit dce1926

Browse files
Add new overload for CompareTool#compareByContent which generates diff preffix based off output file name
ITXT-CR-749 Autoported commit. Original commit hash: [d1fb502b6c]
1 parent 4ae3023 commit dce1926

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

itext.tests/itext.kernel.tests/itext/kernel/utils/CompareToolTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public virtual void CompareToolErrorReportTest01() {
6767
compareTool.SetGenerateCompareByContentXmlReport(true);
6868
String outPdf = sourceFolder + "simple_pdf.pdf";
6969
String cmpPdf = sourceFolder + "cmp_simple_pdf.pdf";
70-
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "difference");
70+
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder);
7171
System.Console.Out.WriteLine(result);
7272
NUnit.Framework.Assert.IsNotNull(result, "CompareTool must return differences found between the files");
7373
// Comparing the report to the reference one.
@@ -86,7 +86,7 @@ public virtual void CompareToolErrorReportTest02() {
8686
compareTool.SetGenerateCompareByContentXmlReport(true);
8787
String outPdf = sourceFolder + "tagged_pdf.pdf";
8888
String cmpPdf = sourceFolder + "cmp_tagged_pdf.pdf";
89-
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "difference");
89+
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder);
9090
System.Console.Out.WriteLine(result);
9191
NUnit.Framework.Assert.IsNotNull(result, "CompareTool must return differences found between the files");
9292
// Comparing the report to the reference one.
@@ -105,7 +105,7 @@ public virtual void CompareToolErrorReportTest03() {
105105
compareTool.SetGenerateCompareByContentXmlReport(true);
106106
String outPdf = sourceFolder + "screenAnnotation.pdf";
107107
String cmpPdf = sourceFolder + "cmp_screenAnnotation.pdf";
108-
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "difference");
108+
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder);
109109
System.Console.Out.WriteLine(result);
110110
NUnit.Framework.Assert.IsNotNull(result, "CompareTool must return differences found between the files");
111111
// Comparing the report to the reference one.
@@ -125,7 +125,7 @@ public virtual void CompareToolErrorReportTest04() {
125125
compareTool.SetGenerateCompareByContentXmlReport(true);
126126
String outPdf = sourceFolder + "simple_pdf.pdf";
127127
String cmpPdf = sourceFolder + "cmp_simple_pdf_with_space .pdf";
128-
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "difference");
128+
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder);
129129
System.Console.Out.WriteLine(result);
130130
NUnit.Framework.Assert.IsNotNull(result, "CompareTool must return differences found between the files");
131131
// Comparing the report to the reference one.
@@ -141,7 +141,7 @@ public virtual void ImgFilterDiffTest() {
141141
compareTool.SetGenerateCompareByContentXmlReport(true);
142142
String outPdf = sourceFolder + "imgFilterDiff.pdf";
143143
String cmpPdf = sourceFolder + "cmp_imgFilterDiff.pdf";
144-
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "difference");
144+
String result = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder);
145145
// test that compareByContent doesn't fail with error
146146
System.Console.Out.WriteLine(result);
147147
NUnit.Framework.Assert.IsNotNull(result);

itext/itext.kernel/itext/kernel/utils/CompareTool.cs

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public virtual CompareTool.CompareResult CompareByCatalog(PdfDocument outDocumen
217217
/// By default, pages are treated as special objects and if they are met in the process of comparison, then they are
218218
/// not checked as objects, but rather simply checked that they has same page numbers in both documents.
219219
/// This behaviour is intended for the
220-
/// <see cref="CompareByContent(System.String, System.String, System.Collections.Generic.IDictionary{K, V})"/>
220+
/// <see cref="CompareByContent(System.String, System.String, System.String)"/>
221221
/// set of methods, because in them documents are compared in page by page basis. Thus, we don't need to check if pages
222222
/// are of the same content when they are met in comparison process, we are sure that we will compare their content or
223223
/// we have already compared them.
@@ -396,6 +396,52 @@ public virtual String CompareVisually(String outPdf, String cmpPdf, String outPa
396396
return CompareVisually(outPath, differenceImagePrefix, ignoredAreas);
397397
}
398398

399+
/// <summary>
400+
/// Compares two PDF documents by content starting from page dictionaries and then recursively comparing
401+
/// corresponding objects which are referenced from them.
402+
/// </summary>
403+
/// <remarks>
404+
/// Compares two PDF documents by content starting from page dictionaries and then recursively comparing
405+
/// corresponding objects which are referenced from them. You can roughly imagine it as depth-first traversal
406+
/// of the two trees that represent pdf objects structure of the documents.
407+
/// <p>
408+
/// Unlike
409+
/// <see cref="CompareByCatalog(iText.Kernel.Pdf.PdfDocument, iText.Kernel.Pdf.PdfDocument)"/>
410+
/// this method performs content comparison page by page
411+
/// and doesn't compare the tag structure, acroforms and all other things that doesn't belong to specific pages.
412+
/// <br />
413+
/// When comparison by content is finished, if any differences were found, visual comparison is automatically started.
414+
/// For more info see
415+
/// <see cref="CompareVisually(System.String, System.String, System.String, System.String)"/>
416+
/// .
417+
/// For this overload, differenceImagePrefix value is generated using diff_%outPdfFileName%_ format.
418+
/// <p>
419+
/// For more explanations about what is outPdf and cmpPdf see last paragraph of the
420+
/// <see cref="CompareTool"/>
421+
/// class description.
422+
/// </remarks>
423+
/// <param name="outPdf">the absolute path to the output file, which is to be compared to cmp-file.</param>
424+
/// <param name="cmpPdf">the absolute path to the cmp-file, which is to be compared to output file.</param>
425+
/// <param name="outPath">the absolute path to the folder, which will be used to store image files for visual comparison.
426+
/// </param>
427+
/// <returns>
428+
/// string containing text report of the encountered content differences and also list of the pages that are
429+
/// visually different, or null if there are no content and therefore no visual differences.
430+
/// </returns>
431+
/// <exception cref="System.Exception">
432+
/// if the current thread is interrupted by another thread while it is waiting
433+
/// for ghostscript or imagemagic processes, then the wait is ended and an
434+
/// <see cref="System.Exception"/>
435+
/// is thrown.
436+
/// </exception>
437+
/// <exception cref="System.IO.IOException">
438+
/// is thrown if any of the input files are missing or any of the auxiliary files
439+
/// that are created during comparison process wasn't possible to be created.
440+
/// </exception>
441+
public virtual String CompareByContent(String outPdf, String cmpPdf, String outPath) {
442+
return CompareByContent(outPdf, cmpPdf, outPath, null, null, null, null);
443+
}
444+
399445
/// <summary>
400446
/// Compares two PDF documents by content starting from page dictionaries and then recursively comparing
401447
/// corresponding objects which are referenced from them.
@@ -429,8 +475,16 @@ public virtual String CompareVisually(String outPdf, String cmpPdf, String outPa
429475
/// string containing text report of the encountered content differences and also list of the pages that are
430476
/// visually different, or null if there are no content and therefore no visual differences.
431477
/// </returns>
432-
/// <exception cref="System.Exception"/>
433-
/// <exception cref="System.IO.IOException"/>
478+
/// <exception cref="System.Exception">
479+
/// if the current thread is interrupted by another thread while it is waiting
480+
/// for ghostscript or imagemagic processes, then the wait is ended and an
481+
/// <see cref="System.Exception"/>
482+
/// is thrown.
483+
/// </exception>
484+
/// <exception cref="System.IO.IOException">
485+
/// is thrown if any of the input files are missing or any of the auxiliary files
486+
/// that are created during comparison process wasn't possible to be created.
487+
/// </exception>
434488
public virtual String CompareByContent(String outPdf, String cmpPdf, String outPath, String differenceImagePrefix
435489
) {
436490
return CompareByContent(outPdf, cmpPdf, outPath, differenceImagePrefix, null, null, null);
@@ -560,8 +614,16 @@ public virtual String CompareByContent(String outPdf, String cmpPdf, String outP
560614
/// string containing text report of the encountered content differences and also list of the pages that are
561615
/// visually different, or null if there are no content and therefore no visual differences.
562616
/// </returns>
563-
/// <exception cref="System.Exception"/>
564-
/// <exception cref="System.IO.IOException"/>
617+
/// <exception cref="System.Exception">
618+
/// if the current thread is interrupted by another thread while it is waiting
619+
/// for ghostscript or imagemagic processes, then the wait is ended and an
620+
/// <see cref="System.Exception"/>
621+
/// is thrown.
622+
/// </exception>
623+
/// <exception cref="System.IO.IOException">
624+
/// is thrown if any of the input files are missing or any of the auxiliary files
625+
/// that are created during comparison process wasn't possible to be created.
626+
/// </exception>
565627
public virtual String CompareByContent(String outPdf, String cmpPdf, String outPath, String differenceImagePrefix
566628
, IDictionary<int, IList<Rectangle>> ignoredAreas, byte[] outPass, byte[] cmpPass) {
567629
Init(outPdf, cmpPdf);
@@ -937,6 +999,14 @@ private String CompareVisually(String outPath, String differenceImagePrefix, IDi
937999
if (!outPath.EndsWith("/")) {
9381000
outPath = outPath + "/";
9391001
}
1002+
if (differenceImagePrefix == null) {
1003+
String fileBasedPrefix = "";
1004+
if (outPdfName != null) {
1005+
// should always be initialized by this moment
1006+
fileBasedPrefix = outPdfName + "_";
1007+
}
1008+
differenceImagePrefix = "diff_" + fileBasedPrefix;
1009+
}
9401010
PrepareOutputDirs(outPath, differenceImagePrefix);
9411011
System.Console.Out.WriteLine("Comparing visually..........");
9421012
if (ignoredAreas != null && !ignoredAreas.IsEmpty()) {
@@ -1209,7 +1279,6 @@ private void LoadPagesFromReader(PdfDocument doc, IList<PdfDictionary> pages, IL
12091279
}
12101280
}
12111281

1212-
/// <exception cref="System.IO.IOException"/>
12131282
private void CompareDocumentsEncryption(PdfDocument outDocument, PdfDocument cmpDocument, CompareTool.CompareResult
12141283
compareResult) {
12151284
PdfDictionary outEncrypt = outDocument.GetTrailer().GetAsDictionary(PdfName.Encrypt);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a69a8131faca8884a31b7ad55730775e7b682d36
1+
d1fb502b6ce201adef926ae458591c2a98191f09

0 commit comments

Comments
 (0)