当前位置: 首页 > 知识库问答 >
问题:

如何将表格添加到页眉或页脚?

淳于亦
2023-03-14
  XWPFDocument docx = (XWPFDocument) dockingObject;

  CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy policy = new WPFHeaderFooterPolicy(docx, sectPr);
  XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  // somthing like: header.createTable();

亲切的问候...

~丹尼尔

共有1个答案

司空凌
2023-03-14

这可以使用公共的XWPFTable insertNewTbl(org.apache.xmlbeans.xmlCursor游标)。

方法:在标题中创建一个新段落。然后从该段的org.openxmlformats.schemas.wordprocessingml.x2006.main.ctp获取org.apache.xmlbeans.xmlcursor。然后插入该光标定位的表。

示例:

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

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

import org.apache.xmlbeans.XmlCursor;
import java.math.BigInteger;

public class CreateWordHeaderFooterTable {

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

  XWPFDocument doc= new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The Body:");

  paragraph = doc.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum....");

  // create header-footer
  CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);

  // create header start
  XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = header.getParagraphArray(0);
  if (paragraph == null) paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);

  run = paragraph.createRun();  
  run.setText("The Header:");

  // create table in header
  paragraph = header.createParagraph();
  XmlCursor cursor = paragraph.getCTP().newCursor();
  XWPFTable table = header.insertNewTbl(cursor);
  XWPFTableRow row = table.getRow(0); if (row == null) row = table.createRow();
  int twipsPerInch =  1440;
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(6 * twipsPerInch));
  for (int i = 0; i < 3; i++) {
   XWPFTableCell cell = row.getCell(i); if (cell == null) cell = row.createCell();
   CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
   tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
   tblWidth.setType(STTblWidth.DXA);
   if (cell.getParagraphs().size() > 0) {
    paragraph = cell.getParagraphs().get(0);
   } else {
    paragraph = cell.addParagraph();   
   }
   run = paragraph.createRun();
   run.setText("Header Table Cell " + i);
  }

  // create footer start
  XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = footer.getParagraphArray(0);
  if (paragraph == null) paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  run = paragraph.createRun();  
  run.setText("The Footer:");
  
  FileOutputStream out = new FileOutputStream("CreateWordHeaderFooterTable.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}
 类似资料:
  • 问题内容: 我在将新的简单XWPFTable添加到XWPFHeader(或Footer)时遇到了严重的麻烦。不幸的是,似乎只有一个人遇到相同的问题(https://bz.apache.org/bugzilla/show_bug.cgi?id=57366#c0)。 有谁有办法实现这一目标? 任何帮助将不胜感激! 亲切的问候… 〜丹尼尔 问题答案: 这可以使用公共XWPFTable insertNew

  • 问题内容: 如何使用itext从html源向pdf添加标头? 当前,我们扩展了PdfPageEventHelper并覆盖了这些方法。工作正常,但是当我进入2个以上页面时,它将引发RuntimeWorkerException。 问题答案: 通常, 禁止 在事件中添加内容。这是 禁止 添加内容到的对象。您应该使用而 不是 文档在方法中添加页眉和页脚。此外:通过一遍又一遍地解析HTML,您正在浪费大量C

  • 问题内容: 我花了一点时间试图找出一种将标头添加到的方法,但未成功。 这是我到目前为止所得到的: 在似乎是处理的配置对象的项目。由于找不到任何方法,因此决定使用方法,并将标头视图添加到第一个位置以用作标头。 Aaand这就是事情变得更糟的地方: 在尝试创建的不同时刻调用了几次之后(还尝试在设置所有内容(甚至是适配器的数据)后添加视图),我意识到我不知道这是否是正确的方法(并且它看起来不是)。 PS

  • 问题内容: 我正在使用iText 5创建pdf,并希望添加页脚。我所做的一切都像第14章中的“ iText in action”一书中所述。 没有错误,但没有显示页脚。有人可以告诉我我在做什么错吗? 我的代码: 问题答案: 您报告的问题无法重现。我以您的示例为例,并使用此事件创建了TextFooter示例: 请注意,我只创建了一次和实例,从而提高了性能。我还介绍了页脚和页眉。您声称要添加页脚,但实

  • 使用iTextSharp,您可以通过将事件附加到PDF来向PDF添加页眉/页脚,如本SO答案中所述:https://stackoverflow.com/a/19004392 我怎样才能用 iText 7 做同样的事情? 这个链接有Java代码示例,但看起来不像它使用的页面事件。