java - Apache POI : How to set font style for a field (PAGE, PAGENUM, PAGEREF...)

标签 java apache ms-word apache-poi docx

从另一个(已回答的)问题中,我学会了如何将页面计数器添加到 Word 文档中。此外,我需要在字段(即页面计数器)上设置字体系列样式(颜色、粗体、斜体、下划线……)。如何实现?

CTSimpleField ctSimpleField = paragraph.getCTP().addNewFldSimple();

CTSimpleField 不提供直接设置这些属性的方法。

原始问题:How to add page numbers in format X of Y while creating a word document using apache poi api?

import java.io.*;

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

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

public class CreateWordHeaderFooter {

 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.... page 1");

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

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

  // create header-footer
  XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
  if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

  // 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 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("Page ");
  // this adds the page counter
  paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
  run = paragraph.createRun();
  run.setText(" of ");
  // this adds the page total number
  paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");

  doc.write(new FileOutputStream("CreateWordHeaderFooter.docx"));

 }
}

最佳答案

为了能够格式化,我们需要在 Word 中运行。但是要使用运行创建字段,我们需要为每个字段运行三次。第一次运行标记 FldChar 开始,然后第一次运行标记字段 InstrText,第三次运行标记 FldChar 结束。每次运行都可以而且必须根据需要进行格式化。

例子:

import java.io.*;

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

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

public class CreateWordHeaderFooter {

 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.... page 1");

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

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

  // create header-footer
  XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
  if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

  // 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 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.setBold(true);  
  run.setText("Page ");

  run = paragraph.createRun();  
  run.setBold(true); 
  run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.BEGIN);

  run = paragraph.createRun(); 
  run.setBold(true); 
  run.getCTR().addNewInstrText().setStringValue("PAGE \\* MERGEFORMAT");

  run = paragraph.createRun();  
  run.setBold(true); 
  run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.END);

  run = paragraph.createRun();  
  run.setBold(true); 
  run.setText(" of ");

  run = paragraph.createRun();  
  run.setBold(true); 
  run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.BEGIN);

  run = paragraph.createRun();  
  run.setBold(true); 
  run.getCTR().addNewInstrText().setStringValue("NUMPAGES \\* MERGEFORMAT");

  run = paragraph.createRun();  
  run.setBold(true); 
  run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.END);

  doc.write(new FileOutputStream("CreateWordHeaderFooter.docx"));

 }
}

关于java - Apache POI : How to set font style for a field (PAGE, PAGENUM, PAGEREF...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45795752/

相关文章:

java - 如何动态更新 Android 应用程序?

python - 有没有人设法在 Mac OS X Leopard 上为 apache 编译 mod_wsgi?

c# - Sharepoint 2010 从模板创建列表

php - Apache ErrorDocument 指令不重定向

ms-access - 从 Word 文档表单控件获取数据

c# - 在 C# WinForms 中预览文档(Word、Excel、PDF、文本文件等)?

java - 在 EJB 2 session Bean 中实现单例功能

java - 文件选择器上的重命名按钮

java - 修复 Eclipse 中的模块 {a} 不会 "opens {package}"到模块 {B}

apache mod_proxy ProxyPassReverse Location header