How to edit an existing document header string

How to edit an existing document header string,For example, my header is:“old text 123” and would like to edit “new text 345”

Hi,

Thanks for your inquiry. Please use the following code snippet to change the text of Header/Footer of document. Please read the following documentation links for your kind reference.

https://reference.aspose.com/words/net/aspose.words/section/headersfooters/
https://reference.aspose.com/words/net/aspose.words/headerfooter/

Document doc = new Document(MyDir + "in.docx");
foreach (Section section in doc.Sections)
{
    foreach (HeaderFooter hf in section.HeadersFooters)
    {
        foreach (Run run in hf.GetChildNodes(NodeType.Run, true))
        {
            run.Text = run.Text + "_Changed";
        }
    }
}
doc.Save(MyDir + "AsposeOut.docx");

Hope this helps you. Please let us know if you have any more queries.

If the header that link to the previous page, Can’t get the header,section.HeadersFooters.count=0,how can also do?

Hi,

Thanks for your inquiry. The section’s header/fooder is LinkToPrevious (true) that is the reason you are getting HeadersFooters count = 0. You need to update the parent Header/Footer in your document, the linked Header/Footer will be updated automatically.

You might find the following code helps to easily retrieve the content from any linked headers or footers. Simply call the method with your document object before doing your retrieval then you can retrieve the content from the linked header or footer just like you are trying to do now.

The code works by copying linked header footers from previous sections to the actual section.

Document doc = new Document(MyDir + "in.docx");
CopyLinkedHeadersFooters(doc);
foreach (Section section in doc.Sections)
{
    foreach (HeaderFooter hf in section.HeadersFooters)
    {
        foreach (Run run in hf.GetChildNodes(NodeType.Run, true))
        {
            run.Text = run.Text + "_Changed";
        }
    }
}
doc.Save(MyDir + "AsposeOut.docx");
private static void CopyLinkedHeadersFooters(Document doc)
{
    foreach (Section section in doc.Sections)
    {
        if (section == doc.FirstSection) continue;
        Section previousSection = (Section)section.PreviousSibling;
        HeaderFooterCollection previousHeaderFooters = previousSection.HeadersFooters;
        foreach (HeaderFooter headerFooter in previousHeaderFooters)
        {
            if (section.HeadersFooters[headerFooter.HeaderFooterType] != null) continue;
            if (
            (headerFooter.HeaderFooterType == HeaderFooterType.HeaderFirst
            || headerFooter.HeaderFooterType == HeaderFooterType.FooterFirst)
            &&
            (previousSection.PageSetup.DifferentFirstPageHeaderFooter != section.PageSetup.DifferentFirstPageHeaderFooter)
            ) continue;
            HeaderFooter newHeaderFooter = (HeaderFooter)previousHeaderFooters[headerFooter.HeaderFooterType].Clone(true);
            section.HeadersFooters.Add(newHeaderFooter);
        }
    }
}

Please let me know if this helps.