Create a table with 2 or more rows as a part of FOOTER in a Word Document

I want to get a complete tabular format in the footer section for the Word documnet.
I have worked upon it and almost reached the target, but facing some problems.
I am attaching the screenshot of the footer that I am getting, actually, am unable to align the cells in both the rows.

Following is my code :
public class AsposeWord_HeaderAndFooter {

private static final String dataDir = "E:\\";

public static void main(String[] args) throws Exception {
	//ExStart:CreateHeadersFootersUsingDocumentBuilder
	Document doc = new Document(dataDir+"CO-G-ITS-WI-17345.docx");
	DocumentBuilder builder = new DocumentBuilder(doc);

	Section currentSection = builder.getCurrentSection();
	PageSetup pageSetup = currentSection.getPageSetup();

	// Specify if we want headers/footers of the first page to be different from other pages.
	// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
	// different headers/footers for odd and even pages.
	pageSetup.setDifferentFirstPageHeaderFooter(false);

	// Set font properties for header text.
	builder.getFont().setName("Arial");
	builder.getFont().setBold(true);
	builder.getFont().setSize(14);
	
	// --- Create header for pages other than first. ---
	pageSetup.setHeaderDistance(20);
	builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
	builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	// Specify another header title for other pages.
	builder.write("Aspose.Words Header/Footer Creation Primer.");
	// --- Create footer for pages other than first. ---
	builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
	// We use table with two cells to make one part of the text on the line (with page numbering)
	// to be aligned left, and the other part of the text (with copyright) to be aligned right.
	builder.startTable();
	// Clear table borders
	//builder.getCellFormat().clearFormatting();
	builder.insertCell();
	// Set first cell to 1/3 of the page width.
	//builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));
	// Insert page numbering text here.
	// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
	builder.write("Title ");
	builder.insertCell();
	builder.write("Ref ");
	builder.insertCell();
	builder.write("Ver ");
	builder.insertCell();
	builder.write("Issue Date ");
	builder.insertCell();
	builder.write("Page ");
	builder.endRow();
	builder.endTable();
	/*
	builder.insertField("PAGE", "");
	builder.write(" of ");
	builder.insertField("NUMPAGES", "");*/

	// Align this text to the left.
	//builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

	/*builder.insertCell();
	// Set the second cell to 2/3 of the page width.
	builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 * 2 / 3));

	builder.write("(C) 2001 Aspose Pty Ltd. All rights reserved.");

	// Align this text to the right.
	builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);*/

	
	builder.startTable();
	
	builder.insertCell();
	builder.write("ABC ");
	builder.insertCell();
	builder.write("XYZ ");
	builder.insertCell();
	builder.write("1.2.3");
	builder.insertCell();
	builder.write("15-05-1998");
	builder.insertCell();
	builder.insertField("PAGE", "");
	builder.write(" of ");
	builder.insertField("NUMPAGES", "");
	builder.endRow();
	builder.endTable();
	
	
	builder.moveToDocumentEnd();
	// Make page break to create a second page on which the primary headers/footers will be seen.
	builder.insertBreak(BreakType.PAGE_BREAK);

	// Make section break to create a third page with different page orientation.
	builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

	// Get the new section and its page setup.
	currentSection = builder.getCurrentSection();
	pageSetup = currentSection.getPageSetup();

	// Set page orientation of the new section to landscape.
	pageSetup.setOrientation(Orientation.LANDSCAPE);

	// This section does not need different first page header/footer.
	// We need only one title page in the document and the header/footer for this page
	// has already been defined in the previous section
	pageSetup.setDifferentFirstPageHeaderFooter(false);

	// This section displays headers/footers from the previous section by default.
	// Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this.
	// Page width is different for the new section and therefore we need to set
	// a different cell widths for a footer table.
	currentSection.getHeadersFooters().linkToPrevious(false);

	// If we want to use the already existing header/footer set for this section
	// but with some minor modifications then it may be expedient to copy headers/footers
	// from the previous section and apply the necessary modifications where we want them.
	copyHeadersFootersFromPreviousSection(currentSection);

	// Find the footer that we want to change.
	HeaderFooter primaryFooter = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

	/*Row row = primaryFooter.getTables().get(0).getFirstRow();
	row.getFirstCell().getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));
	row.getLastCell().getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 * 2 / 3));

*/
// Save the resulting document.
doc.save(dataDir + “HeaderFooter.Word_Out.doc”);
//ExEnd:CreateHeadersFootersUsingDocumentBuilder
}

private static void copyHeadersFootersFromPreviousSection(Section section) throws Exception
{
    Section previousSection = (Section)section.getPreviousSibling();

    if (previousSection == null)
        return;

    section.getHeadersFooters().clear();

    for (HeaderFooter headerFooter : previousSection.getHeadersFooters())
        section.getHeadersFooters().add(headerFooter.deepClone(true));
}

}

Kindly help ! And, also if I could get a better code.
Thanks in advance ! :slight_smile:

Tablular Footer.PNG (4.0 KB)

