When using Aspose.Words to convert HTML to Word, how can I make the page numbers in the footer differ between odd and even pages, with odd-page numbers on the right and even-page numbers on the left? How should this be written in the HTML?

Additionally, if I need to customize the starting page number, how should this be specified in the HTML?

@pizhai2

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

  1. Odd and Even Page Numbers: Utilize the oddFooter and evenFooter 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.

  2. 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

  1. Create your HTML with the specified styles for odd and even footers.
  2. Use the provided Java code to set the starting page number.
  3. 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!

1 Like

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)

1 Like