需要在页面末尾插入文本,但表格仍在下一页继续

时间:2019-11-07 16:35:17

标签: apache-poi

我正在创建一个用apache POI词扩展到下4页的表,当该表扩展到下一页时,我需要在第一页末尾插入文本,并且只需要在第一页和其余页的文本无需插入

enter image description here

 private static void addFootnoteReference() throws IOException {
        XWPFDocument document = new XWPFDocument();
    document.createParagraph().createRun().setText("This is test");
    // check to add footnotes in case of empty
    if (document.getFootnotes().isEmpty()) {
        document.createFootnotes();
    }
    // add footnote
    CTFtnEdn ctfInstance = CTFtnEdn.Factory.newInstance();
    BigInteger id = new BigInteger("0");
    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("Council Regulation (EC) No 1224/2009 of 20 November 2009 establishing a Community control system for ensuring compliance with the rules of the common fisheries policy, amending Regulations (EC) No 847/96, (EC) No 2371/2002, (EC) No 811/2004, (EC) No 768/2005, (EC) No 2115/2005, (EC) No 2166/2005, (EC) No 388/2006, (EC) No 509/2007, (EC) No 676/2007, (EC) No 1098/2007, (EC) No 1300/2008, (EC) No 1342/2008 and repealing Regulations (EEC) No 2847/93, (EC) No 1627/94 and (EC) No 1966/2006 (OJ L 343, 22.12.2009, p. 1).");
    cttext.setSpace(SpaceAttribute.Space.PRESERVE);
    // add footnote to document
    document.addFootnote(ctfInstance);
    ctr = document.getParagraphArray(0).getCTP().addNewR();
    ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference");
    ctr.addNewFootnoteReference().setId(id);

    // if styles dont already exist then create them
    if (document.getStyles()==null){
        document.createStyles();
    }

    CTStyle style = CTStyle.Factory.newInstance();
    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));

    FileOutputStream out = new FileOutputStream("example.docx");
    document.write(out);
    out.close();
    document.close();
    }

以上代码产生以下格式的输出: Extra 1 appearing on the top

1 个答案:

答案 0 :(得分:1)

您的屏幕快照显示的是脚注。请参见XWPFFootnote:“使用XWPFDocument.createFootnote()XWPFFootnotes.createFootnote()创建新的脚注。”并且:“要在段落中创建对脚注的引用,您需要创建一个运行CTFtnEdnRef的运行,该运行指定目标段落的ID。XWPFParagraph.addFootnoteReference(XWPFAbstractFootnoteEndnote)方法可以为您完成此操作。”

Word中,脚注仅出现在该页面的脚上,该页面包含已应用脚注引用的段落。因此,如果您想成为第2页的脚注,则需要在第2页的某个段落上应用脚注引用。在该段落的后面,将有一个数字(脚注引用),该数字引用出现在脚上的脚注。页面的

有关在散布多个页面的表中的外观的完整示例:

import java.io.File;
import java.io.FileOutputStream;

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

public class CreateWordTableMultiplePagesFootnote {

 static void setColumnWidth(XWPFTable table, int row, int col, int width) {
  CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
  tblWidth.setW(BigInteger.valueOf(width));
  tblWidth.setType(STTblWidth.DXA);
  CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
  if (tcPr != null) {
   tcPr.setTcW(tblWidth);
  } else {
   tcPr = CTTcPr.Factory.newInstance();
   tcPr.setTcW(tblWidth);
   table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
  }
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  // create footnote; will be referenced later
  XWPFFootnote footnote = document.createFootnote(); // apache poi 4.1.1
  paragraph = footnote.createParagraph();
  run=paragraph.createRun();  
  run.setText("The content of the footnote... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor...");

  //create table
  //2 rows 3 columns
  XWPFTable table = document.createTable(2, 3);

  for (int row = 0; row < 2; row++) {
   for (int col = 0; col < 3; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
    if (row < 1) { // header row
     table.getRow(row).getCell(col).setColor("D9D9D9"); // header row color
     table.getRow(row).setRepeatHeader(true); // header row shall repeat on new pages
    }
   }
  }

  //defining the column widths for the grid
  //column width values are in unit twentieths of a point (1/1440 of an inch)
  int defaultColWidth = 1*1440*6/3; // 3 columns fits to 6 inches 
  int[] colunmWidths = new int[] {
   defaultColWidth, defaultColWidth, defaultColWidth 
  };

  //create CTTblGrid for this table with widths of the columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
  setColumnWidth(table, 0, 0, colunmWidths[0]);
  //other columns
  for (int col = 1; col < colunmWidths.length; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
   setColumnWidth(table, 0, col, colunmWidths[0]);
  }

  //add more rows to fill the pages
  for (int row = 2; row < 100; row++) {
   XWPFTableRow tableRow = table.createRow();
   for (int col = 0; col < 3; col++) {
    tableRow.getCell(col).setText("row " + row + ", col " + col);
   }
   if (row == 35) { // 36th row should be in page 2
    if (tableRow.getCell(1).getParagraphs().size() > 0 ) // get the paragraph of second cell
     paragraph = tableRow.getCell(1).getParagraphs().get(0); 
    else paragraph = tableRow.getCell(1).addParagraph();
    //set footnote reference
    paragraph.addFootnoteReference(footnote); // apache poi 4.1.1
   }
  }

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("create_table.docx"); 
  document.write(out);
  out.close();
  document.close();

 }
}

