Differentiating between two similar files is a common task for app developers, especially when working with images or text. In many cases, developers need to be able to determine if two files are identical, or if there are any differences between them. This process is known as file comparison, and there are several different ways to do it. Comparisons between documents or images could be done manually, or, you can develop specific applications to automate the comparison process.
Manual document comparison could be laborious, time-consuming, and may require dedicated resources for process accomplishment. Developing a file comparison application can help save time, and make the whole process easier to use. GroupDocs.Comparison for .NET and Java file comparison libraries allow text comparison, image comparison, document comparison, and difference checking among a host of commonly used document and image file formats such as PDF, DOC, DOCX, XLS, XLSX, PPT, PPTX, VSD, ONE, ODT, TXT, DWG, PNG, JPEG, EML, MSG, HTML, in addition to programming language formats like CS, PHP, JS, CPP, JSON, SQL and many more.
Before comparing your documents or images, please check the .NET and Java installation guides to ensure having installed the required components on your system.
We'll now take a look at some of the most common use cases of file comparison, as well as how they can be used to compare documents and images in .NET and Java.
Comparing documents can be tricky especially when you're dealing with different file formats. As a developer, you may need to compare and check differences for the compared data file formats such as PDF, Word, Excel, and PowerPoint documents. There are a few different ways to do this, including building a .NET or Java document comparison application or injecting the code for comparing multi-format files in your web service.
GroupDocs.Comparison APIs for .NET and Java platforms provide you with easy methods for comparing PDF files together with Microsoft Word documents, Excel spreadsheets, and PowerPoint presentations in different frameworks and environments of .NET and Java respectively.
string sourcePath = @"source.pdf"; // Path to the source pdf document string targetPath = @"target.pdf"; // Path to the target pdf document string resultPath = @"result.pdf"; // Path to the result pdf document using (Comparer comparer = new Comparer(sourcePath)) { comparer.Add(targetPath); CompareOptions compareOptions = new CompareOptions { DetectStyleChanges = true, DetalisationLevel = DetalisationLevel.High }; comparer.Compare(resultPath, compareOptions); }
Image comparison is a frequently used functionality while comparing files of different formats. If you are working with a lot of images, it could become tedious to compare them and examine the differences, which is one of the regular features of the image file comparison process.
Therefore, developing an application accurately comparing PNG, JPG, BMP, or GIF images or integrating this functionality into your existing image processing software could be a great and robust addition. GroupDocs.Comparison for .NET and Java file comparison libraries provide all the necessary tools to compare different raster images.
// Compare JPG, PNG, GIF, and BMP image formats using .NET Image Comparison API in C# using (Comparer comparer = new Comparer("filepath/soureImage.jpg")) { CompareOptions options = new CompareOptions(); options.GenerateSummaryPage = false; // To get the difference summary, set it 'true' comparer.Add("filepath/targetImage.jpg"); comparer.Compare("filepath/comparisonResultImage.jpg", options); }
Consider working on a development project distributed among many teams. You could regularly come across the same source code document to which, multiple application developers are contributing by injecting code of various modular segments. To avoid any versioning conflicts, you may have to identify the differences between two such files, and upon completion of the comparison, you can use a programmatic approach to merge them, thus creating a unified source code file ready to be shared across a distributed team.
This and any other scenarios involving the comparison of your source code documents are covered by GroupDocs.Comparison APIs (both .NET and Java) which let you compare code of the source files of your .NET or Java software, use a diff checker, and then merge the files resulting in HTML format output.
using (Comparer comparer = new Comparer(sourcePath)) { comparer.Add(targetPath); comparer.Compare(resultPath); ChangeInfo[] changes = comparer.GetChanges(); for (int i = 0; i < 10; i++) { changes[i].ComparisonAction = ComparisonAction.Accept; } for (int i = 10; i < changes.Length; i++) { changes[i].ComparisonAction = ComparisonAction.Reject; } comparer.ApplyChanges(File.Create(resultPath), new ApplyChangeOptions { Changes = changes }); }
try (Comparer comparer = new Comparer(sourcePath)) { comparer.add(targetPath); final Path resultPath = comparer.compare(outputPath); ChangeInfo[] changes = comparer.getChanges(); for (int i = 0; i < 10; i++) { changes[i].setComparisonAction(ComparisonAction.ACCEPT); } for (int i = 10; i < changes.length; i++) { changes[i].setComparisonAction(ComparisonAction.REJECT); } comparer.applyChanges(resultPath, new ApplyChangeOptions(changes)); }
In the event of someone mistakenly reverting the changes you have made to a document, the importance of controlling this aspect of document processing grows even greater. File comparison is a great way of avoiding such problems. By comparing two versions of a document, you can see exactly what has been changed, and then decide whether to accept or reject those changes. GroupDocs.Comparison file comparison libraries give you total control over whether or not to accept the changes after having compared two documents (or images) and checked the differences in .NET or Java platforms.
using (Comparer comparer = new Comparer("source.docx")) { comparer.Add("target.docx"); comparer.Compare(); ChangeInfo[] changes = comparer.GetChanges(); changes[0].ComparisonAction = ComparisonAction.Reject; comparer.ApplyChanges("resultWithRejectedChange.docx", new ApplyChangeOptions() { Changes = changes, SaveOriginalState = true }); changes = comparer.GetChanges(); changes[0].ComparisonAction = ComparisonAction.Accept; comparer.ApplyChanges("resultWithAcceptedChange.docx", new ApplyChangeOptions() { Changes = changes }); }
try (Comparer comparer = new Comparer(sourceFile)) { comparer.add(targetFile); final Path resultPath = comparer.compare(); ChangeInfo[] changes = comparer.getChanges(); changes[0].setComparisonAction(ComparisonAction.REJECT); ApplyChangeOptions changeOptions = new ApplyChangeOptions(); changeOptions.setChanges(changes); changeOptions.setSaveOriginalState(true); comparer.applyChanges(resultFileWithRejectedChange, changeOptions); changes = comparer.getChanges(); changes[0].setComparisonAction(ComparisonAction.ACCEPT); comparer.applyChanges(resultFileWithAcceptedChange, new ApplyChangeOptions(changes)); }
We hope you learned a few different aspects of document comparison, image file comparison, text comparison, and difference checking using GroupDocs.Comparison APIs for .NET and Java. If you are looking to compare your PDF, DOC/DOCX, XLS/XLSX, XLSM, PPT/PPTX, PNG, JPG/JPEG, BMP, GIF, TIFF, CAD, RTF, TXT, ODS, and HTML files on the fly using your mobile devices, please check out GroupDocs.Comparison free online apps and perform document comparisons instantly.