Additionally, if I need to customize the starting page number, how should this be specified in the HTML?
Cause
To achieve different footer page numbers for odd and even pages in a Word document generated from HTML using Aspose.Words, you need to configure the document’s section properties correctly. Additionally, you can specify a custom starting page number through the HTML.
Solution
-
Odd and Even Page Numbers: Utilize the
oddFooter
andevenFooter
properties in the section of your Word document to set distinct footers for odd and even pages. In HTML, these properties can be defined using specific styles. -
Custom Starting Page Number: To set a custom starting page number, employ the
startPageNumber
property in the section settings.
Code
Here’s an example of how to structure your HTML to achieve this:
<!DOCTYPE html>
<html>
<head>
<style>
@page {
odd-footer: "Page " [PAGE] " of " [NUMPAGES];
even-footer: "Page " [PAGE] " of " [NUMPAGES];
}
</style>
</head>
<body>
<div>
<h1>Your Document Title</h1>
<p>Your content goes here.</p>
</div>
</body>
</html>
In your Aspose.Words code, set the starting page number as follows:
Document doc = new Document("input.html");
Section section = doc.getFirstSection();
section.getPageSetup().setStartingPageNumber(5); // Set starting page number to 5
doc.save("output.docx");
Next Steps
- Create your HTML with the specified styles for odd and even footers.
- Use the provided Java code to set the starting page number.
- Convert the HTML to Word using Aspose.Words and verify the output.
If you have any further questions or need additional assistance, feel free to ask!
It doesn’t work,and the page numbers are not even displayed at all.
@pizhai2 The easiest way is to setup page numbering after importing HTML.
https://reference.aspose.com/words/java/com.aspose.words/pagesetup/#getOddAndEvenPagesHeaderFooter
Document doc = new Document("C:\\Temp\\in.html");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertField("PAGE");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.insertField("PAGE");
doc.save("C:\\Temp\\out.docx");
out.docx (10.3 KB)