我已经尝试了下面的代码,但是setColor()
似乎只更新了段落中的文本——它不会影响CTSimpleField
Page Number的颜色。
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
字体颜色、样式和大小?或者有没有不同的方法来生成页码,我们可以更新它们的字体颜色?
setColor
是XWPFRun
的一种方法。所以它只影响XWPFRun
中的文本。
XWPF段落…;…段落. getCTP().addNewFld简单()
方法是将字段添加到Office OpenXMLWord文档中的最简单方法。但是,由于这会在所有文本运行之外的段落中添加一个字段,因此无法进行文本格式设置。这是因为Word中的文本格式只能用于文本运行。
如果需要特殊的文本格式,那么字段必须在文本运行中。但是文本运行中没有简单的字段。要在文本运行中存储字段,必须有一个字段字符类型为BEGIN的特殊文本运行,然后是一个字段内容为字符串内文本的特殊文本运行,然后是一个字段字符类型为END的特殊文本运行。
以下代码显示了这一点。所有使用的XWPFRun
都提供文本起动。
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();
}
}