需要在文本之间添加脚注,下一个文本应该出现在同一行而不是 Apache poi 的下一行
Need to add a footnote in between texts and the next text should be appeared in the same line not in the next line in Apache poi
我正在尝试创建一个包含连续文本的段落。我无法在其间添加脚注
在一个连续的段落之间,它要么在最后添加,要么我需要为接下来的句子创建另一个段落。
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);
}}
在 Word
中创建脚注需要两个部分。首先在文档级别创建脚注。其次将脚注引用附加到文本 运行.
以下方法提供了一种在文档级别创建脚注的方法BigInteger createFootnote(XWPFDocument document, String footnoteText)
。它 returns 脚注标识符然后可用作脚注参考。将脚注引用附加到文本 运行 可以使用 run.getCTR().addNewFootnoteReference().setId(footnoteId);
完成,其中 run
是 XWPFRun
.
如果需要格式化脚注引用,例如上标,那么 Word
本身会为此创建一个特殊的字符样式。这也需要两部分。首先在文档级别创建样式:
XWPFStyles styles = document.createStyles();
XWPFStyle style = new XWPFStyle(CTStyle.Factory.newInstance(), styles);
style.getCTStyle().setType(STStyleType.CHARACTER);
style.getCTStyle().setStyleId("FootnoteReference");
style.getCTStyle().addNewRPr().addNewVertAlign().setVal(STVerticalAlignRun.SUPERSCRIPT);
styles.addStyle(style);
然后将此样式应用于文本 运行s:
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
为此,脚注引用必须在它们自己的文本中 运行s。
完整示例:
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
public class CreateWordFootnotes {
static BigInteger createFootnote(XWPFDocument document, String footnoteText) {
XWPFFootnotes footnotes = document.createFootnotes();
CTFtnEdn ctFtnEdn = CTFtnEdn.Factory.newInstance();
BigInteger footnoteId = BigInteger.valueOf(footnotes.getFootnotesList().size());
ctFtnEdn.setId(footnoteId);
XWPFFootnote footnote = footnotes.addFootnote(ctFtnEdn);
XWPFParagraph paragraph = footnote.addNewParagraph(CTP.Factory.newInstance());
XWPFRun run=paragraph.createRun();
run.getCTR().addNewFootnoteRef();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run = paragraph.createRun();
run.setText(footnoteText);
return footnoteId;
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFStyles styles = document.createStyles();
XWPFStyle style = new XWPFStyle(CTStyle.Factory.newInstance(), styles);
style.getCTStyle().setType(STStyleType.CHARACTER);
style.getCTStyle().setStyleId("FootnoteReference");
style.getCTStyle().addNewRPr().addNewVertAlign().setVal(STVerticalAlignRun.SUPERSCRIPT);
styles.addStyle(style);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The text");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("This is the text run having the first footnote");
String footnoteText = "The content of the first footnote.";
BigInteger footnoteId = createFootnote(document, footnoteText);
run = paragraph.createRun();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run.getCTR().addNewFootnoteReference().setId(footnoteId);
run = paragraph.createRun();
run.setText(" further text comes here and a second footnote");
footnoteText = "The content of the second footnote.";
footnoteId = createFootnote(document, footnoteText);
run = paragraph.createRun();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run.getCTR().addNewFootnoteReference().setId(footnoteId);
run = paragraph.createRun();
run.setText(" and now the paragraph ends here.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordFootnotes.docx");
document.write(out);
out.close();
document.close();
}
}
代码需要 https://poi.apache.org/help/faq.html#faq-N10025.
中提到的所有模式 ooxml-schemas 或 poi-ooxml-full 的完整 jar
使用apache poi 5.x
必须是
...
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
...
我正在尝试创建一个包含连续文本的段落。我无法在其间添加脚注
在一个连续的段落之间,它要么在最后添加,要么我需要为接下来的句子创建另一个段落。
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);
}}
在 Word
中创建脚注需要两个部分。首先在文档级别创建脚注。其次将脚注引用附加到文本 运行.
以下方法提供了一种在文档级别创建脚注的方法BigInteger createFootnote(XWPFDocument document, String footnoteText)
。它 returns 脚注标识符然后可用作脚注参考。将脚注引用附加到文本 运行 可以使用 run.getCTR().addNewFootnoteReference().setId(footnoteId);
完成,其中 run
是 XWPFRun
.
如果需要格式化脚注引用,例如上标,那么 Word
本身会为此创建一个特殊的字符样式。这也需要两部分。首先在文档级别创建样式:
XWPFStyles styles = document.createStyles();
XWPFStyle style = new XWPFStyle(CTStyle.Factory.newInstance(), styles);
style.getCTStyle().setType(STStyleType.CHARACTER);
style.getCTStyle().setStyleId("FootnoteReference");
style.getCTStyle().addNewRPr().addNewVertAlign().setVal(STVerticalAlignRun.SUPERSCRIPT);
styles.addStyle(style);
然后将此样式应用于文本 运行s:
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
为此,脚注引用必须在它们自己的文本中 运行s。
完整示例:
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
public class CreateWordFootnotes {
static BigInteger createFootnote(XWPFDocument document, String footnoteText) {
XWPFFootnotes footnotes = document.createFootnotes();
CTFtnEdn ctFtnEdn = CTFtnEdn.Factory.newInstance();
BigInteger footnoteId = BigInteger.valueOf(footnotes.getFootnotesList().size());
ctFtnEdn.setId(footnoteId);
XWPFFootnote footnote = footnotes.addFootnote(ctFtnEdn);
XWPFParagraph paragraph = footnote.addNewParagraph(CTP.Factory.newInstance());
XWPFRun run=paragraph.createRun();
run.getCTR().addNewFootnoteRef();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run = paragraph.createRun();
run.setText(footnoteText);
return footnoteId;
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFStyles styles = document.createStyles();
XWPFStyle style = new XWPFStyle(CTStyle.Factory.newInstance(), styles);
style.getCTStyle().setType(STStyleType.CHARACTER);
style.getCTStyle().setStyleId("FootnoteReference");
style.getCTStyle().addNewRPr().addNewVertAlign().setVal(STVerticalAlignRun.SUPERSCRIPT);
styles.addStyle(style);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The text");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("This is the text run having the first footnote");
String footnoteText = "The content of the first footnote.";
BigInteger footnoteId = createFootnote(document, footnoteText);
run = paragraph.createRun();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run.getCTR().addNewFootnoteReference().setId(footnoteId);
run = paragraph.createRun();
run.setText(" further text comes here and a second footnote");
footnoteText = "The content of the second footnote.";
footnoteId = createFootnote(document, footnoteText);
run = paragraph.createRun();
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
run.getCTR().addNewFootnoteReference().setId(footnoteId);
run = paragraph.createRun();
run.setText(" and now the paragraph ends here.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordFootnotes.docx");
document.write(out);
out.close();
document.close();
}
}
代码需要 https://poi.apache.org/help/faq.html#faq-N10025.
中提到的所有模式 ooxml-schemas 或 poi-ooxml-full 的完整 jar使用apache poi 5.x
必须是
...
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
...