private void BTN_Clean_Click(object sender, EventArgs e)
{
int cleanedCount = 0;
bool passwordFlg = false;
bool errorFlg = false;
string Message = "";
foreach (string ThisFile in InputFiles)
{
AsposeWords.FileFormatInfo info = AsposeWords.FileFormatUtil.DetectFileFormat(ThisFile);
bool WordAttachment = false;
switch (info.LoadFormat)
{
case AsposeWords.LoadFormat.Doc:
case AsposeWords.LoadFormat.Dot:
case AsposeWords.LoadFormat.Docx:
case AsposeWords.LoadFormat.Docm:
case AsposeWords.LoadFormat.Dotx:
case AsposeWords.LoadFormat.Dotm:
case AsposeWords.LoadFormat.FlatOpc:
case AsposeWords.LoadFormat.Rtf:
case AsposeWords.LoadFormat.WordML:
case AsposeWords.LoadFormat.Html:
case AsposeWords.LoadFormat.Mhtml:
case AsposeWords.LoadFormat.Odt:
case AsposeWords.LoadFormat.Ott:
case AsposeWords.LoadFormat.DocPreWord97:
WordAttachment = true;
break;
default:
WordAttachment = false;
break;
}
// If word Attachment is found
if (WordAttachment)
{
try
{
AsposeWords.Document doc = new AsposeWords.Document(ThisFile);
// Remove if there is any protection on the document
AsposeWords.ProtectionType protection = doc.ProtectionType;
if (protection != AsposeWords.ProtectionType.NoProtection)
doc.Unprotect();
// Remove all built-in and Custom Properties
doc.CustomDocumentProperties.Clear();
doc.BuiltInDocumentProperties.Clear();
// Password will be removed if the document is password protected.
if (protection != AsposeWords.ProtectionType.NoProtection)
doc.Protect(protection);
// Save the file back to temp location
doc.Save(ThisFile);
cleanedCount++;
}
catch (Words.IncorrectPasswordException)
{
passwordFlg = true;
Message = "Password protected files cannot be cleaned";
}
catch (Exception ex)
{
errorFlg = true;
Message = "Error: " + ex.Message;
}
}
else
Message = "Not a Word Document";
}
if (passwordFlg)
LBL_Error.Text = Message;
if (errorFlg)
LBL_Error.Text = Message;
BTN_Clean.Enabled = false;
LBL_Cleaned.Text = cleanedCount.ToString();
}