documenten vergelijken .net – Complete GroupDocs Implementatiegids
If you need to compare documents .net, you’ve come to the right place. Imagine opening two contracts that look identical and instantly spotting every change—no manual scrolling, no missed edits. That’s the power of automated document comparison, and with GroupDocs.Comparison for .NET you can make it happen in minutes.
Snelle Antwoorden
- Welke bibliotheek behandelt documentvergelijking in .NET? GroupDocs.Comparison.
- Kan ik Word-, Excel- en PDF-bestanden vergelijken? Ja—meer dan 50 formaten worden ondersteund.
- Welke versie moet ik gebruiken? Versie 25.4.0 biedt de beste prestaties en stabiliteit.
- Heb ik een licentie nodig voor productie? Een commerciële licentie is vereist voor productie‑implementaties.
- Is async verwerking mogelijk? Absoluut—gebruik
Task.Runmet de comparison API.
Wat is GroupDocs.Comparison?
GroupDocs.Comparison is een .NET‑bibliotheek die programmatisch verschillen tussen twee of meer documenten detecteert en een gemarkeerd resultaatbestand genereert. Het ondersteunt meer dan 50 formaten, verwerkt documenten van honderden pagina’s zonder de volledige inhoud in het geheugen te laden, en biedt fijnmazige controle over vergelijking‑instellingen.
Waarom GroupDocs.Comparison gebruiken voor het vergelijken van documenten .net?
GroupDocs.Comparison levert snelle, nauwkeurige en schaalbare document‑diffing voor .NET‑applicaties. Het kan grote PDF‑ en Office‑bestanden in seconden verwerken, behoudt opmaak en visuele getrouwheid terwijl inserties, deleties en stijlwijzigingen worden gemarkeerd. De bibliotheek werkt op .NET Core, .NET 5/6/7 en het volledige .NET Framework, waardoor het een veelzijdige keuze is voor elk project.
- Snelheid: Verwerkt een PDF van 200 pagina’s in minder dan 2 seconden op een standaard server.
- Nauwkeurigheid: Detecteert tekst, opmaak, tabellen en afbeeldingen met 99,9 % getrouwheid.
- Schaalbaarheid: Verwerkt batch‑taken van duizenden bestanden met streaming‑API’s.
- Flexibiliteit: Werkt met .NET Core 3.1+, .NET 5/6/7 en .NET Framework 4.6.1+.
Vereisten
- Ontwikkelomgeving: .NET Core 3.1 of nieuwer, of .NET Framework 4.6.1 +
- GroupDocs.Comparison Bibliotheek: Versie 25.4.0 (geïnstalleerd via NuGet)
- Voorbeeldbestanden: Word-, PDF- of Excel‑documenten voor testen
- Basis C#‑kennis: Klassen, methoden,
using‑statements
Leuk om te hebben (optioneel)
- Bekendheid met NuGet‑pakketbeheer
- Ervaring met bestands‑I/O en streams
- Begrip van async/await‑patronen
Hoe documenten vergelijken .net met GroupDocs.Comparison?
Om twee documenten te vergelijken met GroupDocs.Comparison, laad elk bestand in een stream, configureer optionele ComparisonSettings en roep de Compare‑methode aan op een Comparer‑instantie. De API retourneert een resultaatdocument dat verschillen markeert, en je kunt het opslaan in elk ondersteund formaat. Deze aanpak vereist slechts een paar regels code terwijl je volledige controle krijgt over het vergelijkingsproces.
Stapsgewijze implementatie
1️⃣ Slim Documentpadbeheer
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";
}
Waarom dit werkt:
- Één plek om paden bij te werken voor dev, test of productie.
- Elimineert hard‑gecodeerde strings verspreid over de codebase.
2️⃣ Kernvergelijkingslogica
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);
}
Definitie‑anker:Comparer is de kernklasse van GroupDocs.Comparison die documentanalyse orkestreert en het gemarkeerde resultaat produceert.
Hoe het helpt:
- Ondersteunt meerdere doel‑documenten via herhaalde
Add()‑aanroepen. - Genereert een resultaatbestand met wijzigingen gemarkeerd in rood, groen of aangepaste kleuren.
3️⃣ Geavanceerde instellingen voor Excel en PDF
You can fine‑tune the comparison to ignore formatting or focus on data changes—perfect for compare excel files c# scenarios.
CompareOptions compareOptions = new CompareOptions()
{
ShowDeletedContent = true,
ShowInsertedContent = true,
StyleChangeDetection = true
};
using (Comparer comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(outputPath, compareOptions);
}
Definitie‑anker:ComparisonSettings stelt je in staat specifieke wijzigingstypen in of uit te schakelen, zoals DetectStyleChanges of DetectTableChanges.
4️⃣ Meerdere formaten verwerken
GroupDocs.Comparison detecteert automatisch het bestandstype, maar je kunt ook een formaat forceren wanneer nodig—ideaal voor 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️⃣ Grote bestanden streamen
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️⃣ Robuuste foutafhandeling
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}");
}
Beste praktijken voor documentvergelijking
- Valideer invoerbestanden voordat je de API aanroept om vroegtijdig niet‑ondersteunde formaten te detecteren.
- Gebruik
Path.Combinevoor cross‑platform padconstructie (zie Valkuil #1). - Schakel alleen benodigde wijzigingstypen in om prestaties te verbeteren (bijv. stijldetectie uitschakelen voor data‑gerichte Excel‑bladen).
- Dispose
Comparer‑objecten onmiddellijk om native bronnen vrij te geven.
Veelvoorkomende valkuilen en hoe ze te vermijden
Valkuil #1: Pad‑scheidingstekenproblemen
Oplossing: Bouw paden altijd met Path.Combine() en 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");
Valkuil #2: Geheugentekort bij grote bestanden
Oplossing: Schakel over naar streaming‑modus voor bestanden groter dan 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);
}
Valkuil #3: Exceptions negeren
Oplossing: Implementeer uitgebreide try‑catch‑blokken en log stacktraces.
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}");
}
Strategieën voor prestatie‑optimalisatie
Geheugenbeheer
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();
}
}
Asynchrone verwerking
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;
}
});
}
Integratie met populaire .NET‑frameworks
ASP.NET Core Web API‑integratie
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‑componentintegratie
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
}
}
Praktijkvoorbeelden
Scenario 1: Juridische contractbeoordeling
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
};
}
}
Scenario 2: Versiebeheer voor spreadsheets
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);
}
}
Probleemoplossingsgids
Probleem 1: “Bestandsformaat niet ondersteund”
Oplossing: Controleer de bestandsextensie tegen de ondersteunde lijst (50+ formaten) en converteer niet‑ondersteunde types eerst naar PDF.
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);
}
Probleem 2: Geheugenproblemen met grote bestanden
Oplossing: Schakel streaming in en verwerk bestanden in delen.
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);
}
}
}
Probleem 3: Leeg vergelijkingsresultaat
Oplossing: Verhoog de gevoeligheid door DetectFormattingChanges of DetectStyleChanges in te schakelen.
var compareOptions = new CompareOptions
{
DetectStyleChanges = true,
DiagramMasterSetting = new DiagramMasterSetting
{
UseSourceMaster = true,
CloneSourceMaster = true
},
OriginalSize = new Size(600, 800),
HeaderFootersComparison = true,
PaperSize = PaperSize.A4
};
Veelgestelde vragen
V: Hoeveel documenten kan ik tegelijk vergelijken?
Je kunt meerdere doel‑documenten toevoegen aan één Comparer‑instantie met herhaalde Add()‑aanroepen, maar het wordt aanbevolen ze sequentieel te verwerken voor grote batches.
V: Kan GroupDocs.Comparison wachtwoord‑beveiligde bestanden verwerken?
Ja—geef het wachtwoord door bij het construeren van de Comparer of bij het laden van het document.
using (var comparer = new Comparer(sourcePath, new LoadOptions("password")))
{
comparer.Add(targetPath, new LoadOptions("targetPassword"));
comparer.Compare(outputPath);
}
V: Welke bestandsformaten ondersteunt GroupDocs.Comparison?
Meer dan 50 formaten, waaronder DOCX, XLSX, PPTX, PDF, JPEG, PNG, TXT en meer.
V: Hoe pas ik het uiterlijk van wijzigingen aan?
Gebruik ComparisonSettings om InsertedColor, DeletedColor en StyleChangeColor in te stellen.
var compareOptions = new CompareOptions
{
InsertedItemStyle = new StyleSettings
{
HighlightColor = Color.Green,
FontColor = Color.DarkGreen
},
DeletedItemStyle = new StyleSettings
{
HighlightColor = Color.Red,
FontColor = Color.DarkRed
}
};
V: Is het mogelijk specifieke wijzigingstypen te negeren?
Absoluut—schakel opties zoals DetectStyleChanges of DetectTableChanges uit in ComparisonSettings.
var compareOptions = new CompareOptions
{
DetectStyleChanges = false, // Ignore formatting changes
HeaderFootersComparison = false, // Skip headers/footers
WordsSeparatorChars = new[] { ' ', '\n', '\r', '\t' } // Define word boundaries
};
V: Kan ik documenten vergelijken die in cloud‑opslag staan?
Ja—download de streams lokaal of vergelijk direct vanuit een 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);
}
V: Hoe voer ik GroupDocs.Comparison uit in een Docker‑container?
Neem de benodigde native dependencies op in je Dockerfile en kopieer het licentiebestand naar de 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
V: Welke licentie is vereist voor productie?
Een commerciële GroupDocs.Comparison‑licentie is verplicht voor productie‑implementaties. Opties omvatten developer, site en OEM‑licenties.
V: Hoe moet ik vergelijking‑fouten elegant afhandelen?
Wikkel de vergelijkingsaanroep in een try‑catch‑blok, log de exception en retourneer een gebruiksvriendelijke foutmelding.
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" };
}
Essentiële bronnen en documentatie
- Complete Documentation: GroupDocs Comparison .NET Docs
- API Reference Guide: GroupDocs API Reference for .NET
- Community Support Forum: GroupDocs Forum
- Latest Release Downloads: Releases Page
- Free Trial Download: Try Free Version
- Temporary License Application: Apply for Temporary License
- Purchase Full License: Buy GroupDocs License
- Releases: Releases
- Temporary License Page: Temporary License Page
- Purchase Page: Purchase Page
Laatst bijgewerkt: 2026-06-10
Getest met: GroupDocs.Comparison 25.4.0 for .NET
Auteur: GroupDocs
Install-Package GroupDocs.Comparison -Version 25.4.0
dotnet add package GroupDocs.Comparison --version 25.4.0
using GroupDocs.Comparison;