Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表中的页码_Java_Ms Word_Apache Poi_Page Numbering - Fatal编程技术网

Java 表中的页码

Java 表中的页码,java,ms-word,apache-poi,page-numbering,Java,Ms Word,Apache Poi,Page Numbering,我创建了一个方法,在Word文档页脚中添加一个表,其中有一行三列。现在,我试图将页码添加到中间列,我所发现的只是将页码直接添加到页脚上。我用过那个代码,但没能做到。 有人能帮忙吗 这是适用于表的代码 static public void footer(XWPFDocument doc) { CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy footerPoli

我创建了一个方法,在Word文档页脚中添加一个表,其中有一行三列。现在,我试图将页码添加到中间列,我所发现的只是将页码直接添加到页脚上。我用过那个代码,但没能做到。 有人能帮忙吗

这是适用于表的代码

static public void footer(XWPFDocument doc) {

    CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy footerPolicy= new XWPFHeaderFooterPolicy(doc, sectPr);

    XWPFFooter footer = footerPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

    XWPFRun run;

    // create table in footer
    XWPFParagraph pgh1 = footer.createParagraph();
    XmlCursor cursor = pgh1.getCTP().newCursor();
    XWPFTable table = footer.insertNewTbl(cursor);
    XWPFTableRow row = table.getRow(0);
    if (row == null) row = table.createRow();
    int twipsPerInch = 1440;
    table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(7 * twipsPerInch));

    XWPFTableCell cell = row.getCell(0);
    if (cell == null) cell = row.createCell();
    CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
    tblWidth.setType(STTblWidth.DXA);
    pgh1 = cell.getParagraphs().get(0);
    run = pgh1.createRun();
    run.setText("blah blah blah");

    cell = row.getCell(1);
    if (cell == null) cell = row.createCell();
    tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(3 * twipsPerInch));
    tblWidth.setType(STTblWidth.DXA);
    XWPFParagraph pageNumberParagraph = cell.getParagraphs().get(0); 
    pageNumberParagraph.setAlignment(ParagraphAlignment.CENTER);
    run = pageNumberParagraph.createRun();
    run.setText("1");

    cell = row.getCell(2);
    if (cell == null) cell = row.createCell();
    tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(2 * twipsPerInch));
    tblWidth.setType(STTblWidth.DXA);
    pgh1 = cell.getParagraphs().get(0);
    pgh1.setAlignment(ParagraphAlignment.RIGHT);
    run = pgh1.createRun();
    run.setText("blah blah blah");
}
然后我试着用这个来添加页码,但我想不出来

CTPageNumber pgNum = sectPr.isSetPgNumType() ? sectPr.getPgNumType() : sectPr.addNewPgNumType();
pgNum.setStart(BigInteger.valueOf(1));
有关如何在
Word
中创建页码字段的信息,请参阅

为此,创建一个文本运行并在这些文本运行中插入字段
“PAGE\\*MERGEFORMAT”
和/或
“NUMPAGES\\\*MERGEFORMAT”
。这就是
Word
的GUI所做的

...
run = paragraph.createRun();  
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();  
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
...
当然,具有这些字段的文本运行也可以在表格单元格中

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
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 document = new XWPFDocument();

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

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum.... page 1");

  paragraph = document.createParagraph();
  run=paragraph.createRun();
  run.addBreak(BreakType.PAGE); 
  run.setText("Lorem ipsum.... page 2");

  paragraph = document.createParagraph();
  run=paragraph.createRun();
  run.addBreak(BreakType.PAGE); 
  run.setText("Lorem ipsum.... page 3"); 

  // create header start
  XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);

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

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

  // create footer start
  XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);

  // create table in footer
  paragraph = footer.getParagraphArray(0);
  if (paragraph == null) paragraph = footer.createParagraph();
  XmlCursor cursor = paragraph.getCTP().newCursor();
  XWPFTable table = footer.insertNewTbl(cursor);
  XWPFTableRow row = table.getRow(0); if (row == null) row = table.createRow();
  int twipsPerInch =  1440;
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(7 * 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(((i==1)?3:2) * twipsPerInch));
   tblWidth.setType(STTblWidth.DXA);
   paragraph = cell.getParagraphs().get(0);
   run = paragraph.createRun();
   if (i == 0) {
    paragraph.setAlignment(ParagraphAlignment.LEFT);
    run.setText("Left footer text");
   } else if (i == 1) {
    paragraph.setAlignment(ParagraphAlignment.CENTER);
    run.setText("Page ");
    paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
    run = paragraph.createRun();  
    run.setText(" of ");
    paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
   } else if (i == 2) {
    paragraph.setAlignment(ParagraphAlignment.RIGHT);
    run.setText("Right footer text");
   }
  }

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

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入org.apache.poi.wp.usermodel.HeaderFooterType;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
导入org.apache.xmlbeans.XmlCursor;
导入java.math.biginger;
公共类CreateWordHeaderFooterTable{
公共静态void main(字符串[]args)引发异常{
XWPFDocument document=新的XWPFDocument();
//正文内容
XWPFParagraph paragraph paragraph=document.createParagraph();
XWPFRun=段落.createRun();
run.setText(“主体:”);
段落=document.create段落();
run=段落.createRun();
run.setText(“Lorem ipsum…第1页”);
段落=document.create段落();
run=段落.createRun();
run.addBreak(BreakType.PAGE);
run.setText(“Lorem ipsum…第2页”);
段落=document.create段落();
run=段落.createRun();
run.addBreak(BreakType.PAGE);
run.setText(“Lorem ipsum…第3页”);
//创建标题开始
XWPFHeader header=document.createHeader(HeaderFooterType.DEFAULT);
段落=标题。getParagraphArray(0);
如果(段落==null)段落=header.create段落();
段落.设置对齐(段落对齐.左);
run=段落.createRun();
run.setText(“标题”);
//创建页脚开始
xwpfooter footer=document.createFooter(HeaderFooterType.DEFAULT);
//在页脚中创建表
段落=页脚。getParagraphArray(0);
如果(段落==null)段落=footer.create段落();
XmlCursor cursor=段落.getCTP().newCursor();
XWPFTable table=footer.insertNewTbl(光标);
XWPFTableRow=table.getRow(0);if(row==null)row=table.createRow();
int twipperinch=1440;
table.getCTTbl();
对于(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((i==1)?3:2)*twipperinch));
设置类型(STTblWidth.DXA);
段落=cell.get段落().get(0);
run=段落.createRun();
如果(i==0){
段落.设置对齐(段落对齐.左);
run.setText(“左脚文本”);
}else如果(i==1){
段落.设置对齐(段落对齐.中间);
run.setText(“页面”);
段落.getCTP().addNewFldSimple().setInstr(“第\\*页合并格式”);
run=段落.createRun();
run.setText(“of”);
段落.getCTP().addNewFldSimple().setInstr(“NUMPAGES\\\*MERGEFORMAT”);
}else如果(i==2){
段落.setAlignment(段落alignment.RIGHT);
run.setText(“右脚文本”);
}
}
FileOutputStream out=新的FileOutputStream(“CreateWordHeaderFooterTable.docx”);
文件。写(出);
out.close();
document.close();
}
}

的确,谢谢!成功了,谢谢!我想了解更多关于CTxx的信息。我会更深入地看一看。再次感谢