I am creating a table which is extended to next 4 pages in apache POI word and when the table is extended to the next page ,i need to insert the text at the end of first page and it needs to be happen only for the first page and rest page the text needs not to be inserted
private static void addFootnoteReference() throws IOException {
XWPFDocument document = new XWPFDocument();
document.createParagraph().createRun().setText("This is test");
// check to add footnotes in case of empty
if (document.getFootnotes().isEmpty()) {
document.createFootnotes();
}
// add footnote
CTFtnEdn ctfInstance = CTFtnEdn.Factory.newInstance();
BigInteger id = new BigInteger("0");
ctfInstance.setId(id);
CTP ctp = ctfInstance.addNewP();
ctp.addNewPPr().addNewPStyle().setVal("FootnoteText");
CTR ctr = ctp.addNewR();
ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference");
ctr.addNewFootnoteRef();
CTText cttext = ctp.addNewR().addNewT();
cttext.setStringValue("Council Regulation (EC) No 1224/2009 of 20 November 2009 establishing a Community control system for ensuring compliance with the rules of the common fisheries policy, amending Regulations (EC) No 847/96, (EC) No 2371/2002, (EC) No 811/2004, (EC) No 768/2005, (EC) No 2115/2005, (EC) No 2166/2005, (EC) No 388/2006, (EC) No 509/2007, (EC) No 676/2007, (EC) No 1098/2007, (EC) No 1300/2008, (EC) No 1342/2008 and repealing Regulations (EEC) No 2847/93, (EC) No 1627/94 and (EC) No 1966/2006 (OJ L 343, 22.12.2009, p. 1).");
cttext.setSpace(SpaceAttribute.Space.PRESERVE);
// add footnote to document
document.addFootnote(ctfInstance);
ctr = document.getParagraphArray(0).getCTP().addNewR();
ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference");
ctr.addNewFootnoteReference().setId(id);
// if styles dont already exist then create them
if (document.getStyles()==null){
document.createStyles();
}
CTStyle style = CTStyle.Factory.newInstance();
style.setType(STStyleType.PARAGRAPH);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(100));
style.setStyleId("FootnoteText");
style.addNewName().setVal("footnote text");
style.addNewBasedOn().setVal("Normal");
style.addNewLink().setVal("FootnoteTextChar");
style.addNewUiPriority().setVal(new BigInteger("99"));
style.addNewSemiHidden();
style.addNewUnhideWhenUsed();
CTRPr rpr = style.addNewRPr();
rpr.addNewSz().setVal(new BigInteger("20"));
rpr.addNewSzCs().setVal(new BigInteger("20"));
// add style
document.getStyles().addStyle(new XWPFStyle(style));
FileOutputStream out = new FileOutputStream("example.docx");
document.write(out);
out.close();
document.close();
}
What your screenshot shows is a footnote. See XWPFFootnote: "Create a new footnote using XWPFDocument.createFootnote()
or XWPFFootnotes.createFootnote()
." And: "To create a reference to a footnote within a paragraph you create a run with a CTFtnEdnRef
that specifies the ID of the target paragraph. The XWPFParagraph.addFootnoteReference(XWPFAbstractFootnoteEndnote)
method does this for you."
In Word
a footnote appears only on foot of that page which contains the paragraph which has the footnote reference applied. So if you wants to be the footnote on page 2, then you need applying the footnote reference to a paragraph on page 2. Behind that paragraph there will then be a number (the footnote reference) which references to the footnote which appears on the foot of the page.
Complete example for how this could look like in a table which spreads multiple pages:
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
public class CreateWordTableMultiplePagesFootnote {
static void setColumnWidth(XWPFTable table, int row, int col, int width) {
CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(width));
tblWidth.setType(STTblWidth.DXA);
CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setTcW(tblWidth);
} else {
tcPr = CTTcPr.Factory.newInstance();
tcPr.setTcW(tblWidth);
table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
}
}
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The table:");
// create footnote; will be referenced later
XWPFFootnote footnote = document.createFootnote(); // apache poi 4.1.1
paragraph = footnote.createParagraph();
run=paragraph.createRun();
run.setText("The content of the footnote... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor...");
//create table
//2 rows 3 columns
XWPFTable table = document.createTable(2, 3);
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
if (row < 1) { // header row
table.getRow(row).getCell(col).setColor("D9D9D9"); // header row color
table.getRow(row).setRepeatHeader(true); // header row shall repeat on new pages
}
}
}
//defining the column widths for the grid
//column width values are in unit twentieths of a point (1/1440 of an inch)
int defaultColWidth = 1*1440*6/3; // 3 columns fits to 6 inches
int[] colunmWidths = new int[] {
defaultColWidth, defaultColWidth, defaultColWidth
};
//create CTTblGrid for this table with widths of the columns.
//necessary for Libreoffice/Openoffice to accept the column widths.
//first column
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
setColumnWidth(table, 0, 0, colunmWidths[0]);
//other columns
for (int col = 1; col < colunmWidths.length; col++) {
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
setColumnWidth(table, 0, col, colunmWidths[0]);
}
//add more rows to fill the pages
for (int row = 2; row < 100; row++) {
XWPFTableRow tableRow = table.createRow();
for (int col = 0; col < 3; col++) {
tableRow.getCell(col).setText("row " + row + ", col " + col);
}
if (row == 35) { // 36th row should be in page 2
if (tableRow.getCell(1).getParagraphs().size() > 0 ) // get the paragraph of second cell
paragraph = tableRow.getCell(1).getParagraphs().get(0);
else paragraph = tableRow.getCell(1).addParagraph();
//set footnote reference
paragraph.addFootnoteReference(footnote); // apache poi 4.1.1
}
}
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("create_table.docx");
document.write(out);
out.close();
document.close();
}
}
Same example prepared to use with apache poi 3.14
:
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;
import org.apache.poi.xwpf.usermodel.XWPFFootnotes;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import java.math.BigInteger;
public class CreateWordTableMultiplePagesFootnote {
static void setColumnWidth(XWPFTable table, int row, int col, int width) {
CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(width));
tblWidth.setType(STTblWidth.DXA);
CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setTcW(tblWidth);
} else {
tcPr = CTTcPr.Factory.newInstance();
tcPr.setTcW(tblWidth);
table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
}
}
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The table:");
// create footnote; will be referenced later
XWPFFootnotes footnotes = document.createFootnotes();
CTFtnEdn ctFtnEdn = CTFtnEdn.Factory.newInstance();
BigInteger footnoteId = BigInteger.valueOf(footnotes.getFootnotesList().size());
ctFtnEdn.setId(footnoteId);
XWPFFootnote footnote = footnotes.addFootnote(ctFtnEdn);
paragraph = footnote.addNewParagraph(CTP.Factory.newInstance());
run=paragraph.createRun();
run.getCTR().addNewFootnoteRef();
run=paragraph.createRun();
run.setText("The content of the footnote... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor...");
//create table
//2 rows 3 columns
XWPFTable table = document.createTable(2, 3);
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
if (row < 1) { // header row
table.getRow(row).getCell(col).setColor("D9D9D9"); // header row color
table.getRow(row).setRepeatHeader(true); // header row shall repeat on new pages
}
}
}
//defining the column widths for the grid
//column width values are in unit twentieths of a point (1/1440 of an inch)
int defaultColWidth = 1*1440*6/3; // 3 columns fits to 6 inches
int[] colunmWidths = new int[] {
defaultColWidth, defaultColWidth, defaultColWidth
};
//create CTTblGrid for this table with widths of the columns.
//necessary for Libreoffice/Openoffice to accept the column widths.
//first column
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
setColumnWidth(table, 0, 0, colunmWidths[0]);
//other columns
for (int col = 1; col < colunmWidths.length; col++) {
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
setColumnWidth(table, 0, col, colunmWidths[0]);
}
//add more rows to fill the pages
for (int row = 2; row < 100; row++) {
XWPFTableRow tableRow = table.createRow();
for (int col = 0; col < 3; col++) {
tableRow.getCell(col).setText("row " + row + ", col " + col);
}
if (row == 35) { // 36th row should be in page 2
if (tableRow.getCell(1).getParagraphs().size() > 0 ) // get the paragraph of second cell
paragraph = tableRow.getCell(1).getParagraphs().get(0);
else paragraph = tableRow.getCell(1).addParagraph();
//set footnote reference
paragraph.createRun().getCTR().addNewFootnoteReference().setId(footnoteId);
}
}
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("create_table.docx");
document.write(out);
out.close();
document.close();
}
}
The code needs the full jar of all of the schemas ooxml-schemas or poi-ooxml-full as mentioned in https://poi.apache.org/help/faq.html#faq-N10025.