We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert page number after some text in footer aspose word java

Hi,

I want to insert page numbers in footer in same line with text, but when footer contains some text beside page number, page number and text in footer switch places. I am doing this while converting document from html to word.


Text in footer is sent from html and text doesn’t contains MERGEFORMAT tag.
This is the code for adding page number in footer:

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Elements elements = doc.select(ASPOSE_FOOTER_TAG);

if(elements.size() == 0 || !elements.html().contains(“MERGEFORMAT”)) {
log.debug(“Add page number”);
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert PAGE field into the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.insertField(“PAGE”, null);
builder.write("/");
builder.insertField(“NUMPAGES”, null);
}<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>

Is there any way to replace whole text in footer?

Hi Milan,


Thanks for your inquiry. You need to move cursor to the last child node inside header/footer and then insert fields. Please see the following code for example:
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>DocumentBuilder builder = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>DocumentBuilder(doc);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
HeaderFooter fp = doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
builder.moveTo(fp.getChildNodes(NodeType.ANY, true).get(fp.getChildNodes(NodeType.ANY, true).getCount() - 1));
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>builder.write(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“Hello”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);

In case the problem still remains and to ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Your HTML file or HTML string
  • Aspose.Words generated output DOCX file showing the undesired behavior (does footer with text and page number.docx show unexpected behavior?)
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word.
  • Please create a standalone Java application (source code without compilation errors) that helps us reproduce your specific problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,