/ Published in: C#
This source code uses public classes and interfaces exposed by GroupDocs.Metadata for .NET to clean metadata from the documents created by a particular author in some directory. Steps include:
1) Scan all documents from an author in a directory (input)
2) Clean metadata associated with the documents.
3) Save documents without metadata in a new directory (output)
1) Scan all documents from an author in a directory (input)
2) Clean metadata associated with the documents.
3) Save documents without metadata in a new directory (output)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using GroupDocs.Metadata.Formats; using GroupDocs.Metadata.Formats.Document; using GroupDocs.Metadata.Standards.Doc; using GroupDocs.Metadata.Tools; using System.IO; using GroupDocs.Metadata.MetadataProperties; namespace GroupDocs.Metadata.Examples.Utilities.CSharp { //ExStart:DocCleaner public class DocCleaner { // Absolute path to the GroupDocs.Metadata license file private const string LicensePath = @"GroupDocs.Metadata.lic"; // Absolute path to the documents directory public string DocumentsPath { get; set; } static DocCleaner() { /* set product license * uncomment following function if you have product license * */ //SetInternalLicense(); } public DocCleaner(string documentsPath) { // check if directory exists if (!Directory.Exists(Common.MapSourceFilePath( documentsPath))) { } this.DocumentsPath = documentsPath; } /// <summary> /// Applies the product license /// </summary> private static void SetInternalLicense() { license.SetLicense(LicensePath); } /// <summary> /// Takes author name and removes metadata in files created by specified author /// </summary> /// <param name="authorName">Author name</param> public void RemoveMetadataByAuthor(string authorName) { // Map directory in source folder string sourceDirectoryPath = Common.MapSourceFilePath(this.DocumentsPath); // get files presented in target directory string[] files = Directory.GetFiles(sourceDirectoryPath); foreach (string path in files) { // recognize format FormatBase format = FormatFactory.RecognizeFormat(path); // initialize DocFormat DocFormat docFormat = format as DocFormat; if (docFormat != null) { // get document properties DocMetadata properties = docFormat.DocumentProperties; // check if author is the same if (string.Equals(properties.Author, authorName, StringComparison.OrdinalIgnoreCase)) { // remove comments docFormat.ClearComments(); // find all custom keys foreach (KeyValuePair<string, PropertyValue> keyValuePair in properties) { if (!properties.IsBuiltIn(keyValuePair.Key)) { customKeys.Add(keyValuePair.Key); } } // and remove all of them foreach (string key in customKeys) { properties.Remove(key); } //====== yet to change things ========================= // and commit changes string fileName = Path.GetFileName(path); string outputFilePath = Common.MapDestinationFilePath(this.DocumentsPath + "/" + fileName); docFormat.Save(outputFilePath); //===================================================== } } } Console.WriteLine("Press any key to exit."); } } //ExEnd:DocCleaner }