A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Comparing Two PDF Files in C# (Developer Guide)Skip to footer content
USING IRONPDF

How to Compare Two PDF Files in C#

Introduction

Comparing PDF documents programmatically is a crucial requirement in modern .NET Core applications, whether this is from tracking document revisions to ensuring compliance in legal workflows. Whether you're validating contract changes, monitoring different versions, or implementing quality assurance processes, automated PDF file comparison helps developers save time and reduce discrepancies.

IronPDF offers a streamlined approach to compare two PDF files using C# by combining powerful text extraction capabilities with flexible document comparison options. This tutorial demonstrates how to efficiently compare two PDF documents using IronPDF's intuitive API with practical code examples.

Getting Started: Install and Set Up Your .NET Project

First, install IronPDF via NuGet Package Manager in your .NET project:

Install-Package IronPdf

Or add the reference using the .NET CLI:

dotnet add package IronPdf
dotnet add package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'dotnet add package IronPdf
$vbLabelText   $csharpLabel

For Linux or Windows environments, refer to the documentation for platform-specific instructions. Once the .NET package is installed, configure your license (optional for development):

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

Basic Comparison: Compare Two PDFs

The foundation of comparing PDF documents is extracting and comparing text content. Here's sample code to compare two PDF files:

using IronPdf;
using System;
class PdfComparer
{
    public static void CompareSimple(string pdf1Path, string pdf2Path)
    {
        // Load two PDF documents
        var pdf1 = PdfDocument.FromFile(pdf1Path);
        var pdf2 = PdfDocument.FromFile(pdf2Path);
        // Extract text from both PDFs
        string text1 = pdf1.ExtractAllText();
        string text2 = pdf2.ExtractAllText();
        // Compare the two documents
        if (text1 == text2)
        {
            Console.WriteLine("PDF files are identical");
        }
        else
        {
            Console.WriteLine("PDFs have differences");
            // Find differences and calculate similarity
            double similarity = CalculateSimilarity(text1, text2);
            Console.WriteLine($"Comparison result: {similarity:P} similar");
        }
    }
    private static double CalculateSimilarity(string text1, string text2)
    {
        int maxLength = Math.Max(text1.Length, text2.Length);
        if (maxLength == 0) return 1.0;
        int differences = 0;
        int minLength = Math.Min(text1.Length, text2.Length);
        for (int i = 0; i < minLength; i++)
        {
            if (text1[i] != text2[i]) differences++;
        }
        differences += Math.Abs(text1.Length - text2.Length);
        return 1.0 - (double)differences / maxLength;
    }
}
using IronPdf;
using System;
class PdfComparer
{
    public static void CompareSimple(string pdf1Path, string pdf2Path)
    {
        // Load two PDF documents
        var pdf1 = PdfDocument.FromFile(pdf1Path);
        var pdf2 = PdfDocument.FromFile(pdf2Path);
        // Extract text from both PDFs
        string text1 = pdf1.ExtractAllText();
        string text2 = pdf2.ExtractAllText();
        // Compare the two documents
        if (text1 == text2)
        {
            Console.WriteLine("PDF files are identical");
        }
        else
        {
            Console.WriteLine("PDFs have differences");
            // Find differences and calculate similarity
            double similarity = CalculateSimilarity(text1, text2);
            Console.WriteLine($"Comparison result: {similarity:P} similar");
        }
    }
    private static double CalculateSimilarity(string text1, string text2)
    {
        int maxLength = Math.Max(text1.Length, text2.Length);
        if (maxLength == 0) return 1.0;
        int differences = 0;
        int minLength = Math.Min(text1.Length, text2.Length);
        for (int i = 0; i < minLength; i++)
        {
            if (text1[i] != text2[i]) differences++;
        }
        differences += Math.Abs(text1.Length - text2.Length);
        return 1.0 - (double)differences / maxLength;
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code loads two PDF files, extracts their complete text content, and performs a basic comparison. The method provides a result indicating how similar the documents are, helping quantify the differences between files.

Input

How to Compare Two PDF Files in C#: Figure 4 - Sample PDF Input 1

How to Compare Two PDF Files in C#: Figure 5 - Sample PDF Input 2

Output

How to Compare Two PDF Files in C#: Figure 6 - Console Output

Advanced: Page-by-Page PDF Comparison

For more detailed analysis, compare PDF documents page by page to identify exactly where changes occur:

public static void CompareByPage(string pdf1Path, string pdf2Path)
{
    // Using Comparer class pattern for the first PDF document
    var pdf1 = PdfDocument.FromFile(pdf1Path);
    var pdf2 = PdfDocument.FromFile(pdf2Path);
    int maxPages = Math.Max(pdf1.PageCount, pdf2.PageCount);
    for (int i = 0; i < maxPages; i++)
    {
        string page1Text = i < pdf1.PageCount ? 
            pdf1.ExtractTextFromPage(i) : "";
        string page2Text = i < pdf2.PageCount ? 
            pdf2.ExtractTextFromPage(i) : "";
        if (page1Text != page2Text)
        {
            Console.WriteLine($"Difference found on page {i + 1}");
            // Highlight differences in output
        }
    }
}
public static void CompareByPage(string pdf1Path, string pdf2Path)
{
    // Using Comparer class pattern for the first PDF document
    var pdf1 = PdfDocument.FromFile(pdf1Path);
    var pdf2 = PdfDocument.FromFile(pdf2Path);
    int maxPages = Math.Max(pdf1.PageCount, pdf2.PageCount);
    for (int i = 0; i < maxPages; i++)
    {
        string page1Text = i < pdf1.PageCount ? 
            pdf1.ExtractTextFromPage(i) : "";
        string page2Text = i < pdf2.PageCount ? 
            pdf2.ExtractTextFromPage(i) : "";
        if (page1Text != page2Text)
        {
            Console.WriteLine($"Difference found on page {i + 1}");
            // Highlight differences in output
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This compare method iterates through each page, comparing content individually. The process handles PDFs with different page counts gracefully, making it ideal when comparing multiple PDF documents where pages might have been updated.

Comparing Multiple PDF Documents

To enhance your system for comparing multiple PDF documents, extend the Comparer class:

public class MultiPdfComparer
{
    public static void CompareMultiple(params string[] pdfPaths)
    {
        if (pdfPaths.Length < 2) return;
        // Load first PDF document as reference
        var referencePdf = PdfDocument.FromFile(pdfPaths[0]);
        string referenceText = referencePdf.ExtractAllText();
        // Compare with other PDF files
        for (int i = 1; i < pdfPaths.Length; i++)
        {
            var currentPdf = PdfDocument.FromFile(pdfPaths[i]);
            string currentText = currentPdf.ExtractAllText();
            if (referenceText != currentText)
            {
                Console.WriteLine($"PDF {i} differs from reference");
            }
        }
        // Results saved for further processing
    }
}
public class MultiPdfComparer
{
    public static void CompareMultiple(params string[] pdfPaths)
    {
        if (pdfPaths.Length < 2) return;
        // Load first PDF document as reference
        var referencePdf = PdfDocument.FromFile(pdfPaths[0]);
        string referenceText = referencePdf.ExtractAllText();
        // Compare with other PDF files
        for (int i = 1; i < pdfPaths.Length; i++)
        {
            var currentPdf = PdfDocument.FromFile(pdfPaths[i]);
            string currentText = currentPdf.ExtractAllText();
            if (referenceText != currentText)
            {
                Console.WriteLine($"PDF {i} differs from reference");
            }
        }
        // Results saved for further processing
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This approach allows developers to compare multiple PDF documents against a reference document, perfect for batch processing requirements.

Output

How to Compare Two PDF Files in C#: Figure 7 - Comapre Multiple PDFs Output

Working with Password-Protected PDFs

IronPDF seamlessly handles encrypted PDF documents using simple steps. Pass passwords when loading protected files:

public static void CompareSecuredPdfs(string pdf1Path, string pdf2Path, 
                                      string password1, string password2)
{
    // Load and compare two PDFs with passwords
    var pdf1 = PdfDocument.FromFile(pdf1Path, password1);
    var pdf2 = PdfDocument.FromFile(pdf2Path, password2);
    string text1 = pdf1.ExtractAllText();
    string text2 = pdf2.ExtractAllText();
    // Compare two PDF files and save results
    bool identical = text1.Equals(text2);
    var comparisonResult = identical ? "identical" : "different";
    Console.WriteLine($"Secured PDFs are {comparisonResult}");
    // Accept or reject changes based on comparison
}
public static void CompareSecuredPdfs(string pdf1Path, string pdf2Path, 
                                      string password1, string password2)
{
    // Load and compare two PDFs with passwords
    var pdf1 = PdfDocument.FromFile(pdf1Path, password1);
    var pdf2 = PdfDocument.FromFile(pdf2Path, password2);
    string text1 = pdf1.ExtractAllText();
    string text2 = pdf2.ExtractAllText();
    // Compare two PDF files and save results
    bool identical = text1.Equals(text2);
    var comparisonResult = identical ? "identical" : "different";
    Console.WriteLine($"Secured PDFs are {comparisonResult}");
    // Accept or reject changes based on comparison
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

By passing passwords when calling the FromFile method, you can compare two PDF files that are encrypted, perfect for sensitive document workflows.

Creating Comparison Reports

Generate detailed comparison results and save them for review:

public static void CreateComparisonReport(string pdf1Path, string pdf2Path)
{
    var pdf1 = PdfDocument.FromFile(pdf1Path);
    var pdf2 = PdfDocument.FromFile(pdf2Path);
    // Extract and compare
    var differences = new List<string>();
    for (int i = 0; i < Math.Max(pdf1.PageCount, pdf2.PageCount); i++)
    {
        // Extract page text (guard for missing pages)
        string page1Text = i < pdf1.PageCount ? pdf1.ExtractTextFromPage(i) ??     string.Empty : string.Empty;
        string page2Text = i < pdf2.PageCount ? pdf2.ExtractTextFromPage(i) ?? string.Empty : string.Empty;
        // If identical, no entry needed
        if (page1Text == page2Text) continue;
        // Compute a simple similarity score (0..1)
        double similarity = CalculateSimilarity(page1Text, page2Text);
        differences.Add($"Page {i + 1}: Similarity {similarity:P}. Lengths:  [{page1Text.Length}, {page2Text.Length}].");
    }
    // Create output report
    var renderer = new ChromePdfRenderer();
    var sb = new System.Text.StringBuilder();
    sb.Append($"<h1>PDF Comparison Results</h1>");
    sb.Append($"<p>Total differences: {differences.Count}</p>");
    if (differences.Count > 0)
    {
     sb.Append("<ol>");
     foreach (var d in differences)
     {
       sb.Append($"<li><pre style='white-space:pre-wrap'>{d}</pre></li>");
     }
     sb.Append("</ol>");
    }
    else
    {
     sb.Append("<p>No page-level differences detected.</p>");
    }
    var reportHtml = sb.ToString();
    var reportPdf = renderer.RenderHtmlAsPdf(reportHtml);
    reportPdf.SaveAs("comparison-report.pdf");
}
public static void CreateComparisonReport(string pdf1Path, string pdf2Path)
{
    var pdf1 = PdfDocument.FromFile(pdf1Path);
    var pdf2 = PdfDocument.FromFile(pdf2Path);
    // Extract and compare
    var differences = new List<string>();
    for (int i = 0; i < Math.Max(pdf1.PageCount, pdf2.PageCount); i++)
    {
        // Extract page text (guard for missing pages)
        string page1Text = i < pdf1.PageCount ? pdf1.ExtractTextFromPage(i) ??     string.Empty : string.Empty;
        string page2Text = i < pdf2.PageCount ? pdf2.ExtractTextFromPage(i) ?? string.Empty : string.Empty;
        // If identical, no entry needed
        if (page1Text == page2Text) continue;
        // Compute a simple similarity score (0..1)
        double similarity = CalculateSimilarity(page1Text, page2Text);
        differences.Add($"Page {i + 1}: Similarity {similarity:P}. Lengths:  [{page1Text.Length}, {page2Text.Length}].");
    }
    // Create output report
    var renderer = new ChromePdfRenderer();
    var sb = new System.Text.StringBuilder();
    sb.Append($"<h1>PDF Comparison Results</h1>");
    sb.Append($"<p>Total differences: {differences.Count}</p>");
    if (differences.Count > 0)
    {
     sb.Append("<ol>");
     foreach (var d in differences)
     {
       sb.Append($"<li><pre style='white-space:pre-wrap'>{d}</pre></li>");
     }
     sb.Append("</ol>");
    }
    else
    {
     sb.Append("<p>No page-level differences detected.</p>");
    }
    var reportHtml = sb.ToString();
    var reportPdf = renderer.RenderHtmlAsPdf(reportHtml);
    reportPdf.SaveAs("comparison-report.pdf");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

How to Compare Two PDF Files in C#: Figure 8 - Comaprison Report Output

Why Choose IronPDF

IronPDF excels at PDF comparison through its straightforward API. The library supports .NET Core, .NET Framework, and runs on Windows, Linux, and macOS. Key advantages include:

  • Simple API to compare PDF files
  • Support for different versions of PDFs
  • Built-in handling of revisions
  • Easy to develop and create comparison tools
  • Comprehensive documentation and support

How to Compare Two PDF Files in C#: Figure 9 - Cross-platform compatibility

Conclusion

IronPDF transforms complex PDF document comparison tasks into manageable operations. Whether creating document management systems or comparing two PDF files using C#, IronPDF provides all the tools needed.

How to Compare Two PDF Files in C#: Figure 10 - Compare two PDF files using C# - IronPDF

Ready to learn more? Download IronPDF's free trial and set up PDF files using professional-grade comparison capabilities. For production deployment, explore our licensing options and refer to our comprehensive documentation for additional details.

How to Compare Two PDF Files in C#: Figure 11 - Licensing

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of ...Read More
Ready to Get Started?
Nuget Downloads 15,824,053 | Version: 2025.10 just released