How to set 'header and table body on the same page'wrod

I want to use Java to set ‘header and table body on the same page’, and I also need to add a solid line below the number of pages on each page
I am new to Aspose Word. Please provide code sample.

Thanks in advance

@Mikeykiss

To set a header and a table body on the same page using Aspose.Words for Java, you can follow the steps below. Additionally, I will show you how to add a solid line below the page number in the footer.

Here’s a sample code snippet that demonstrates how to achieve this:

import com.aspose.words.*;

public class HeaderAndTableExample {
    public static void main(String[] args) throws Exception {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Set up the header
        builder.MoveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
        builder.ParagraphFormat.Alignment = ParagraphAlignment.CENTER;
        builder.Font.Size = 14;
        builder.Font.Bold = true;
        builder.Write("This is the Header");

        // Move to the main document body
        builder.MoveToDocument();

        // Create a table
        Table table = builder.StartTable();
        builder.InsertCell();
        builder.Write("Cell 1");
        builder.InsertCell();
        builder.Write("Cell 2");
        builder.EndRow();
        builder.InsertCell();
        builder.Write("Cell 3");
        builder.InsertCell();
        builder.Write("Cell 4");
        builder.EndTable();

        // Move to the footer
        builder.MoveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
        builder.ParagraphFormat.Alignment = ParagraphAlignment.RIGHT;
        builder.Write("Page ");
        builder.InsertField("PAGE", "");
        builder.Write(" of ");
        builder.InsertField("NUMPAGES", "");

        // Add a solid line below the page number
        builder.InsertBreak(BreakType.LINE_BREAK);
        builder.InsertHorizontalRule();

        // Save the document
        doc.Save("HeaderAndTableExample.docx");
    }
}

Explanation:

  1. Creating the Document: A new document is created using the Document class.
  2. Setting the Header: The header is accessed and text is added with specific formatting.
  3. Creating a Table: A simple table is created with two rows and two cells.
  4. Setting the Footer: The footer is accessed, and page numbers are added using fields.
  5. Adding a Solid Line: A horizontal rule is inserted below the page number to create a solid line.
  6. Saving the Document: Finally, the document is saved to a file.

This code will ensure that the header and the table body appear on the same page, and it will also add a solid line below the page number in the footer.

For more detailed information on working with headers and footers in Aspose.Words for Java, you can refer to the official documentation here and here.

Feel free to ask if you have any further questions!

@Mikeykiss To keep table header row and the next row of the table together you can set ParagraphFormat.KeepWithNext and ParagraphFormat.KeepTogether for the paragraphs in the table header row.