准备与apache poi 3.14一起使用的相同示例:

import java.io.File;
import java.io.FileOutputStream;

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;
import org.apache.poi.xwpf.usermodel.XWPFFootnotes;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;

import java.math.BigInteger;

public class CreateWordTableMultiplePagesFootnote {

 static void setColumnWidth(XWPFTable table, int row, int col, int width) {
  CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
  tblWidth.setW(BigInteger.valueOf(width));
  tblWidth.setType(STTblWidth.DXA);
  CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
  if (tcPr != null) {
   tcPr.setTcW(tblWidth);
  } else {
   tcPr = CTTcPr.Factory.newInstance();
   tcPr.setTcW(tblWidth);
   table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
  }
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  // create footnote; will be referenced later
  XWPFFootnotes footnotes = document.createFootnotes();
  CTFtnEdn ctFtnEdn = CTFtnEdn.Factory.newInstance();
  BigInteger footnoteId = BigInteger.valueOf(footnotes.getFootnotesList().size());
  ctFtnEdn.setId(footnoteId);
  XWPFFootnote footnote = footnotes.addFootnote(ctFtnEdn);
  paragraph = footnote.addNewParagraph(CTP.Factory.newInstance());
  run=paragraph.createRun();
  run.getCTR().addNewFootnoteRef(); 
  run=paragraph.createRun();  
  run.setText("The content of the footnote... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor... Lorem ipsum semit dolor...");

  //create table
  //2 rows 3 columns
  XWPFTable table = document.createTable(2, 3);

  for (int row = 0; row < 2; row++) {
   for (int col = 0; col < 3; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
    if (row < 1) { // header row
     table.getRow(row).getCell(col).setColor("D9D9D9"); // header row color
     table.getRow(row).setRepeatHeader(true); // header row shall repeat on new pages
    }
   }
  }

  //defining the column widths for the grid
  //column width values are in unit twentieths of a point (1/1440 of an inch)
  int defaultColWidth = 1*1440*6/3; // 3 columns fits to 6 inches 
  int[] colunmWidths = new int[] {
   defaultColWidth, defaultColWidth, defaultColWidth 
  };

  //create CTTblGrid for this table with widths of the columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
  setColumnWidth(table, 0, 0, colunmWidths[0]);
  //other columns
  for (int col = 1; col < colunmWidths.length; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
   setColumnWidth(table, 0, col, colunmWidths[0]);
  }

  //add more rows to fill the pages
  for (int row = 2; row < 100; row++) {
   XWPFTableRow tableRow = table.createRow();
   for (int col = 0; col < 3; col++) {
    tableRow.getCell(col).setText("row " + row + ", col " + col);
   }
   if (row == 35) { // 36th row should be in page 2
    if (tableRow.getCell(1).getParagraphs().size() > 0 ) // get the paragraph of second cell
     paragraph = tableRow.getCell(1).getParagraphs().get(0); 
    else paragraph = tableRow.getCell(1).addParagraph();
    //set footnote reference
    paragraph.createRun().getCTR().addNewFootnoteReference().setId(footnoteId);
   }
  }

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("create_table.docx"); 
  document.write(out);
  out.close();
  document.close();

 }
}