Hello there
I have a problem with a footer on the first page.
I need to remove a part of it (page/pageCount). My approach was to replace it using regex.
However while regex finds the part i want to replace, the node.Text is readonly and cannot be assigned.
This is how i iterate through the nodes
foreach (Node node in sec.HeadersFooters[HeaderFooterType.FooterFirst].ChildNodes)
Is there a way to remove this specific part of the footer without removing it all?
@ImTryingOkay Page and Page Count are represented by fields, not by simple text. You can see these fields by pressing Alt+F9
in MS Word. You can use code like the following to remove them, but Note the code is an example and I assumed that there is PAGE/NUMPAGES
in the first page footer:
Document doc = new Document(@"C:\Temp\in.docx");
// Get the footer.
HeaderFooter first = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterFirst];
// Page number and page count are represented using fields. it is required simply romve these fields.
// Also it is required to remove Run node between these fields, which represents slash.
foreach (Field field in first.Range.Fields)
{
if (field.Start.FieldType == FieldType.FieldPage)
{
// This will remove slash. An additional check can be added here.
field.End.NextSibling.Remove();
field.Remove();
}
if (field.Start.FieldType == FieldType.FieldNumPages)
field.Remove();
}
doc.Save(@"C:\Temp\out.docx");