Skip to content
Advertisement

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

I have tried the code below, but it seems that setColor() only updates the text in the paragraph — it does not affect the color of 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");

Is there a way to update the CTSimpleField font color, style, and size? or is there a different way to generate Page Numbers where we can update their font color?

Advertisement

Answer

The setColor is a method of XWPFRun. So it only affects the text in that XWPFRun.

The method XWPFParagraph paragraph ...; ... paragraph.getCTP().addNewFldSimple() ist the simplest method to add a field into a Office Open XML Word document. But as this adds a field into the paragraph outside all text runs, no text formatting is possible. This is because text formatting in Word is only possible for text runs.

If special text formatting is needed, then the field would must be within a text run. But there is no simple field possible within a text run. To store fields in text runs, there must be a special text run having field char type BEGIN, followed by a special text run having the field content as in-string text, followed by a special text run having field char type END.

Following code shows that. All used XWPFRuns provide text fromatting.

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

 }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement