Table of contents
Overview
Managing document metadata across various file formats can be challenging. GroupDocs.Metadata for .NET simplifies this by providing a tagging system to unify common metadata properties (e.g. Title, Author, Created Date) under tags. In this how-to guide, we’ll explore how to find, add, copy, update, and remove metadata using tags with GroupDocs.Metadata for .NET.
GroupDocs.Metadata uses tags to label important metadata properties in a file, regardless of the document’s format or metadata standard. Each tag belongs to a category that groups related tags for easier discovery. For example: Content Tags: Describe the content of a file (e.g. language, genre, subject, rating) Person Tags: Identify people or organizations related to the content (e.g. document creator, editor, company) Time Tags: Represent timestamps in the file’s lifecycle (e.g. creation date, last modified time, print date) (Other categories include Legal, Origin, Tool, etc., each grouping relevant tags.) Using these tags, you can search and manipulate metadata in a unified way across Word documents, PDFs, images, and more. For instance, a “Author” or Creator tag in the Person category will match the author property in a Word document or a PDF without you needing to know the format-specific metadata field name. This makes tags extremely useful for organizing files and automating metadata management tasks.
Use Cases
In this section, we will introduce different use cases that highlight the practical applications.
Find Metadata Properties by Tag
One common use case is finding metadata entries in a file by their tag. This is particularly helpful for improving searchability – you can fetch specific info from any document without worrying about its format.
Steps
- Load the file into a
Metadata
object. - Find properties using
FindProperties()
with a tag-based predicate. You can filter by a specific tag or an entire category. - Iterate through results to inspect or use the metadata.
// Load the source document (e.g., a PowerPoint file)
using (Metadata metadata = new Metadata("source.pptx"))
{
// Find properties tagged as "last editor" or "last modified time"
var properties = metadata.FindProperties(p =>
p.Tags.Contains(Tags.Person.Editor) ||
p.Tags.Contains(Tags.Time.Modified));
// List the found properties and their values
foreach (var property in properties)
{
Console.WriteLine($"Property name: {property.Name}, Value: {property.Value}");
}
}
Adding Metadata Properties by Tag
Sometimes a file might be missing certain metadata (e.g. no “Last Printed Date” recorded). Using GroupDocs.Metadata, you can add a metadata property by specifying a tag, and the API will place it in the appropriate metadata package if the format supports it.
Steps
- Load the file into a
Metadata
object. - Call
AddProperties()
with a predicate to identify where to add, and provide the new property value. - Check the return value to see how many properties were added.
- Save the document (if you want to persist the changes).
For example, the code below adds a “Last Printed” date/time to a document if it doesn’t already have one:
using (Metadata metadata = new Metadata("source.pdf"))
{
if (metadata.FileFormat != FileFormat.Unknown && !metadata.GetDocumentInfo().IsEncrypted)
{
// Add a "Last Printed" date property if it's missing
int affected = metadata.AddProperties(
p => p.Tags.Contains(Tags.Time.Printed),
new PropertyValue(DateTime.Now));
Console.WriteLine("Properties added: " + affected);
metadata.Save("output.pdf");
}
}
Here we use Tags.Time.Printed
– the tag for “Printed” date under the Time category. The predicate p.Tags.Contains(Tags.Time.Printed)
targets the known Printed metadata property. If the property is missing, AddProperties
will add it with the value we provided (DateTime.Now in this case).
The method returns the count of properties added (affected), which we output. Finally, we save the file to persist the new metadata.
Copy Metadata Properties by Tag
Sometimes, you may want to copy only specific types of metadata, such as author names or creation dates. The CopyTo
method allows you to use a predicate to filter the properties to be copied. Here’s how to copy only metadata related to authors or creation dates:
Steps
- Load both the source and the target files using the
Metadata
class. - Use the
CopyTo
method to transfer metadata from source to target. - Save the target file to apply the copied metadata.
using (Metadata sourceMetadata = new Metadata("source.xlsx"))
using (Metadata targetMetadata = new Metadata("target.pptx"))
{
// Copy only Author (Creator) and Creation Date metadata properties
sourceMetadata.CopyTo(targetMetadata, p =>
p.Tags.Contains(Tags.Person.Creator) ||
p.Tags.Contains(Tags.Time.Created));
// Save the changes
targetMetadata.Save();
}
In this example, only metadata with tags for Creator (author name) and Created Date will be copied from the Excel file (source.xlsx) to the PowerPoint presentation (target.pptx). This selective copying gives you precise control over your metadata, making it easy to ensure your documents only contain exactly the metadata you want.
Updating Metadata Properties by Tag
Beyond adding new metadata, you often need to update existing metadata values (for instance, correcting an author name or standardizing dates). GroupDocs.Metadata provides the SetProperties()
method for this. It uses tags to find the target properties and updates their values. If a targeted property is missing, SetProperties can add it – effectively combining update and add in one step.
Steps
- Load the file into a
Metadata
object. - Call
SetProperties()
with a tag-based filter and the newPropertyValue
. - Inspect the returned count of affected properties (updated or added).
- Save the changes to file if needed.
For example, suppose we want to update the creation date and modified date of a document to the current date/time (this could be used to reset or standardize timestamps):
using (Metadata metadata = new Metadata("source.pdf"))
{
// Update all "Created" or "Modified" date properties to now
int affected = metadata.SetProperties(
p => p.Tags.Contains(Tags.Time.Created) || p.Tags.Contains(Tags.Time.Modified),
new PropertyValue(DateTime.Now));
Console.WriteLine("Properties updated or added: " + affected);
metadata.Save("updated.pdf");
}
In the predicate, Tags.Time.Created and Tags.Time.Modified are tags for the creation date and last modification date respectively. The code will find any metadata properties marked with either tag and set them to DateTime.Now. If, say, a Created date property didn’t exist in the file’s metadata, the library will add it (because it’s a known standard property for that format)
Removing Metadata by Tag
For privacy or compliance, you may need to remove certain metadata (e.g. author names or personal info) from documents. Tags make it easy to target and remove such metadata across different formats. The RemoveProperties()
method accepts a predicate like the others, and will strip out any matching metadata entries.
Steps
- Load the file into a
Metadata
object. - Call
RemoveProperties()
with a tag-based predicate identifying properties to remove. - Review the count of removed properties returned by the method.
- Save the file to apply changes.
using (Metadata metadata = new Metadata("source.docx"))
{
// Remove properties that are tagged as Creator or Editor, or any string containing "John"
int removedCount = metadata.RemoveProperties(p =>
p.Tags.Contains(Tags.Person.Creator) ||
p.Tags.Contains(Tags.Person.Editor) ||
(p.Value.Type == MetadataPropertyType.String && p.Value.ToString().Contains("John"))
);
Console.WriteLine("Properties removed: " + removedCount);
metadata.Save("cleaned.docx");
}
This predicate combines multiple criteria: any property tagged as Creator or Editor (both fall under the Person category) will be removed, and additionally any string metadata that contains “John” in its value will be removed. In a real scenario, you might use such logic to cleanse documents of personal data (like author names or client names).
Conclusion
In this article, we demonstrated how GroupDocs.Metadata for .NET’s tagging feature allows you to add, copy, find, update, and remove metadata in a unified way across many document types. By leveraging tags like Content, Person, Time, etc., you can build workflows that organize files by consistent metadata, improve searchability (find all files by author or date, for example), and automate document categorization or cleanup.
See Also
- GroupDocs.Metadata for .NET Documentation
- GroupDocs.Metadata for .NET Product Overview
- GroupDocs.Metadata for .NET GitHub Examples
- GroupDocs.Metadata for .NET Release Notes
Get a free trial
You can download a free trial version of GroupDocs.Metadata for .NET from releases.groupdocs.com. You can also acquire a temporary license to try all features and functionalities without restrictions from here.