Clean metadata from documents created by a particular author


/ Published in: C#
Save to your folder(s)

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)


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using GroupDocs.Metadata.Formats;
  6. using GroupDocs.Metadata.Formats.Document;
  7. using GroupDocs.Metadata.Standards.Doc;
  8. using GroupDocs.Metadata.Tools;
  9. using System.IO;
  10. using GroupDocs.Metadata.MetadataProperties;
  11.  
  12. namespace GroupDocs.Metadata.Examples.Utilities.CSharp
  13. {
  14. //ExStart:DocCleaner
  15. public class DocCleaner
  16. {
  17. // Absolute path to the GroupDocs.Metadata license file
  18. private const string LicensePath = @"GroupDocs.Metadata.lic";
  19.  
  20. // Absolute path to the documents directory
  21. public string DocumentsPath { get; set; }
  22.  
  23. static DocCleaner()
  24. {
  25. /* set product license
  26.   * uncomment following function if you have product license
  27.   * */
  28. //SetInternalLicense();
  29. }
  30.  
  31. public DocCleaner(string documentsPath)
  32. {
  33. // check if directory exists
  34. if (!Directory.Exists(Common.MapSourceFilePath( documentsPath)))
  35. {
  36. throw new DirectoryNotFoundException("Directory not found: " + documentsPath);
  37. }
  38.  
  39. this.DocumentsPath = documentsPath;
  40. }
  41. /// <summary>
  42. /// Applies the product license
  43. /// </summary>
  44. private static void SetInternalLicense()
  45. {
  46. License license = new License();
  47. license.SetLicense(LicensePath);
  48. }
  49.  
  50. /// <summary>
  51. /// Takes author name and removes metadata in files created by specified author
  52. /// </summary>
  53. /// <param name="authorName">Author name</param>
  54. public void RemoveMetadataByAuthor(string authorName)
  55. {
  56. // Map directory in source folder
  57. string sourceDirectoryPath = Common.MapSourceFilePath(this.DocumentsPath);
  58.  
  59. // get files presented in target directory
  60. string[] files = Directory.GetFiles(sourceDirectoryPath);
  61.  
  62. foreach (string path in files)
  63. {
  64. // recognize format
  65. FormatBase format = FormatFactory.RecognizeFormat(path);
  66.  
  67. // initialize DocFormat
  68. DocFormat docFormat = format as DocFormat;
  69. if (docFormat != null)
  70. {
  71. // get document properties
  72. DocMetadata properties = docFormat.DocumentProperties;
  73.  
  74. // check if author is the same
  75. if (string.Equals(properties.Author, authorName, StringComparison.OrdinalIgnoreCase))
  76. {
  77. // remove comments
  78. docFormat.ClearComments();
  79.  
  80. List<string> customKeys = new List<string>();
  81.  
  82. // find all custom keys
  83. foreach (KeyValuePair<string, PropertyValue> keyValuePair in properties)
  84. {
  85. if (!properties.IsBuiltIn(keyValuePair.Key))
  86. {
  87. customKeys.Add(keyValuePair.Key);
  88. }
  89. }
  90.  
  91. // and remove all of them
  92. foreach (string key in customKeys)
  93. {
  94. properties.Remove(key);
  95. }
  96. //====== yet to change things =========================
  97. // and commit changes
  98. string fileName = Path.GetFileName(path);
  99. string outputFilePath = Common.MapDestinationFilePath(this.DocumentsPath + "/" + fileName);
  100. docFormat.Save(outputFilePath);
  101. //=====================================================
  102. }
  103. }
  104. }
  105.  
  106. Console.WriteLine("Press any key to exit.");
  107. }
  108. }
  109. //ExEnd:DocCleaner
  110. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.