เปรียบเทียบเอกสาร .net – คู่มือการใช้งาน GroupDocs อย่างสมบูรณ์

หากคุณต้องการ compare documents .net คุณมาถูกที่แล้ว ลองนึกภาพการเปิดสัญญาสองฉบับที่ดูเหมือนกันและทันทีที่เห็นการเปลี่ยนแปลงทั้งหมด—ไม่มีการเลื่อนดูด้วยตนเอง ไม่มีการแก้ไขที่พลาด นั่นคือพลังของการเปรียบเทียบเอกสารอัตโนมัติ และด้วย GroupDocs.Comparison for .NET คุณสามารถทำได้ในไม่กี่นาที

คำตอบด่วน

  • ไลบรารีใดที่จัดการการเปรียบเทียบเอกสารใน .NET? GroupDocs.Comparison.
  • ฉันสามารถเปรียบเทียบไฟล์ Word, Excel และ PDF ได้หรือไม่? Yes—over 50 formats are supported.
  • ควรใช้เวอร์ชันใด? Version 25.4.0 offers the best performance and stability.
  • ฉันต้องการไลเซนส์สำหรับการใช้งานจริงหรือไม่? A commercial license is required for production deployments.
  • การประมวลผลแบบ async เป็นไปได้หรือไม่? Absolutely—use Task.Run with the comparison API.

GroupDocs.Comparison คืออะไร?

GroupDocs.Comparison เป็นไลบรารี .NET ที่ตรวจจับความแตกต่างระหว่างเอกสารสองฉบับหรือมากกว่าโดยอัตโนมัติและสร้างไฟล์ผลลัพธ์ที่ไฮไลท์. รองรับกว่า 50 รูปแบบ, ประมวลผลไฟล์หลายร้อยหน้าโดยไม่ต้องโหลดเนื้อหาทั้งหมดเข้าสู่หน่วยความจำ, และให้การควบคุมละเอียดของการตั้งค่าการเปรียบเทียบ

ทำไมต้องใช้ GroupDocs.Comparison สำหรับ compare documents .net?

GroupDocs.Comparison delivers fast, accurate, and scalable document diffing for .NET applications. It can process large PDFs and Office files in seconds, preserving formatting and visual fidelity while highlighting insertions, deletions, and style changes. The library works across .NET Core, .NET 5/6/7, and the full .NET Framework, making it a versatile choice for any project.

  • ความเร็ว: Processes a 200‑page PDF in under 2 seconds on a standard server.
  • ความแม่นยำ: Detects text, formatting, tables, and images with 99.9 % fidelity.
  • ความสามารถในการขยาย: Handles batch jobs of thousands of files using streaming APIs.
  • ความยืดหยุ่น: Works with .NET Core 3.1+, .NET 5/6/7, and .NET Framework 4.6.1+.

ข้อกำหนดเบื้องต้น

  • สภาพแวดล้อมการพัฒนา: .NET Core 3.1 หรือใหม่กว่า, หรือ .NET Framework 4.6.1 +
  • ไลบรารี GroupDocs.Comparison: Version 25.4.0 (installed via NuGet)
  • ไฟล์ตัวอย่าง: เอกสาร Word, PDF, หรือ Excel สำหรับการทดสอบ
  • ความรู้พื้นฐาน C#: คลาส, เมธอด, คำสั่ง using

สิ่งที่ควรมี (ไม่บังคับ)

  • ความคุ้นเคยกับการจัดการแพ็กเกจ NuGet
  • ประสบการณ์กับไฟล์ I/O และสตรีม
  • ความเข้าใจในรูปแบบ async/await

วิธีเปรียบเทียบเอกสาร .net ด้วย GroupDocs.Comparison?

To compare two documents with GroupDocs.Comparison, load each file into a stream, configure optional ComparisonSettings, and invoke the Compare method on a Comparer instance. The API returns a result document that highlights differences, and you can save it to any supported format. This approach requires only a few lines of code while giving you full control over the comparison process.

