Return to Snippet

Revision: 70679
at June 8, 2016 20:35 by muhammadsabir


Initial Code
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)))
            {
                throw new DirectoryNotFoundException("Directory not found: " + documentsPath);
            }

            this.DocumentsPath = documentsPath;
        }
        /// <summary>
        /// Applies the product license
        /// </summary>
        private static void SetInternalLicense()
        { 
            License license = new License();
            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();

                        List<string> customKeys = new List<string>();

                        // 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
}

Initial URL
https://github.com/groupdocs-metadata/GroupDocs.Metadata-for-.NET/blob/master/Examples/GroupDocs.Metadata.Examples.CSharp/Utilities/DocCleaner.cs

Initial Description
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)

Initial Title
Clean metadata from documents created by a particular author

Initial Tags
email, excel, api

Initial Language
C#