I am trying to delete/remove custom properties from a word document, the document is corrupted. This is my code so far. I compiles correctly, but does not open.
public static MemoryStream DeleteCustomPropertiers(Stream fileStream)
{
var msStream = new MemoryStream();
fileStream.CopyTo(msStream);
using (var document = WordprocessingDocument.Open(msStream, true))
{
var properties = new Dictionary<string, string>();
if (document != null)
{
var customProps = document.CustomFilePropertiesPart;
if (customProps != null)
{
if (customProps.Properties != null)
{
var props = from n in customProps.Properties.Elements<CustomDocumentProperty>()
where n.Name == "Props1" || n.Name == "Props2"
select n;
for (int i = props.Count() - 1; i >= 0; i--)
{
var prop = props.ToList()[i];
prop.Remove();
}
customProps.Properties.Save();
}
}
}
msStream.Position = 0;
document.MainDocumentPart.Document.Save();
document.Dispose();
return msStream;
}
}
OK, I disposed the document, but I am still getting the error:
Word found unreadable content in ''. Do you want to recover the contents of this docuemtn? if you trust the source of this document, click yes