| title | Preserve Target Metadata with GroupDocs.Comparison – .NET Tutorial | ||||
|---|---|---|---|---|---|
| linktitle | Metadata Preservation Tutorial | ||||
| description | Learn how to preserve target metadata during document comparison using GroupDocs.Comparison for .NET. Step-by-step guide with C# examples. | ||||
| keywords | preserve target metadata, GroupDocs.Comparison metadata preservation, .NET document comparison, metadata preservation tutorial | ||||
| weight | 1 | ||||
| url | /net/advanced-comparison/groupdocs-comparison-net-metadata-target/ | ||||
| date | 2026-03-06 | ||||
| lastmod | 2026-03-06 | ||||
| categories |
|
||||
| tags |
|
||||
| type | docs |
Ever compared two documents only to lose important metadata in the process? You're not alone. When you need to preserve target metadata while comparing documents in a .NET application, the task can feel tricky—but it doesn’t have to be.
GroupDocs.Comparison for .NET lets you decide which document’s metadata survives the comparison result. Whether you’re building a document‑management system, handling legal contracts, or managing collaborative content, you’ll want the metadata from the right source document every time.
In this tutorial you’ll learn how to preserve target metadata during comparison, avoid common pitfalls, and implement the solution in real‑world scenarios.
- What does “preserve target metadata” mean? It keeps the metadata (author, creation date, custom properties, etc.) from the document you designate as the target when generating the comparison result.
- Which GroupDocs.Comparison version is required? Version 25.4.0 or later.
- Can I use this with .NET Core? Yes – .NET Core 2.0+ or .NET Framework 4.6.1+.
- Is a license needed for production? A commercial license is required for production; a free trial works for learning.
- Will the feature work with PDF and DOCX? Yes – all major Office and PDF formats support metadata preservation.
Before jumping into code, let’s talk about why preserving target metadata matters. Document metadata isn’t just “nice to have”—it’s often legally required or business‑critical:
- Legal documents – need to retain attorney‑client privilege markers.
- Corporate files – must keep compliance tags and approval chains.
- Academic papers – author attribution and revision history are essential.
- Technical documentation – version control and review status matter.
Without proper handling, you might accidentally strip away information that took months to establish. That’s where the preserve target metadata option shines.
- GroupDocs.Comparison for .NET: Version 25.4.0 or later (earlier versions have limited metadata options).
- .NET Framework: 4.6.1 or higher, or .NET Core 2.0+.
- Visual Studio (or any C# IDE you prefer).
- Basic C# knowledge (nothing too advanced, promise!).
- Two sample documents for testing (Word .docx works great).
You don’t need to be a GroupDocs expert, but you should be comfortable with:
- C#
usingstatements and file handling. - Basic document‑processing concepts.
- What metadata actually is (author, title, custom properties, etc.).
Ready? Let’s set this up.
Getting GroupDocs.Comparison installed is straightforward, but there are a couple of gotchas to watch out for.
NuGet Package Manager Console (easiest method):
Install-Package GroupDocs.Comparison -Version 25.4.0.NET CLI (if you prefer command line):
dotnet add package GroupDocs.Comparison --version 25.4.0Pro tip: Always specify the version to avoid unexpected breaking changes in your project.
Here’s where many developers get stuck initially. GroupDocs.Comparison isn’t free, but you have options:
- Free Trial – full functionality for 30 days, perfect for evaluation.
- Temporary License – extended evaluation period if you need more time.
- Commercial License – for production use (various pricing tiers available).
Don’t worry about licensing right now if you’re just learning—the trial version includes all preserve target metadata features.
Let’s make sure everything’s working with a simple test:
using System.IO;
using GroupDocs.Comparison;
string sourceFilePath = "source.docx";
string targetFilePath = "target.docx";
// Initialize the Comparer object.
using (Comparer comparer = new Comparer(sourceFilePath))
{
// Add the target document for comparison.
comparer.Add(targetFilePath);
}If this compiles without errors, you’re good to go. If not, double‑check your package installation and using statements.
Now for the main event—actually preserving metadata during document comparison. This is where GroupDocs.Comparison really shines.
During a typical comparison:
- Source document provides the base content.
- Target document provides the changes to compare against.
- The output document combines both, but whose metadata wins?
By default, GroupDocs.Comparison uses the source document’s metadata. To preserve target metadata, you need to tell the API explicitly.
This establishes the “baseline” document—the one you’re comparing against:
using (Comparer comparer = new Comparer(sourceFilePath))
{
// All comparison operations happen within this scope
}Why use using statements? They automatically dispose of resources, preventing memory leaks when processing large documents. Trust me, you’ll thank yourself later when dealing with 50 MB Word files.
Tell the comparer which document contains the changes you want to analyze:
comparer.Add(targetFilePath);Common mistake: Confusing source and target. Think of it this way—source is your “original,” target is your “updated version.”
Specify which document’s metadata should be kept in the output:
comparer.Compare(outputFileName, new SaveOptions() { CloneMetadataType = MetadataType.Target });What’s happening? CloneMetadataType = MetadataType.Target tells GroupDocs.Comparison: “Hey, I want to keep the target document’s metadata in my final result.”
Here’s everything together in a runnable program:
using System;
using System.IO;
using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
class Program
{
static void Main(string[] args)
{
try
{
string sourceFile = "original_document.docx";
string targetFile = "updated_document.docx";
string outputFile = "comparison_result.docx";
using (Comparer comparer = new Comparer(sourceFile))
{
comparer.Add(targetFile);
// Preserve target document metadata
comparer.Compare(outputFile, new SaveOptions()
{
CloneMetadataType = MetadataType.Target
});
Console.WriteLine($"Comparison completed! Check {outputFile}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error during comparison: {ex.Message}");
}
}
}File Path Issues – always use full paths or ensure your files live in the working directory:
// Good
string sourceFile = Path.Combine(Directory.GetCurrentDirectory(), "docs", "source.docx");
// Risky (might work locally but fail in production)
string sourceFile = "source.docx";Memory Management – for large documents, always wrap Comparer objects in using statements.
Version Compatibility – different GroupDocs.Comparison releases expose different metadata options—stick with 25.4.0 or newer for best results.
| Scenario | Prefer Target Metadata | Prefer Source Metadata |
|---|---|---|
| Updated author info needed | ✅ | ❌ |
| Original document has legal precedence | ❌ | ✅ |
| Custom properties added only in the newer file | ✅ | ❌ |
| You want to keep the “master” document’s history | ❌ | ✅ |
You can compare against several targets while still preserving metadata from the first target you add:
using (Comparer comparer = new Comparer(sourceFilePath))
{
comparer.Add(targetFilePath1);
comparer.Add(targetFilePath2);
comparer.Add(targetFilePath3);
// Metadata will come from the first target document
comparer.Compare(outputFileName, new SaveOptions()
{
CloneMetadataType = MetadataType.Target
});
}Law firms often need to compare contract versions while preserving specific metadata markers:
// Preserve client metadata from updated contract
using (Comparer comparer = new Comparer("original_contract.docx"))
{
comparer.Add("client_revised_contract.docx");
comparer.Compare("final_contract_comparison.docx", new SaveOptions()
{
CloneMetadataType = MetadataType.Target // Keep client's metadata
});
}When multiple researchers collaborate, you want to preserve the most recent author information:
// Keep metadata from the researcher's latest submission
using (Comparer comparer = new Comparer("draft_paper.docx"))
{
comparer.Add("researcher_updates.docx");
comparer.Compare("paper_comparison.docx", new SaveOptions()
{
CloneMetadataType = MetadataType.Target // Preserve researcher metadata
});
}In regulated industries, maintaining compliance metadata is critical:
// Preserve compliance tags from updated policy document
using (Comparer comparer = new Comparer("old_policy.docx"))
{
comparer.Add("compliance_approved_policy.docx");
comparer.Compare("policy_comparison.docx", new SaveOptions()
{
CloneMetadataType = MetadataType.Target // Keep compliance metadata
});
}The most common issue. Debug with explicit checks:
string sourceFile = "source.docx";
// Always check if files exist before comparison
if (!File.Exists(sourceFile))
{
Console.WriteLine($"Source file not found: {Path.GetFullPath(sourceFile)}");
return;
}
// Same for target files
if (!File.Exists(targetFile))
{
Console.WriteLine($"Target file not found: {Path.GetFullPath(targetFile)}");
return;
}For documents over 10 MB, consider these optimizations:
// Use explicit disposal for large documents
using (var comparer = new Comparer(sourceFile))
{
comparer.Add(targetFile);
var saveOptions = new SaveOptions()
{
CloneMetadataType = MetadataType.Target
};
comparer.Compare(outputFile, saveOptions);
// Explicitly clean up
GC.Collect();
GC.WaitForPendingFinalizers();
}When working with protected files or network shares:
try
{
using (var comparer = new Comparer(sourceFile))
{
comparer.Add(targetFile);
comparer.Compare(outputFile, new SaveOptions()
{
CloneMetadataType = MetadataType.Target
});
}
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Access denied. Check file permissions.");
Console.WriteLine($"Details: {ex.Message}");
}
catch (IOException ex)
{
Console.WriteLine("File I/O error occurred.");
Console.WriteLine($"Details: {ex.Message}");
}GroupDocs.Comparison can be memory‑intensive. Use using statements to guarantee disposal:
// Good - automatic resource cleanup
using (var comparer = new Comparer(sourceFile))
{
// comparison logic here
}
// Bad - potential memory leaks
var comparer = new Comparer(sourceFile);
// ... comparison logic
// comparer.Dispose(); // Easy to forget!Process Documents in Batches – if you’re comparing many files, handle them in smaller groups to keep memory usage low.
For desktop or web apps, wrap comparison in an async method:
public async Task<bool> CompareDocumentsAsync(string source, string target, string output)
{
return await Task.Run(() =>
{
try
{
using (var comparer = new Comparer(source))
{
comparer.Add(target);
comparer.Compare(output, new SaveOptions()
{
CloneMetadataType = MetadataType.Target
});
return true;
}
}
catch
{
return false;
}
});
}- Small (< 1 MB) – process directly.
- Medium (1‑10 MB) – show progress to keep UI responsive.
- Large (> 10 MB) – always use async processing and consider explicit GC as shown above.
Below is a ready‑to‑use controller that accepts two uploaded files, runs the comparison, and returns the result while preserving target metadata:
[ApiController]
[Route("api/[controller]")]
public class DocumentComparisonController : ControllerBase
{
[HttpPost("compare-with-target-metadata")]
public async Task<IActionResult> CompareWithTargetMetadata(
IFormFile sourceFile,
IFormFile targetFile)
{
var tempSource = Path.GetTempFileName();
var tempTarget = Path.GetTempFileName();
var outputPath = Path.GetTempFileName();
try
{
// Save uploaded files temporarily
await sourceFile.CopyToAsync(new FileStream(tempSource, FileMode.Create));
await targetFile.CopyToAsync(new FileStream(tempTarget, FileMode.Create));
// Perform comparison with target metadata preservation
using (var comparer = new Comparer(tempSource))
{
comparer.Add(tempTarget);
comparer.Compare(outputPath, new SaveOptions()
{
CloneMetadataType = MetadataType.Target
});
}
// Return comparison result
var resultBytes = await System.IO.File.ReadAllBytesAsync(outputPath);
return File(resultBytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"comparison_result.docx");
}
finally
{
// Clean up temporary files
if (System.IO.File.Exists(tempSource)) System.IO.File.Delete(tempSource);
if (System.IO.File.Exists(tempTarget)) System.IO.File.Delete(tempTarget);
if (System.IO.File.Exists(outputPath)) System.IO.File.Delete(outputPath);
}
}
}Q: Can I preserve metadata from multiple target documents when comparing?
A: When you add several target files, GroupDocs.Comparison uses the metadata from the first target document added. Add the document whose metadata you want to keep first in the chain.
Q: What happens if the target document lacks some metadata fields?
A: Only the metadata that exists in the target will be copied to the output. Missing fields are simply omitted; the comparison still succeeds.
Q: How do I handle password‑protected documents?
A: Use a LoadOptions object with the password, then pass it to the Comparer constructor:
var loadOptions = new LoadOptions() { Password = "your_password" };
using (var comparer = new Comparer(sourceFile, loadOptions))
{
// comparison logic here
}Q: Is there a way to preserve only selected metadata properties?
A: The current API preserves all metadata from the chosen source (Target or Source). For granular control you’d need to extract the properties after comparison and re‑apply them manually.
Q: Which document formats support metadata preservation?
A: Most common business formats—DOCX, PDF, PPTX, XLSX, and many others—support metadata preservation. See the official docs for the full list.
Q: Where can I get help if I run into issues?
A: Visit the GroupDocs Support Forum for community assistance, or contact GroupDocs support directly if you have a commercial license.
- Official Documentation: GroupDocs.Comparison for .NET Docs
- API Reference: Complete API Reference
- Download Latest Version: GroupDocs Downloads
- Free Trial: Start Your Trial
- Purchase Options: Licensing and Pricing
Last Updated: 2026-03-06
Tested With: GroupDocs.Comparison 25.4.0 for .NET
Author: GroupDocs