@Kushal.20

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor, please find the info as under :
Input Doc : TestA3.docx
Output : HeaderFooter.Word_Out.doc
Expected O/P : Expected_FOOTER.pngPDF_FOOTER.PNG (5.9 KB)

@Kushal.20

Unfortunately, we have not found Word documents with your post. Please ZIP and attach them again. Thanks for your cooperation.

@tahir.manzoor okay.
I am attaching the only output file in which am facing the issue.
I have added the headers and the footers, but the table in the footer is having problem , its upper border is not visible and merging with the document body.wordFooter.PNG (9.7 KB)

@Kushal.20

Unfortunately, it is difficult to say what the problem is without input documents. Please ZIP and attach your input documents.

@tahir.manzoor
Okay am attaching the input document. Please find it as Word_Test.docx .Word_Test.zip (28.7 KB)

The output is same wordFooter.PNG (9.7 KB)

Please help ASAP. Thanks a lot for your continuous support !

@Kushal.20

You are creating two tables in your code. You should create one table with two rows. Moreover, please set the cell’s width according to your requirement. We suggest you please read the following article.
Specifying Table and Cell Widths

If you still want to create two tables, please set the cell’s width using CellFormat.Width property as shown below.

builder.startTable();
builder.getCellFormat().setWidth(50);
builder.insertCell();
builder.write("Title ");
builder.insertCell();
builder.write("Ref ");
builder.insertCell();
builder.write("Ver ");
builder.insertCell();
builder.write("Issue Date ");
builder.insertCell();
builder.write("Page ");
builder.endRow();
builder.endTable();


builder.startTable();

builder.insertCell();
builder.write("ABC ");
builder.insertCell();
builder.write("XYZ ");
builder.insertCell();
builder.write("1.2.3");
builder.insertCell();
builder.write("15-05-1998");
builder.insertCell();
builder.insertField("PAGE", "");
builder.write(" of ");
builder.insertField("NUMPAGES", "");
builder.endRow();
builder.endTable();

@tahir.manzoor, this is the code that I am using ,

private static void addTableFooterInDoc(String dir,String fileName) throws Exception{
System.out.println("************* Start **************");

	Document doc = new Document(dir + fileName);
	System.out.println("page count :: "+ doc.getPageCount());
	DocumentBuilder builder = new DocumentBuilder(doc);
	//builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
	builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);

	builder.getParagraphFormat().setSpaceBefore(0.0);
	builder.getParagraphFormat().setSpaceAfter(0.0);
	
	Table table = builder.startTable();
	
	String[] arrHeaders = new String [] {"Title","Document Ref","Version","Date of issue","Document Owner","Process Owner","Page"};
	String[] arrFooters = new String [] {"System Backup Instruction","CO-G-ITS-WI-17345","3.3","27/05/2019","Khare, Kushal (HQ)","Gupta, Sachin (HQ)","1 Of 1"};
	
	// Insert a cell
	System.out.println("1111111");
	for(int i=0;i<7;i++) {
		builder.insertCell();
		builder.setBold(true);
		builder.getFont().setName("Verdana");
		builder.getFont().setSize(8.5);
		builder.getFont().setColor(Color.BLACK);
		builder.write(arrHeaders[i]);
	}
	builder.endRow();
	
	System.out.println("2222222");
	for(int i=0;i<7;i++) {
		builder.insertCell();
		builder.setBold(false);
		builder.getFont().setName("Verdana");
		builder.getFont().setSize(7);
		builder.getFont().setColor(Color.BLUE);
		if(i==6) {
			builder.insertField("PAGE", "");
			builder.write(" of ");
			builder.insertField("NUMPAGES", "");
		}
		else
		builder.write(arrFooters[i]);
	}
	builder.endRow();
	builder.endTable();
	table.setPreferredWidth(PreferredWidth.fromPoints(580));
	table.setAlignment(TableAlignment.CENTER);
	
	doc.save(dir + "Out.docx");
	//doc.save(dir + "Out.pdf");
	System.out.println("************* End **************");
}

I am using a single table over here, still getting the above mentioned problem with the footer. The files that I am testing upon are same.
Kindly help me solve this, its been a long time am stuck at this.
Thankyou :hugs:

@Kushal.20

You are facing the expected behavior of Aspose.Words. If you perform the same scenario using MS Word, you will get the same output.

You set the page border in your input document. You can insert an empty paragraph before inserting the table in the footer of document to get the desired output.

builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);

builder.insertParagraph();
builder.getCurrentParagraph().getParagraphBreakFont().setSize(5);

@tahir.manzoor
So, you mean that it’s because of the page border that I have inserted ??
And, if this is the case, then I need to insert a paragraph before, always . Right ?

@Kushal.20

Yes, your understanding is correct. You may use PageSetup.FooterDistance property to achieve your requirement. This property sets the distance (in points) between the footer and the bottom of the page.

I already tried , but it does not proved to be helpful !
Anyways, thanks ! :slight_smile:

@Kushal.20

In your case, inserting empty paragraph before table in the footer of document will work. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Sure !
Thanks, @tahir.manzoor :slight_smile: