Aspose.word replace text in headerFooter and body

Hello, i’m currently trying to do a replace operation inside a document in the header first/primary, footer first/primary and body, i’m proceeding as following :

public static void DoReplace(DocumentBuilder pbuilder)
{
    var listeVar = new List<string>("Header first !".Split('#'));
    var regex = new Regex(@"(?i)\@zvar.*?\@");
    // #1
    if (listeVar.Count > 0)
    {
        var section = pbuilder.Document.FirstSection;
        HeaderFooter headerFooter = section.HeadersFooters[HeaderFooterType.HeaderFirst];
        var findReplaceOptions = new FindReplaceOptions();
        findReplaceOptions.ReplacingCallback = new ZVarReplaceMatchesWithListContent(listeVar);
        headerFooter.Range.Replace(regex, "", findReplaceOptions);
    }
    // #2
    listeVar.Clear();
    listeVar.AddRange(new List<string>("Footer first !".Split('#')));
    if (listeVar.Count > 0)
    {
        var section = pbuilder.Document.FirstSection;
        HeaderFooter headerFooter = section.HeadersFooters[HeaderFooterType.FooterFirst];
        var findReplaceOptions = new FindReplaceOptions();
        findReplaceOptions.ReplacingCallback = new ZVarReplaceMatchesWithListContent(listeVar);
        headerFooter.Range.Replace(regex, "", findReplaceOptions);
    }
    //
    if (pbuilder.Document.PageCount > 1)
    {
        // #3
        if (pbuilder.Document.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
        {
            listeVar.Clear();
            listeVar.AddRange(new List<string>("Header Primary !".Split('#')));
            if (listeVar.Count > 0)
            {
            var section = pbuilder.Document.FirstSection;
            HeaderFooter headerFooter = section.HeadersFooters[HeaderFooterType.HeaderPrimary];
            var findReplaceOptions = new FindReplaceOptions();
            findReplaceOptions.ReplacingCallback = new ZVarReplaceMatchesWithListContent(listeVar);
            headerFooter.Range.Replace(regex, "", findReplaceOptions);
            }
        }
        // #4
        if (pbuilder.Document.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary] != null)
        {
            listeVar.Clear();
            listeVar.AddRange(new List<string>("Footer Primary !".Split('#')));
            if (listeVar.Count > 0)
            {
            var section = pbuilder.Document.FirstSection;
            HeaderFooter headerFooter = section.HeadersFooters[HeaderFooterType.FooterPrimary];
            var findReplaceOptions = new FindReplaceOptions();
            findReplaceOptions.ReplacingCallback = new ZVarReplaceMatchesWithListContent(listeVar);
            headerFooter.Range.Replace(regex, "", findReplaceOptions);
            }
        }
    }
    // #5
    listeVar.Clear();
    listeVar.AddRange(new List<string>("body 1#body 2#body 3".Split('#')));
    if (listeVar.Count > 0)
    {
        var findReplaceOptions = new FindReplaceOptions();
        findReplaceOptions.ReplacingCallback = new ZVarReplaceMatchesWithListContent(listeVar);
        foreach (Section section in pbuilder.Document.Sections)
        {
            section.Range.Replace(regex, "", findReplaceOptions);
        }
    }
} 

and here is the code of my replacingcallback :

public List<string> substituteValues; 

public ZVarReplaceMatchesWithListContent(List<string> pMmatchesValues) { substituteValues = pMmatchesValues; }

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
    e.Replacement = "";
    if (substituteValues != null && substituteValues.Count > 0)
    {
        e.Replacement = substituteValues[0];
        substituteValues.RemoveAt(0);
        //
        return ReplaceAction.Replace;
    }
    else
    {
        return ReplaceAction.Skip;
    }
}

When i execute this code on the .dotm file attached, i get the file res1.pdf where we can see that only operations #1 and #2 succeded.

However if i disable operations #3 and #4 then operation #5 succeed as well (see file res.pdf)

What troubles me the most is that when i debug my code, every replace operation seems to work (range.content changes as expected) but the final result doesn’t conform.

Can you help solve this problem ?

Sincerely.
docs.zip (470.2 KB)

@aaronArchest I tested your code, and it worked correctly. Please make sure to call the UpdatePageLayout() method before saving your document as a PDF.
output.zip (617.6 KB)

1 Like

thanks that was, indeed, all i needed !

1 Like