DocumentCompare.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using ComPDFKit.Compare;
  2. using ComPDFKit.PDFDocument;
  3. using System;
  4. using System.IO;
  5. namespace DocumentCompareTest
  6. {
  7. internal class DocumentCompare
  8. {
  9. private static string outputPath =Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) ?? string.Empty, "Output", "CS");
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Running PDFPage test sample…\r\n");
  13. SDKLicenseHelper.LicenseVerify();
  14. if (!Directory.Exists(outputPath))
  15. {
  16. Directory.CreateDirectory(outputPath);
  17. }
  18. CPDFDocument document1 = CPDFDocument.InitWithFilePath("ElectricalDiagram.pdf");
  19. CPDFDocument document2 = CPDFDocument.InitWithFilePath("ElectricalDiagram_New.pdf");
  20. if (OverlayCompareDocument(document1, document2))
  21. {
  22. Console.WriteLine("Compare document done.");
  23. }
  24. else
  25. {
  26. Console.WriteLine("Compare document failed.");
  27. }
  28. document1.Release();
  29. document2.Release();
  30. Console.WriteLine("--------------------");
  31. CPDFDocument document3 = CPDFDocument.InitWithFilePath("Text.pdf");
  32. CPDFDocument document4 = CPDFDocument.InitWithFilePath("TextChanged.pdf");
  33. if (ContentCompareDocument(document3, document4))
  34. {
  35. Console.WriteLine("Compare document done.");
  36. }
  37. else
  38. {
  39. Console.WriteLine("Compare document failed.");
  40. }
  41. document3.Release();
  42. document4.Release();
  43. Console.WriteLine("--------------------");
  44. Console.WriteLine("Done!");
  45. Console.WriteLine("--------------------");
  46. Console.ReadLine();
  47. }
  48. static private bool OverlayCompareDocument(CPDFDocument document1, CPDFDocument document2)
  49. {
  50. CPDFCompareOverlay compareOverlay = new CPDFCompareOverlay(document1, "1-5", document2, "1-5");
  51. compareOverlay.Compare();
  52. CPDFDocument comparisonDocument = compareOverlay.ComparisonDocument();
  53. string path = Path.Combine(outputPath, "CompareDocumentTest.pdf");
  54. if (!comparisonDocument.WriteToFilePath(path))
  55. {
  56. return false;
  57. }
  58. Console.WriteLine("Browse the changed file in " + path);
  59. return true;
  60. }
  61. static private bool ContentCompareDocument(CPDFDocument document, CPDFDocument documentNew)
  62. {
  63. CPDFCompareContent compareContent = new CPDFCompareContent(document, documentNew);
  64. int pageCount = Math.Min(document.PageCount, documentNew.PageCount);
  65. for (int i = 0; i < pageCount; i++)
  66. {
  67. Console.WriteLine("Page: {0}", i);
  68. CPDFCompareResults compareResults = compareContent.Compare(i, i, CPDFCompareType.CPDFCompareTypeAll, true);
  69. Console.WriteLine("Replace count: {0}", compareResults.ReplaceCount);
  70. Console.WriteLine("TextResults count: {0}", compareResults.TextResults.Count);
  71. Console.WriteLine("Delete count: {0}", compareResults.DeleteCount);
  72. Console.WriteLine("Insert count: {0}", compareResults.InsertCount);
  73. }
  74. return true;
  75. }
  76. }
  77. }