我正在尝试创建一个带有连续文本的段落。我无法在连续段落之间添加脚注,要么它是在最后添加的,要么我需要为下一个句子创建另一个段落。
1. 下面是附加的解释代码片段:
public class WordBuilder1 {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The table:");
createFootNote(document);
createParagraph(document, "This is the second sentence", 12, ParagraphAlignment.LEFT, false, UnderlinePatterns.NONE);
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
CTPageSz pageSz = sectPr.addNewPgSz();
pageSz.setOrient(STPageOrientation.PORTRAIT);
pageSz.setW(BigInteger.valueOf(11900)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
pageSz.setH(BigInteger.valueOf(16840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"
FileOutputStream out = new FileOutputStream("C:\\Users\\mishrne\\example.docx");
document.write(out);
out.close();
}
private static void createFootNote(XWPFDocument document) {
XWPFRun run = document.createParagraph().createRun();
run.setText("This is a footnote first line");
run.setFontFamily(TIMES_NEW_ROMAN);
run.setFontSize(12);
if (document.getFootnotes().isEmpty()) {
document.createFootnotes();
}
prepareFootNotes(document, FOOTNOTE_TEXT);
// if styles dont already exist then create them
if (document.getStyles() == null) {
document.createStyles();
}
prepareFooterStyle(document);
}
//create paragraph
private static void createParagraph(XWPFDocument document, String text, int fontSize, ParagraphAlignment paragraphAlignment, boolean isBold, UnderlinePatterns single) {
XWPFParagraph paragraph = document.createParagraph();
if (paragraphAlignment != null) {
paragraph.setAlignment(paragraphAlignment);
}
XWPFRun run = paragraph.createRun();
run.setText(text);
run.setFontFamily("Times New Roman");
run.setFontSize(fontSize);
run.setUnderline(single);
run.setBold(isBold);
}
private static void prepareFootNotes(XWPFDocument document, String footnotesText) {
// add footnote
CTFtnEdn ctfInstance = CTFtnEdn.Factory.newInstance();
BigInteger id = new BigInteger("1");
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(footnotesText);
cttext.setSpace(SpaceAttribute.Space.PRESERVE);
// add footnote to document
document.addFootnote(ctfInstance);
ctr = document.getParagraphArray(1).getCTP().addNewR();
ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference");
ctr.addNewFootnoteReference().setId(id);
}
private static void prepareFooterStyle(XWPFDocument document) {
CTStyle style = CTStyle.Factory.newInstance();
style.setStyleId("FootnoteReference");
style.setType(STStyleType.CHARACTER);
style.addNewName().setVal("footnote reference");
style.addNewBasedOn().setVal("DefaultParagraphFont");
style.addNewUiPriority().setVal(new BigInteger("99"));
style.addNewSemiHidden();
style.addNewUnhideWhenUsed();
style.addNewRPr().addNewVertAlign().setVal(STVerticalAlignRun.SUPERSCRIPT);
// add style
document.getStyles().addStyle(new XWPFStyle(style));
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));
}
private static void setHeaderRowforSingleCell(XWPFTableCell cell, String text) {
XWPFParagraph tempParagraph = cell.getParagraphs().get(0);
tempParagraph.setIndentationLeft(100);
tempParagraph.setIndentationRight(100);
tempParagraph.setAlignment(ParagraphAlignment.RIGHT);
tempParagraph.setSpacingAfter(50);
XWPFRun tempRun = tempParagraph.createRun();
tempRun.setFontSize(10);
tempRun.setText(text);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}}