การดำเนินการแบบขั้นตอน

1️⃣ การจัดการเส้นทางเอกสารอัจฉริยะ

Centralizing file paths prevents “file not found” errors and makes environment switches painless.

public static class Constants
{
    public const string SOURCE_WORD = @"YOUR_DOCUMENT_DIRECTORY\source.docx";
    public const string TARGET_WORD = @"YOUR_DOCUMENT_DIRECTORY\target.docx";

    public static string GetOutputDirectoryPath()
    {
        return @"YOUR_OUTPUT_DIRECTORY";
    }

    public const string RESULT_WORD = "result.docx";
}

ทำไมวิธีนี้ถึงได้ผล:

  • One place to update paths for dev, test, or production.
  • Eliminates hard‑coded strings scattered throughout the codebase.

2️⃣ โลจิกการเปรียบเทียบหลัก

The Comparer class is the engine that drives the diff algorithm.

using (Comparer comparer = new Comparer(Constants.SOURCE_WORD))
{
    // Add the target document for comparison
    comparer.Add(Constants.TARGET_WORD);

    // Perform the comparison and save the result
    string outputFileName = Path.Combine(Constants.GetOutputDirectoryPath(), Constants.RESULT_WORD);
    comparer.Compare(outputFileName);
}

คำนิยาม anchor:
Comparer คือคลาสหลักของ GroupDocs.Comparison ที่ประสานการวิเคราะห์เอกสารและสร้างผลลัพธ์ที่ไฮไลท์.

วิธีที่ช่วย:

  • รองรับเอกสารเป้าหมายหลายไฟล์ผ่านการเรียก Add() ซ้ำหลายครั้ง.
  • สร้างไฟล์ผลลัพธ์ที่แสดงการเปลี่ยนแปลงด้วยสีแดง, สีเขียว, หรือสีที่กำหนดเอง.

3️⃣ การตั้งค่าขั้นสูงสำหรับ Excel และ PDF

คุณสามารถปรับแต่งการเปรียบเทียบให้ละเว้นการจัดรูปแบบหรือเน้นการเปลี่ยนแปลงข้อมูล—เหมาะสำหรับสถานการณ์ compare excel files c#.

CompareOptions compareOptions = new CompareOptions()
{
    ShowDeletedContent = true,
    ShowInsertedContent = true,
    StyleChangeDetection = true
};

using (Comparer comparer = new Comparer(sourcePath))
{
    comparer.Add(targetPath);
    comparer.Compare(outputPath, compareOptions);
}

คำนิยาม anchor:
ComparisonSettings ให้คุณเปิดหรือปิดประเภทการเปลี่ยนแปลงเฉพาะ เช่น DetectStyleChanges หรือ DetectTableChanges.

4️⃣ การจัดการหลายรูปแบบ

GroupDocs.Comparison automatically detects the file type, but you can also force a format when needed—ideal for compare pdf files c#.

public static void CompareDocumentsByType(string sourcePath, string targetPath, string outputPath)
{
    string extension = Path.GetExtension(sourcePath).ToLower();
    
    using (Comparer comparer = new Comparer(sourcePath))
    {
        comparer.Add(targetPath);
        
        CompareOptions options = GetOptionsForFormat(extension);
        comparer.Compare(outputPath, options);
    }
}

private static CompareOptions GetOptionsForFormat(string extension)
{
    switch (extension)
    {
        case ".pdf":
            return new CompareOptions { DetectStyleChanges = false };
        case ".xlsx":
            return new CompareOptions { CalculateCoordinates = true };
        default:
            return new CompareOptions();
    }
}

5️⃣ การสตรีมไฟล์ขนาดใหญ่

When processing massive documents, streaming avoids loading the entire file into memory.

