Building a Footer from HTML- how to substitute in PAGE and NUMPAGES?

I am building a document from a variety of sources. The footer for each document will be drawn on some HTML data (that I do not control). In some cases, our HTML for the footer passes in the string:

PAGE [#] OF [#]

I have seen the tutorial on page numbering in the footer here: http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/create-headersfooters-using-documentbuilder.html. This doesn’t quite do what I want, but gets me close. What I want to be able to do is replace some given HTML in my source with the field values for PAGE and NUMPAGES. Is this possible to do?

Attaches are my HTML source sample and my C# code sample.

Hi

Thanks for your inquiry. I think, you can use ReplaceEvaluator in this case.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.range.replace_overload_3.html

For example, you have text “[PAGE]” in your document and you would like to replace this text with PAGE field. In this case, you can try using code like the following:

Document doc = new Document(@“Test001\in.doc”);

doc.Range.Replace(new Regex(@“[PAGE]”), new ReplaceEvaluator(ReplaceWithFieldEvaluator), true);

doc.Save(@“Test001\out.doc”);

==========================================================

///

/// NOTE: This is a simplistic method that will only work well when the match

/// starts at the beginning of a run.

///

private static ReplaceAction ReplaceWithFieldEvaluator(object sender, ReplaceEvaluatorArgs e)

{

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

builder.MoveTo(e.MatchNode);

// Replace ‘[PAGE]’ text with a PAGE field.

builder.InsertField(“PAGE”, null);

e.Replacement = “”;

return ReplaceAction.Replace;

}

Hope this helps.

Best regards.

This is very close to working for me. Based on your code, I put the following line in place:

_doc.Range.Replace(new Regex(@"[PAGECURRENT]"), new ReplaceEvaluator(ReplaceWithFieldEvaluator), true);

I used the ReplaceWithFieldEvaluator method exactly as you have it. My input contains the following string:

Page [PAGECURRENT] of [PAGETOTAL]

In my test, I am attempting to only (for now) replace the [PAGECURRENT] tag (as you see in my code). It dos put the proper page number in the page footer, however the formatting is wrong. This is the formatting I am getting from this code:

1Page of [PAGETOTAL]

I am expecting to get the formatting of:

Page 1 of [PAGETOTAL]

Can you help me explain and resolve my error? I would think I am simply making a minor error out of ignorance. Thank you for the help so far!

Hi

Thanks for your inquiry. This occurs, because the code, I provided earlier does not split the matched node. I think, you can find the solution of the problem here:

Best regards.