# ComparePdfDocuments

> Verifies if two documents are equivalent, using multiple comparison options, including the following comparison types:

Verifies if two documents are equivalent, using multiple comparison options, including the following comparison types:

* Comparing words.
* Comparing lines.
* Comparing characters as `char` variables.
  :::note
  The supported document type is `PDF`.
  :::

## Definition

**Namespace:** UiPath.Testing.API

**Assembly:** UiPath.Testing.Activities.Api (in UiPath.Testing.Activities.Api.dll)

## `ComparePdfDocuments(string, string, ComparisonType, CompareDocumentsOptions)`

```
ComparisonResult ComparePdfDocuments(
    string baselinePath,
    string targetPath,
    [ComparisonType comparisonType],
    [CompareDocumentsOptions opts])
```

`baselinePath` [String](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=net-8.0) : The path of the base document that is used as a reference for comparison.

`targetPath` [String](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=net-8.0) : The path of the document that is compared against the base document.

`comparisonType` `ComparisonType` (Optional) : The type of comparison to use when comparing documents. The following options are available:

* `ComparisonType.Char`: Compares every character (`char`) in the documents.
* `ComparisonType.Line`: Compares every line in the documents.
* `ComparisonType.Word`: Compares every word in the documents.

`opts` [CompareDocumentsOptions](https://docs.uipath.com/activities/other/latest/workflow/uipath-testing-activities-api-models-testingoptions-comparedocuments#testingoptionscomparedocuments-class) : The specific comparison options that should be used for this operation. To create an object of type `CompareDocumentsOptions`, use the[TestingOptions.CompareDocuments Class](https://docs.uipath.com/activities/other/latest/workflow/uipath-testing-activities-api-models-testingoptions-comparedocuments#testingoptionscomparedocuments-class).

## Return value

`ComparisonResult`

The result of the comparison action, stored within a `ComparisonResult` variable.

## Examples

**1. Comparing PDFs by characters**:

In this example, we are comparing two PDF files, a light bill and a water bill. The comparison is done at the character level. The `firstBill` and `secondBill` variables represent the paths of these PDF files. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule `2022` and the regex rule `\d{1,2}/\d{1,2}/\d{4}` ignore dates, assuming they are in the MM/DD/YYYY format in the comparison, since they might differ in two separate bills and aren't relevant to the structure of the bills.

Once the comparison is done, the `WithGenerateHtml` method creates an HTML report of the comparison, and any differences found are recorded in the logs using the method `Log`.

   ```
   var firstBill = @"C:\bills\2024.10\pdfs for compare\electricity_bill.pdf";
   var secondBill = @"C:\bills\2024.10\pdfs for compare\water_bill.pdf";

   var identicalCharCompareResult = testing.VerifyDocumentsEquivalence(
       firstBill, secondBill, 
       ComparisonType.Character, 
       TestingOptions.CompareDocuments()
           .WithGenerateHtml(@".\HtmlCompareResults\PDFCharCompare.html")
           .WithIgnoreWildcardRule("WildcardRule", "*2022*", true)
           .WithIgnoreRegexRule("RegexRule", @"\d{1,2}/\d{1,2}/\d{4}", true)
   );

   Log("pdf compare by char " + identicalCharCompareResult.AreEquivalent.ToString(),LogLevel.Error);
   ```
**2. Comparing PDFs by word**:

In this example, we are comparing the same PDF files as in the previous example: a light bill and a water bill. The comparison is done at the word level. The `firstBill` and `secondBill` variables represent the paths of these PDF files. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison. For instance, the wildcard rule `2022` ignore dates, assuming they are in the MM/DD/YYYY format in the comparison, since they might differ in two separate bills and aren't relevant to the structure of the bills. The regex rule `[$]\d+\.\d{2}` ignores currency values, assuming they are in a format similar to `$XX.XX`.

Once the comparison is done, the `WithGenerateHtml` method creates an HTML report of the comparison, and any differences found are recorded in the logs using the method `Log`.

   ```
   var firstBill = @"C:\bills\2024.10\pdfs for compare\electricity_bill.pdf";
   var secondBill = @"C:\bills\2024.10\pdfs for compare\water_bill.pdf";

   var differentWordCompareResult = testing.VerifyDocumentsEquivalence(
       firstBill, secondBill, 
       ComparisonType.Word, 
       TestingOptions.CompareDocuments()
           .WithGenerateHtml(@".\HtmlCompareResults\PDFWordCompareDiff.html")
           .WithIgnoreWildcardRule("WildcardRule", "*2022*", true)
           .WithIgnoreRegexRule("RegexRule", @"[$]\d+\.\d{2}", true)
   );

   Log("pdf compare by Word " + differentWordCompareResult.AreEquivalent.ToString(), LogLevel.Error);
   ```
**3. Comparing PDFs by line**:

In this example, similar to the previous one, we are comparing two PDF files, a light bill and a water bill. In this scenario, the comparison is done at the line level. The `firstBill` and `secondBill` variables represent the paths of these PDF files. The wildcard rule and regex rule provided along with `WithIgnoreWildcardRule` and `WithIgnoreRegexRule` methods are used to ignore specific patterns in the comparison.

For instance, the wildcard rule `2022` ignore dates (assuming they are in the MM/DD/YYYY format) in the comparison, since they might differ in two separate bills and aren't relevant to the structure of the bills. The regex rule `[$]\d+\.\d{2}` ignores currency values, assuming they are in a format similar to `$XX.XX`.

Once the comparison is done, the `WithGenerateHtml` method creates an HTML report of the comparison, and any differences found are recorded in the logs using the method `Log`.

   ```
   var firstBill = @"C:\bills\2024.10\pdfs for compare\electricity_bill.pdf";
   var secondBill = @"C:\bills\2024.10\pdfs for compare\water_bill.pdf";

   var largeLineCompareResult = testing.VerifyDocumentsEquivalence(
       firstBill, secondBill, 
       ComparisonType.Line, 
       TestingOptions.CompareDocuments()
           .WithGeneratePdf(@".\PDFCompareResults",@".\PDFCompareResults")
           .WithIgnoreWildcardRule("WildcardRule", "*2022*", true)
           .WithIgnoreRegexRule("RegexRule", @"[$]\d+\.\d{2}", true)
   );

   Log("Pdf compare by line " + largeLineCompareResult.AreEquivalent.ToString(), LogLevel.Error);
   ```

In all examples, the `AreEquivalent` property returns whether the documents are equal or not.