// For files over 50MB, consider this approach
using (var sourceStream = File.OpenRead(sourcePath))
using (var targetStream = File.OpenRead(targetPath))
using (var comparer = new Comparer(sourceStream))
{
    comparer.Add(targetStream);
    comparer.Compare(outputStream);
}

6️⃣ การจัดการข้อผิดพลาดที่แข็งแกร่ง

Never let a corrupted file crash your service; wrap calls in try‑catch blocks and log useful details.

try
{
    using (Comparer comparer = new Comparer(sourcePath))
    {
        comparer.Add(targetPath);
        comparer.Compare(outputPath);
    }
}
catch (FileNotFoundException ex)
{
    Console.WriteLine($"Document not found: {ex.FileName}");
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"Access denied: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Comparison failed: {ex.Message}");
}

แนวทางปฏิบัติที่ดีที่สุดสำหรับการเปรียบเทียบเอกสาร

  • Validate input files ก่อนเรียก API เพื่อจับรูปแบบที่ไม่รองรับตั้งแต่ต้น.
  • Use Path.Combine สำหรับการสร้างเส้นทางข้ามแพลตฟอร์ม (ดู Pitfall #1).
  • Enable only needed change types เพื่อปรับปรุงประสิทธิภาพ (เช่น ปิดการตรวจจับสไตล์สำหรับแผ่น Excel ที่เน้นข้อมูล).
  • Dispose of Comparer objects ทันทีเพื่อปล่อยทรัพยากรเนทีฟ.

ข้อผิดพลาดทั่วไปและวิธีหลีกเลี่ยง

Pitfall #1: ปัญหาเครื่องหมายแยกเส้นทาง

Solution: Always build paths with Path.Combine() and Path.DirectorySeparatorChar.

// Wrong - will break on different operating systems
string path = "C:/Documents/source.docx";

// Right - works everywhere
string path = Path.Combine("C:", "Documents", "source.docx");

Pitfall #2: การใช้หน่วยความจำเกินขนาดบนไฟล์ใหญ่

Solution: Switch to streaming mode for files larger than 50 MB.

// For files over 50MB, consider this approach
using (var sourceStream = File.OpenRead(sourcePath))
using (var targetStream = File.OpenRead(targetPath))
using (var comparer = new Comparer(sourceStream))
{
    comparer.Add(targetStream);
    comparer.Compare(outputStream);
}

Pitfall #3: การละเลยข้อยกเว้น

Solution: Implement comprehensive try‑catch blocks and log stack traces.

try
{
    using (Comparer comparer = new Comparer(sourcePath))
    {
        comparer.Add(targetPath);
        comparer.Compare(outputPath);
    }
}
catch (FileNotFoundException ex)
{
    Console.WriteLine($"Document not found: {ex.FileName}");
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine($"Access denied: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Comparison failed: {ex.Message}");
}

กลยุทธ์การเพิ่มประสิทธิภาพ

การจัดการหน่วยความจำ

Reuse Comparer instances when possible and call Dispose() after each comparison.

// Always dispose of resources properly
using (var comparer = new Comparer(sourcePath))
{
    comparer.Add(targetPath);
    comparer.Compare(outputPath);
    
    // Comparer is automatically disposed here
}

// For batch processing, clear resources between comparisons
for (int i = 0; i < documentPairs.Count; i++)
{
    using (var comparer = new Comparer(documentPairs[i].Source))
    {
        comparer.Add(documentPairs[i].Target);
        comparer.Compare(documentPairs[i].Output);
    }
    
    // Force garbage collection every 10 documents if needed
    if (i % 10 == 0)
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

การประมวลผลแบบอะซิงโครนัส

Run comparisons on background threads to keep UI responsive.

public async Task<bool> CompareDocumentsAsync(string sourcePath, string targetPath, string outputPath)
{
    return await Task.Run(() =>
    {
        try
        {
            using (var comparer = new Comparer(sourcePath))
            {
                comparer.Add(targetPath);
                comparer.Compare(outputPath);
                return true;
            }
        }
        catch
        {
            return false;
        }
    });
}

การผสานรวมกับ .NET Framework ยอดนิยม

การผสานรวม ASP.NET Core Web API

Expose a REST endpoint that accepts two files and returns the diff result.

[ApiController]
[Route("api/[controller]")]
public class DocumentComparisonController : ControllerBase
{
    [HttpPost("compare")]
    public async Task<IActionResult> CompareDocuments([FromForm] IFormFile sourceFile, [FromForm] IFormFile targetFile)
    {
        if (sourceFile == null || targetFile == null)
            return BadRequest("Both source and target files are required.");

        var tempFolder = Path.GetTempPath();
        var sourcePath = Path.Combine(tempFolder, sourceFile.FileName);
        var targetPath = Path.Combine(tempFolder, targetFile.FileName);
        var outputPath = Path.Combine(tempFolder, $"comparison_{Guid.NewGuid()}.pdf");

        try
        {
            // Save uploaded files
            using (var stream = new FileStream(sourcePath, FileMode.Create))
                await sourceFile.CopyToAsync(stream);
            
            using (var stream = new FileStream(targetPath, FileMode.Create))
                await targetFile.CopyToAsync(stream);

            // Perform comparison
            using (var comparer = new Comparer(sourcePath))
            {
                comparer.Add(targetPath);
                comparer.Compare(outputPath);
            }

            // Return the result file
            var fileBytes = await System.IO.File.ReadAllBytesAsync(outputPath);
            return File(fileBytes, "application/pdf", "comparison_result.pdf");
        }
        finally
        {
            // Clean up temp files
            File.Delete(sourcePath);
            File.Delete(targetPath);
            File.Delete(outputPath);
        }
    }
}

การผสานรวมคอมโพเนนต์ Blazor

Create a reusable component that shows side‑by‑side comparison in the browser.

@using GroupDocs.Comparison
@inject IJSRuntime JSRuntime

<div class="document-comparison">
    <InputFile OnChange="HandleFileSelection" multiple />
    <button @onclick="CompareDocuments" disabled="@(!CanCompare)">Compare Documents</button>
    
    @if (comparisonResult != null)
    {
        <div class="result">
            <a href="@comparisonResult" download="comparison_result.pdf">Download Result</a>
        </div>
    }
</div>

@code {
    private List<IBrowserFile> selectedFiles = new();
    private string comparisonResult;
    
    private bool CanCompare => selectedFiles.Count == 2;

    private async Task HandleFileSelection(InputFileChangeEventArgs e)
    {
        selectedFiles = e.GetMultipleFiles(2).ToList();
    }

    private async Task CompareDocuments()
    {
        if (selectedFiles.Count != 2) return;

        // Implementation similar to Web API example
        // Save files, compare, and generate result
    }
}

กรณีการใช้งานจริง

สถานการณ์ 1: การตรวจสอบสัญญากฎหมาย

Law firms can automatically highlight additions, deletions, and formatting changes across contract revisions.

public class ContractReviewService
{
    public ContractComparisonResult ReviewContract(string originalContract, string revisedContract)
    {
        var outputPath = Path.Combine(Path.GetTempPath(), $"contract_review_{DateTime.Now:yyyyMMddHHmmss}.docx");
        
        var compareOptions = new CompareOptions
        {
            ShowDeletedContent = true,
            ShowInsertedContent = true,
            StyleChangeDetection = true,
            WordsSeparatorChars = new[] { ' ', '.', ',', '!', '?' }
        };

        using (var comparer = new Comparer(originalContract))
        {
            comparer.Add(revisedContract);
            comparer.Compare(outputPath, compareOptions);
        }

        return new ContractComparisonResult
        {
            OutputPath = outputPath,
            HasChanges = File.Exists(outputPath),
            ComparisonDate = DateTime.Now
        };
    }
}

สถานการณ์ 2: การควบคุมเวอร์ชันสำหรับสเปรดชีต

Finance teams can detect changes in Excel models without manually opening each file.

public class DocumentVersionControl
{
    public void TrackDocumentChanges(string documentPath, string repositoryPath)
    {
        var versions = Directory.GetFiles(repositoryPath, "*.docx").OrderBy(f => f);
        var latestVersion = versions.LastOrDefault();

        if (latestVersion != null)
        {
            var comparisonPath = Path.Combine(repositoryPath, $"changes_{DateTime.Now:yyyyMMdd}.docx");
            
            using (var comparer = new Comparer(latestVersion))
            {
                comparer.Add(documentPath);
                comparer.Compare(comparisonPath);
            }

            // Archive the comparison for future reference
            ArchiveComparison(comparisonPath);
        }
    }

    private void ArchiveComparison(string comparisonPath)
    {
        // Implementation for archiving comparison results
        var archivePath = Path.Combine(Path.GetDirectoryName(comparisonPath), "archive", Path.GetFileName(comparisonPath));
        Directory.CreateDirectory(Path.GetDirectoryName(archivePath));
        File.Move(comparisonPath, archivePath);
    }
}

คู่มือการแก้ไขปัญหา

Issue 1: “File format not supported”

Solution: Verify the file extension against the supported list (50+ formats) and convert unsupported types to PDF first.

private static readonly HashSet<string> SupportedFormats = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".docx", ".doc", ".pdf", ".xlsx", ".xls", ".pptx", ".ppt", ".txt", ".rtf"
};

public static bool IsFormatSupported(string filePath)
{
    var extension = Path.GetExtension(filePath);
    return SupportedFormats.Contains(extension);
}

Issue 2: ปัญหาหน่วยความจำกับไฟล์ขนาดใหญ่

Solution: Enable streaming and process files in chunks.

public static void CompareLargeDocuments(string sourcePath, string targetPath, string outputPath)
{
    var fileInfo = new FileInfo(sourcePath);
    
    if (fileInfo.Length > 50 * 1024 * 1024) // 50MB threshold
    {
        // Use streaming approach
        using (var sourceStream = File.OpenRead(sourcePath))
        using (var targetStream = File.OpenRead(targetPath))
        using (var outputStream = File.Create(outputPath))
        using (var comparer = new Comparer(sourceStream))
        {
            comparer.Add(targetStream);
            comparer.Compare(outputStream);
        }
    }
    else
    {
        // Standard approach for smaller files
        using (var comparer = new Comparer(sourcePath))
        {
            comparer.Add(targetPath);
            comparer.Compare(outputPath);
        }
    }
}

Issue 3: ผลลัพธ์การเปรียบเทียบว่างเปล่า

Solution: Increase sensitivity by toggling DetectFormattingChanges or DetectStyleChanges.

var compareOptions = new CompareOptions
{
    DetectStyleChanges = true,
    DiagramMasterSetting = new DiagramMasterSetting
    {
        UseSourceMaster = true,
        CloneSourceMaster = true
    },
    OriginalSize = new Size(600, 800),
    HeaderFootersComparison = true,
    PaperSize = PaperSize.A4
};

คำถามที่พบบ่อย

Q: ฉันสามารถเปรียบเทียบเอกสารได้กี่ฉบับพร้อมกัน?
A: You can add multiple target documents to a single Comparer instance using repeated Add() calls, but processing them sequentially is recommended for large batches.

Q: GroupDocs.Comparison สามารถจัดการไฟล์ที่มีการป้องกันด้วยรหัสผ่านได้หรือไม่?
A: Yes—pass the password when constructing the Comparer or loading the document.

using (var comparer = new Comparer(sourcePath, new LoadOptions("password")))
{
    comparer.Add(targetPath, new LoadOptions("targetPassword"));
    comparer.Compare(outputPath);
}

Q: GroupDocs.Comparison รองรับรูปแบบไฟล์อะไรบ้าง?
A: Over 50 formats, including DOCX, XLSX, PPTX, PDF, JPEG, PNG, TXT, and more.

Q: ฉันจะปรับแต่งลักษณะการเปลี่ยนแปลงอย่างไร?
A: Use ComparisonSettings to set InsertedColor, DeletedColor, and StyleChangeColor.

var compareOptions = new CompareOptions
{
    InsertedItemStyle = new StyleSettings
    {
        HighlightColor = Color.Green,
        FontColor = Color.DarkGreen
    },
    DeletedItemStyle = new StyleSettings
    {
        HighlightColor = Color.Red,
        FontColor = Color.DarkRed
    }
};

Q: สามารถละเว้นประเภทการเปลี่ยนแปลงเฉพาะได้หรือไม่?
A: Absolutely—disable options like DetectStyleChanges or DetectTableChanges in ComparisonSettings.

var compareOptions = new CompareOptions
{
    DetectStyleChanges = false, // Ignore formatting changes
    HeaderFootersComparison = false, // Skip headers/footers
    WordsSeparatorChars = new[] { ' ', '\n', '\r', '\t' } // Define word boundaries
};

Q: ฉันสามารถเปรียบเทียบเอกสารที่เก็บในคลาวด์ได้หรือไม่?
A: Yes—download the streams locally or compare directly from a MemoryStream.

using (var sourceStream = await DownloadFromCloudAsync(sourceUrl))
using (var targetStream = await DownloadFromCloudAsync(targetUrl))
using (var comparer = new Comparer(sourceStream))
{
    comparer.Add(targetStream);
    comparer.Compare(outputStream);
}

Q: ฉันจะรัน GroupDocs.Comparison ภายในคอนเทนเนอร์ Docker อย่างไร?
A: Include the necessary native dependencies in your Dockerfile and copy the license file into the container.

FROM mcr.microsoft.com/dotnet/aspnet:6.0
# Install necessary fonts and libraries for document processing
RUN apt-get update && apt-get install -y libfontconfig1 libfreetype6

Q: ต้องการไลเซนส์แบบใดสำหรับการใช้งานจริง?
A: A commercial GroupDocs.Comparison license is mandatory for production deployments. Options include developer, site, and OEM licenses.

Q: ฉันควรจัดการกับความล้มเหลวของการเปรียบเทียบอย่างไรให้ราบรื่น?
A: Wrap the comparison call in a try‑catch block, log the exception, and return a user‑friendly error message.

public async Task<ComparisonResult> CompareDocumentsWithRetry(string source, string target, int maxRetries = 3)
{
    for (int attempt = 1; attempt <= maxRetries; attempt++)
    {
        try
        {
            using (var comparer = new Comparer(source))
            {
                comparer.Add(target);
                var outputPath = GenerateOutputPath();
                comparer.Compare(outputPath);
                
                return new ComparisonResult { Success = true, OutputPath = outputPath };
            }
        }
        catch (Exception ex) when (attempt < maxRetries)
        {
            _logger.LogWarning($"Comparison attempt {attempt} failed: {ex.Message}. Retrying...");
            await Task.Delay(TimeSpan.FromSeconds(attempt * 2)); // Exponential backoff
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, $"Comparison failed after {maxRetries} attempts");
            return new ComparisonResult { Success = false, Error = ex.Message };
        }
    }
    
    return new ComparisonResult { Success = false, Error = "Max retries exceeded" };
}

แหล่งข้อมูลและเอกสารสำคัญ


อัปเดตล่าสุด: 2026-06-10
ทดสอบด้วย: GroupDocs.Comparison 25.4.0 for .NET
ผู้เขียน: GroupDocs

Install-Package GroupDocs.Comparison -Version 25.4.0
dotnet add package GroupDocs.Comparison --version 25.4.0
using GroupDocs.Comparison;

บทเรียนที่เกี่ยวข้อง