| categories |
|
||||
|---|---|---|---|---|---|
| date | 2026-03-06 | ||||
| description | เรียนรู้วิธีการรักษาเมตาดาต้าของเป้าหมายระหว่างการเปรียบเทียบเอกสารโดยใช้ GroupDocs.Comparison สำหรับ .NET คู่มือขั้นตอนโดยละเอียดพร้อมตัวอย่าง C# | ||||
| keywords | preserve target metadata, GroupDocs.Comparison metadata preservation, .NET document comparison, metadata preservation tutorial | ||||
| lastmod | 2026-03-06 | ||||
| linktitle | Metadata Preservation Tutorial | ||||
| tags |
|
||||
| title | รักษาเมตาดาต้าเป้าหมายด้วย GroupDocs.Comparison – บทแนะนำ .NET | ||||
| type | docs | ||||
| url | /th/net/advanced-comparison/groupdocs-comparison-net-metadata-target/ | ||||
| weight | 1 |
เคยเปรียบเทียบเอกสารสองไฟล์แล้วทำให้เมตาดาต้าที่สำคัญหายไปหรือไม่? คุณไม่ได้เป็นคนเดียว เมื่อคุณต้อง preserve target metadata ขณะเปรียบเทียบเอกสารในแอปพลิเคชัน .NET งานนี้อาจดูซับซ้อน—แต่จริง ๆ แล้วไม่จำเป็นต้องเป็นเช่นนั้น
GroupDocs.Comparison for .NET ให้คุณเลือกว่าเมตาดาต้าของเอกสารใดจะคงอยู่ในผลลัพธ์การเปรียบเทียบ ไม่ว่าคุณจะกำลังสร้างระบบจัดการเอกสาร, จัดการสัญญากฎหมาย, หรือจัดการเนื้อหาที่ทำงานร่วมกัน คุณก็ต้องการเมตาดาต้าจากแหล่งเอกสารที่ถูกต้องทุกครั้ง
ในบทเรียนนี้คุณจะได้เรียนรู้วิธี preserve target metadata ระหว่างการเปรียบเทียบ, หลีกเลี่ยงข้อผิดพลาดทั่วไป, และนำไปใช้ในสถานการณ์จริง
- 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.
ก่อนจะลงมือเขียนโค้ด เรามาพูดถึงเหตุผลที่การ preserve target metadata มีความสำคัญ เมตาดาต้าเอกสารไม่ได้เป็นแค่ “ของดีที่มี” เท่านั้น—บ่อยครั้งมันเป็นข้อกำหนดทางกฎหมายหรือเป็นสิ่งที่ธุรกิจต้องพึ่งพา:
- 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.
หากไม่ได้จัดการอย่างเหมาะสม คุณอาจทำให้ข้อมูลที่ใช้เดือน ๆ เพื่อสร้างหายไปโดยบังเอิญ นั่นคือจุดที่ตัวเลือก preserve target metadata มีประโยชน์
- 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.
| สถานการณ์ | ต้องการเมตาดาต้า Target | ต้องการเมตาดาต้า Source |
|---|---|---|
| ต้องการข้อมูลผู้เขียนที่อัปเดต | ✅ | ❌ |
| เอกสารต้นฉบับมีอำนาจตามกฎหมาย | ❌ | ✅ |
| มีคุณสมบัติเฉพาะที่เพิ่มในไฟล์ใหม่เท่านั้น | ✅ | ❌ |
| ต้องการเก็บประวัติ “master” ของเอกสาร | ❌ | ✅ |
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