Replacing Bookmarks with Fields in Footer

I am trying to update some of our legacy documents by removing a bookmark and the value it references and replacing the bookmark with the {NUMPAGES} field in the document footer. I have included a document with this issue below.

I have been unable to figure out how to insert a field into a specific place in an already existing Footer. Any help is greatly appreciated.

Footer Bookmark Test File.zip (9.2 KB)

@brandon.hart.idsdoc,

Please try using the following code. Output document is also attached (see 18.12.zip (9.1 KB)):

Document doc = new Document("E:\\Footer Bookmark Test File\\bookmarkTestDoc.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark("TOTPAGES", false, true);
builder.Font.Color = Color.Blue;
builder.InsertField(FieldType.FieldNumPages, true);

Bookmark bm = doc.Range.Bookmarks["TOTPAGES"];
bm.Text = string.Empty;
bm.Remove();

doc.Save("E:\\Footer Bookmark Test File\\18.12.docx");

Thank you for the reply!

I don’t see any changes in the 18.12.docx document. I ran the code you suggested on my file and nothing changed in the footer. However, when I just ran this section of your code:

Bookmark bm = doc.Range.Bookmarks["TOTPAGES"];
bm.Text = string.Empty;
bm.Remove();

The red {NUMPAGES} was removed instead of the TotPages bookmark. This ended up setting the bookmark to !Undefined Bookmark, TOTPAGES instead of removing it completely. I have attached a file showing this named RemoveBookmarkOnly.rtf

I also just ran the InsertField portion of your code:

builder.MoveToBookmark("TOTPAGES", false, true);
builder.Font.Color = Color.Blue;
builder.InsertField(FieldType.FieldNumPages, true);

and it ended up inserting another {NUMPAGES} to the right of the red {NUMPAGES} instead of next to the TotPages bookmark. I attached a file showing this named InsertFieldOnly.rtf

Insert Field and Remove Bookmark Docs.zip (18.7 KB)

@brandon.hart.idsdoc,

We consider this (bookmarkTestDoc.zip (9.1 KB)) as your input Word document.

Please also ZIP and attach your expected Word document (DOCX file) showing the correct final output here for our reference. Please create this expected document by using MS Word. We will then investigate the structure of your expected document as to how you want your final output be generated like and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Here is the expected result file.

Expected Result.zip (8.9 KB)

@brandon.hart.idsdoc,

We are working on your query and will get back to you soon.

@brandon.hart.idsdoc,

Please try using the following code to get the expected output:

Document doc = new Document("E:\\Footer Bookmark Test File\\bookmarkTestDoc.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);

string bmName = "TOTPAGES";
doc.Range.Bookmarks[bmName].Text = "";
doc.Range.Bookmarks[bmName].Remove();

doc.FirstSection.Body.RemoveAllChildren();

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldFormula)
    {
        FieldFormula formula = (FieldFormula)field;
        if (formula.GetFieldCode().ToLower().Contains(bmName.ToLower()))
        {
            builder.MoveTo(formula.Separator);
            builder.Write(" = ");
            BookmarkStart start = builder.StartBookmark(bmName);
            builder.InsertField(FieldType.FieldNumPages, false);
            BookmarkEnd end = builder.EndBookmark(bmName);
            builder.Write(" - 1 ");

            formula.Start.NextSibling.Remove();
            formula.Update();
        }
    }
}

doc.Save("E:\\Footer Bookmark Test File\\18.12.rtf"); 

Attachment: 18.12.zip (3.7 KB)

This is exactly what I needed. Thanks for the help!