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
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
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");
}
}
Document
class.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!