Java ApachePOI:如何设置字段的字体样式(页面、PAGENUM、PAGEREF…)

Java ApachePOI:如何设置字段的字体样式(页面、PAGENUM、PAGEREF…),java,apache,ms-word,apache-poi,docx,Java,Apache,Ms Word,Apache Poi,Docx,从另一个(已回答的)问题中,我学会了如何在Word文档中添加页面计数器。此外,我需要在字段(即页面计数器)上设置字体系列样式(颜色、粗体、斜体、下划线…)。如何做到这一点 CTSimpleField ctSimpleField = paragraph.getCTP().addNewFldSimple(); CTSimpleField不提供直接设置这些属性的方法 原始问题: 为了能够格式化,我们需要在Word中运行。但要使用运行创建字段,我们需要为每个字段运行三次。一次运行以标记FldChar开

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

CTSimpleField ctSimpleField = paragraph.getCTP().addNewFldSimple();
CTSimpleField不提供直接设置这些属性的方法

原始问题:


为了能够格式化,我们需要在
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"));

 }
}

为了能够格式化,我们需要在
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"));

 }
}
格式化“第x页,共y页”仅格式化“第”页和“共”页,而不格式化x页或y页。
XWPFRun=paragration.createRun();run.setText(“页面”);run.setBold(true);run.setFontFamily(“Arial”);格式化“第x页,共y页”仅格式化“第”页和“共”页,而不格式化x页或y页。
XWPFRun=paragration.createRun();run.setText(“页面”);run.setBold(true);run.setFontFamily(“Arial”);