Apache Poi - XWPF:如何改变字体颜色,大小,页脚页码的样式在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中的文本格式化只适用于文本运行。

如果需要特殊的文本格式,则该字段必须在文本运行中。但是在文本运行中不可能有简单的字段。要在文本运行中存储字段,必须有一个字段char类型为BEGIN的特殊文本运行,然后是一个字段内容为字符串文本的特殊文本运行,最后是一个字段char类型为END的特殊文本运行。

下面的代码显示了这一点。所有使用的XWPFRuns都提供文本格式化。

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();
}
}

最新更新