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 !