Apache Poi - XWPF:如何更改 docx 中页脚页码的字体颜色、大小和样式

Apache Poi - XWPF: How to change font color, size, style of the footer Page Number in docx

我试过下面的代码,但似乎 setColor() 只更新了段落中的文本 -- 它不影响 CTSimpleField 页码的颜色。

CTSectPr sectPr = getSectionProperty(0);
CTHdrFtrRef footerRef = sectPr.addNewFooterReference();
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
footerRef.setId(footer.getPackageRelationship().getId());

XWPFParagraph paragraph = footer.createParagraph();

CTSimpleField simpleField =  paragraph.getCTP().addNewFldSimple().setInstr("PAGE  \* Arabic  \* MERGEFORMAT");

XWPFRun run = paragraph.createRun();
run.setText("Page ");
run.setColor("ffffff");

有没有办法更新 CTSimpleField 字体颜色、样式和大小?或者是否有不同的方法来生成页码,我们可以更新它们的字体颜色?

setColorXWPFRun的一个方法。所以它只影响 XWPFRun.

中的文本

方法XWPFParagraph paragraph ...; ... paragraph.getCTP().addNewFldSimple() 是向Office Open XML Word 文档添加字段的最简单方法。但是,由于这会在所有文本 运行 之外的段落中添加一个字段,因此无法进行文本格式化。这是因为 Word 中的文本格式只能用于文本 运行s.

如果需要特殊的文本格式,则字段必须在文本中 运行。但是在文本 运行 中不可能有简单字段。要在文本 运行 中存储字段,必须有一个字段字符类型为 BEGIN 的特殊文本 运行,后跟字段内容为 in-string 文本的特殊文本 运行 ,后跟一个特殊文本 运行,其字段字符类型为 END.

以下代码显示了这一点。所有使用的 XWPFRun 都提供来自 atting 的文本。

import java.io.FileOutputStream;

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

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.wp.usermodel.HeaderFooterType;

public class CreateWordHeaderFooter3 {
    
 static XWPFRun createRunFldCharTypeBegin(XWPFParagraph paragraph) {
  XWPFRun run = paragraph.createRun();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR cTR = run.getCTR();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar cTFldChar = org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar.Factory.newInstance();
  cTFldChar.setFldCharType​(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.BEGIN);
  cTR.setFldCharArray​(new org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar[]{cTFldChar});
  return run;
 }

 static XWPFRun createRunInstrText(XWPFParagraph paragraph, String instrText) {
  XWPFRun run = paragraph.createRun();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR cTR = run.getCTR();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText cTText = org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText.Factory.newInstance();
  cTText.setStringValue(instrText);
  cTR.setInstrTextArray​(new org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText[]{cTText});
  return run;
 }
 
 static XWPFRun createRunFldCharTypeEnd(XWPFParagraph paragraph) {
  XWPFRun run = paragraph.createRun();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR cTR = run.getCTR();
  org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar cTFldChar = org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar.Factory.newInstance();
  cTFldChar.setFldCharType​(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.END);
  cTR.setFldCharArray​(new org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar[]{cTFldChar});
  return run;
 }

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

  XWPFDocument doc = new XWPFDocument();
  XWPFParagraph paragraph;
  XWPFRun run;
  
  // the body content
  paragraph = doc.createParagraph();
  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
  // create header start
  XWPFHeader header = doc.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 header end

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

  paragraph = footer.getParagraphArray(0);
  if (paragraph == null) paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  
  run = paragraph.createRun();  
  run.setText("Page ");
  
  //paragraph.getCTP().addNewFldSimple().setInstr("PAGE \* MERGEFORMAT");
  run = createRunFldCharTypeBegin(paragraph);
  run.setFontSize(16);
  run.setColor("FF0000");
  run = createRunInstrText(paragraph, "PAGE \* MERGEFORMAT");
  run = createRunFldCharTypeEnd(paragraph);
  
  run = paragraph.createRun();  
  run.setText(" of ");
  
  //paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \* MERGEFORMAT");
  run = createRunFldCharTypeBegin(paragraph);
  run.setFontSize(16);
  run.setColor("0000FF");
  run = createRunInstrText(paragraph, "NUMPAGES \* MERGEFORMAT");
  run = createRunFldCharTypeEnd(paragraph);
  
  // create footer end

  FileOutputStream out = new FileOutputStream("CreateWordHeaderFooter.docx");
  doc.write(out);
  out.close();
  doc.close();

 }
}