需要在文本之间添加脚注,并且下一个文本应该出现在 Apache poi 中的同一行而不是下一行

问题描述 投票:0回答:1

我正在尝试创建一个包含连续文本的段落。我无法在其间添加脚注 在连续段落之间,要么在最后添加,要么我需要为下一个句子创建另一个段落。

apache-poi apache-poi-4
1个回答
1
投票

Word
中创建脚注需要两部分。首先在文档级别创建脚注。第二步将脚注引用附加到文本运行中。

以下方法提供了一种在文档级别创建脚注的方法

BigInteger createFootnote(XWPFDocument document, String footnoteText)
。然后它返回可用作脚注引用的脚注标识符。然后可以使用
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);

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

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

 }
}

代码需要所有模式 ooxml-schemas 或 poi-ooxml-full 的完整 jar,如https://poi.apache.org/help/faq.html#faq-N10025中所述。


使用

apache poi 5.x
一定是

...
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
...
© www.soinside.com 2019 - 2024. All rights reserved.