In footer we want to align a Text at the left align and page number at center align. Attached the sample screenshot from MS Word.
Page number is always creating on its own line. Both text and pagenumber I could not able to make it in one line.
@lakshmiM To achieve this you can use table with one row and 3 cells. The first cell has left alignment, the second center alignment and the third is empty. Please see the following screenshot:
For demonstration purposes the table has borders. In your case you can disable borders. Please see our documentation to learn how to work with tables using Aspose.Words:
https://docs.aspose.com/words/net/create-a-table/
I understood that we can use tables and insert it as HTML. But the problem I am facing here is How to use builder.insertField(“PAGE”); along with the HTML. To generate the page numbers we have to use builder.insertField(“PAGE”). But with the div with custom css or table solution. How to generate page numbers ? Do we have any way to generate the page numbers using insertHtml instead of insertField ?
builder.insertHtml("<div style=“display: inline; white-space: nowrap; text-align: left;”>{ PAGE }");
@lakshmiM You can build whole table using DocumentBuilder instead of inserting HTML. But if you need to insert a PAGE field from HTML you can use the following syntax:
<span style="-aw-field-start:true"></span><span style="-aw-field-code:'PAGE'"></span><span style="-aw-field-separator:true"></span><span>1</span><span style="-aw-field-end:true"></span>
Hi, I dont understand exactly what is the above HTML.
Will the below HTML creates dynamic Page number ?
I tried it and it is not creating any dynamic page numbers. Can you please provider a small code snippet with Java
@lakshmiM Yes, this HTML will produce a dynamic page number in MS Word. Here is a simple Java code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("<span style=\"-aw-field-start:true\"></span><span style=\"-aw-field-code:'PAGE'\"></span><span style=\"-aw-field-separator:true\"></span><span>1</span><span style=\"-aw-field-end:true\"></span>");
doc.save("C:\\Temp\\out.docx");
out.docx (7.2 KB)
In the output document PAGE
field is inserted, you can see it by pressing Alt+F9
.
I tried with the above example, I see the page number is coming when docx. But the same HTML is not working when I try to save it as pdf.
wordDocument.save(docOutStream, SaveFormat.DOCX); --> Working
wordDocument.save(docOutStream, SaveFormat.PDF); --> Not Working.
Can you please provide similar example for PDF generation. We have to convert the document based on user preference either a word or pdf.
@lakshmiM The same code works for both DOCX and PDF:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Will show 1
builder.insertHtml("<span style=\"-aw-field-start:true\"></span><span style=\"-aw-field-code:'PAGE'\"></span><span style=\"-aw-field-separator:true\"></span><span>1</span><span style=\"-aw-field-end:true\"></span>");
builder.insertBreak(BreakType.PAGE_BREAK);
// Will show 2
builder.insertHtml("<span style=\"-aw-field-start:true\"></span><span style=\"-aw-field-code:'PAGE'\"></span><span style=\"-aw-field-separator:true\"></span><span>1</span><span style=\"-aw-field-end:true\"></span>");
doc.updateFields();
doc.save("C:\\Temp\\out.docx");
doc.save("C:\\Temp\\out.pdf");
I tried the above example. It is working fine with docx and not working when we save as pdf.
Here is the code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("<div style=\"color: red\">------------------------------------"
+ "<span style=\"-aw-field-start:true\"></span><span style=\"-aw-field-code:'PAGE'\"></span>"
+ "<span style=\"-aw-field-separator:true\"></span><span>1</span><span style=\"-aw-field-end:true\"></span>"
+ "================</div>");
doc.save("C:\\Users\\l7x4\\docs\\outword.docx");
doc.save("C:\\Users\\l7x4\\docs\\outpdf.pdf");
Also, this is the aspose version we are using
implementation(group: 'com.aspose', name:'aspose-words', version: '18.1')
Do we need any upgraded version ?
@lakshmiM You are using quite old version of Aspose.Words, which was released more than 5 years ago. I have used the latest 23.6 version for testing. So, please try updating to the latest version and let us know if the problem still persists on your side.