I am trying to create a a paragraph with continuous text .I am not being able to add a footnote in between
in between a continuous paragraph, either it got added at the last or i need to create another paragraph for the next sentences.
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);
}}
In Word
creating a footnote needs two parts. First creating the footnote on document level. Second attaching the footnote reference to a text run.
The following approach provides a method BigInteger createFootnote(XWPFDocument document, String footnoteText)
for creating a footnote on document level. It returns the footnote identifier to be usable as footnote reference then. The attaching the footnote reference to an text run can be done then using run.getCTR().addNewFootnoteReference().setId(footnoteId);
where run
is a XWPFRun
.
If the need is formatting the footnote references, as superscript for example, then Word
itself creates a special character style for this. This also needs two parts. First create the style on document level:
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);
Then apply this style to the text runs:
run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");
For this, the footnote references must be in their own text runs.
Complete example:
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();
}
}
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.
Using apache poi 5.x
it must be
...
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
...