0

我正在尝试创建一个带有连续文本的段落。我无法在连续段落之间添加脚注,要么它是在最后添加的,要么我需要为下一个句子创建另一个段落。 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);
    }}
4

1 回答 1

1

Word创建脚注时需要两个部分。首先在文档级别创建脚注。其次将脚注引用附加到文本运行。

以下方法提供了一种BigInteger createFootnote(XWPFDocument document, String footnoteText)在文档级别创建脚注的方法。然后它返回脚注标识符以用作脚注参考。然后可以使用run.getCTR().addNewFootnoteReference().setId(footnoteId);where runis a将脚注引用附加到文本运行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);

然后将此样式应用于文本运行:

run.getCTR().addNewRPr().addNewRStyle().setVal("FootnoteReference");

为此,脚注引用必须在它们自己的文本运行中。

完整示例:

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();

 }
}
于 2019-11-20T10:42:05.510 回答