您的屏幕快照显示的是脚注。请参见XWPFFootnote:“使用XWPFDocument.createFootnote()
或XWPFFootnotes.createFootnote()
创建新的脚注。”并且:“要在段落中创建对脚注的引用,您需要创建一个运行CTFtnEdnRef
的运行,该运行指定目标段落的ID。XWPFParagraph.addFootnoteReference(XWPFAbstractFootnoteEndnote)
方法可以为您完成此操作。”
在Word
中,脚注仅出现在该页面的脚上,该页面包含已应用脚注引用的段落。因此,如果您想成为第2页的脚注,则需要在第2页的某个段落上应用脚注引用。在该段落的后面,将有一个数字(脚注引用),该数字引用出现在脚上的脚注。页面的
。
有关在散布多个页面的表中的外观的完整示例:
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,colunmWidths[0]);
//other columns
for (int col = 1; col < colunmWidths.length; col++) {
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
setColumnWidth(table,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();
}
}
准备与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 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,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();
}
}
本文链接:https://www.f2er.com/3143067.html