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

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

我正在创建一个在 apache POI word 中扩展到下 4 页的表格,当表格扩展到下一页时,我需要在第一页的末尾插入文本,并且只需要在第一页发生页和其余页不需要插入文字

apache-poi
1个回答
1
投票

您的屏幕截图显示的是脚注。请参阅 XWPFFootnote:“使用

XWPFDocument.createFootnote()
XWPFFootnotes.createFootnote()
创建新脚注。”并且:“要在段落中创建对脚注的引用,您可以使用指定目标段落 ID 的
CTFtnEdnRef
创建一个连续段。
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();

 }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.