HI,
i have a custom field (field_test for exemple ) in the footer of my page (word 2003) , how can i delete this field by code?
King regards
Hi
Thanks for your inquiry. Could you please attach your input document here for testing? I will check it on my side and provide you more information.
Best regards,
Thank you.
this is the test doc
King regards
Hi
Thank you for additional information. If you would like to remove this field by name you can try using the following code:
// Open document
Document doc = new Document("testdoc.docx");
// Get all FieldStart from the document.
Node[] fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true).ToArray();
// Loop through all FieldStart.
foreach(FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType == FieldType.FieldDocProperty)
{
string fieldCode = string.Empty;
Node currentNode = fieldStart;
// Get Field code
while (currentNode.NodeType != NodeType.FieldSeparator)
{
if (currentNode.NodeType == NodeType.Run)
fieldCode += (currentNode as Run).Text;
currentNode = currentNode.NextSibling;
}
currentNode = fieldStart;
// We should get Property name from field code
Regex regex = new Regex(@"\s*(?\S+)\s+(?\S+)\s*(?.*)");
Match match = regex.Match(fieldCode);
string propertyName = match.Groups["propname"].Value;
if (propertyName == "ImanageFooterVariable")
{
// Remove this field
while (currentNode.NodeType != NodeType.FieldEnd)
{
currentNode = currentNode.NextSibling;
currentNode.PreviousSibling.Remove();
}
currentNode.Remove();
}
}
}
// Save output document
doc.Save("out.docx");
Or you can just clear your footer:
doc.Sections[0].HeadersFooters.Clear();
Please let me know in case of any issues. I will be glad to help you.
Best regards,
thank you very very match