0

I am working with Apache POI 5.2.3 trying to create .doc file with a header style that will be shown in text format options in Google Docs/ Microsoft Office. I tried this code but the text style is not shown the set styles options, and I couldn't find how to mark the text as bold: (the code is based on this comment https://stackoverflow.com/a/36649411)

{
String heading1 = "My Heading 1";
String heading2 = "My Heading 2";
String heading3 = "My Heading 3";   
String heading4 = "My Heading 4";
addCustomHeadingStyle(document, styles, heading1, 1, 36, "4288BC");
addCustomHeadingStyle(document, styles, heading2, 2, 28, "4288BC");
addCustomHeadingStyle(document, styles, heading3, 3, 24, "4288BC");
addCustomHeadingStyle(document, styles, heading4, 4, 20, "000000");
}

private static void addCustomHeadingStyle(XWPFDocument docxDocument, XWPFStyles styles, String strStyleId, int headingLevel, int pointSize, String hexColor) {

    CTStyle ctStyle = CTStyle.Factory.newInstance();
    ctStyle.setStyleId(strStyleId);
    

    CTString styleName = CTString.Factory.newInstance();
    styleName.setVal(strStyleId);
    ctStyle.setName(styleName);

    CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
    indentNumber.setVal(BigInteger.valueOf(headingLevel));

    // lower number > style is more prominent in the formats bar
    ctStyle.setUiPriority(indentNumber);

    CTOnOff onoffnull = CTOnOff.Factory.newInstance();
    ctStyle.setUnhideWhenUsed(onoffnull);

    // style shows up in the formats bar
    ctStyle.setQFormat(onoffnull);

    // style defines a heading of the given level
    CTPPr ppr = CTPPr.Factory.newInstance();
    ppr.setOutlineLvl(indentNumber);
    ctStyle.setPPr(ppr);

    XWPFStyle style = new XWPFStyle(ctStyle);

    CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
    size.setVal(new BigInteger(String.valueOf(pointSize)));
    CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance();
    size2.setVal(new BigInteger("24"));
    
    CTFonts fonts = CTFonts.Factory.newInstance();
    fonts.setAscii("Loma" );

    CTRPr rpr = CTRPr.Factory.newInstance();
    rpr.addNewSz().setVal(new BigInteger(String.valueOf(24)));
    rpr.addNewSzCs().setVal(new BigInteger(String.valueOf(24)));
    rpr.addNewRFonts().setAscii("Arial");

    CTColor color=CTColor.Factory.newInstance();
    color.setVal(hexToBytes(hexColor));
    rpr.setColor(color);
    style.getCTStyle().setRPr(rpr);
    // is a null op if already defined

    style.setType(STStyleType.PARAGRAPH);
    styles.addStyle(style);

}

public static byte[] hexToBytes(String hexString) {
     HexBinaryAdapter adapter = new HexBinaryAdapter();
     byte[] bytes = adapter.unmarshal(hexString);
     return bytes;
}

So my question is:

  1. How do I make the style text bold? (not with run on single paragraph but on all paragraphs with this style)
  2. How do I add those styles to the style options in Google Docs/ Microsoft Office? (Instead of the default Headers/ In addition to them)
hido
  • 63
  • 8

1 Answers1

0
  1. How do I make the style text bold? (not with run on single paragraph but on all paragraphs with this style):

Bold is a text run property (RPr). So in short this would be:

...
CTRPr rpr = ctStyle.addNewRPr();
...
rpr.addNewB()...
...
  1. How do I add those styles to the style options in Google Docs/ Microsoft Office? (Instead of the default Headers/ In addition to them):

This is not possible for Google Docs, at least not without add-ons. Only the present heading styles (heading 1 to 6) can be customized.

The following complete example shows how to customize heading 1 style.

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

public class CreateWordCustomizedHeading1Style {

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

  XWPFDocument document = new XWPFDocument();    
  
  XWPFStyles styles = document.createStyles();  
  
  String heading1Id = "Heading1";
  String heading1Name = "heading 1";
  addCustomizedHeading1Style(styles, heading1Id, heading1Name, 1, 
    "Arial", 36, "4288BC", true);

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Test text for heading 1");

  paragraph.setStyle(heading1Id);
  
  FileOutputStream out = new FileOutputStream("./CreateWordCustomizedHeading1Style.docx");   
  document.write(out);
  out.close();
  document.close();

 }

 private static void addCustomizedHeading1Style(XWPFStyles styles, String heading1Id, String heading1Name, int uiPriority, 
  String fontName, int fontSize, String hexColor, boolean bold) {

  CTStyle ctStyle = CTStyle.Factory.newInstance();
  ctStyle.setStyleId(heading1Id);

  ctStyle.addNewName().setVal(heading1Name);

  ctStyle.addNewUiPriority().setVal(BigInteger.valueOf(uiPriority));

  ctStyle.addNewQFormat();

  CTRPr rpr = ctStyle.addNewRPr();
  rpr.addNewSz().setVal(new BigInteger(String.valueOf(fontSize)));
  rpr.addNewSzCs().setVal(new BigInteger(String.valueOf(fontSize)));
  rpr.addNewRFonts().setAscii(fontName);    
  rpr.getRFontsArray(0).setHAnsi(fontName);    
  rpr.addNewColor().setVal(hexColor);
  rpr.addNewB().setVal(bold);
    
  XWPFStyle style = new XWPFStyle(ctStyle);
  style.setType(STStyleType.PARAGRAPH);
  styles.addStyle(style);

 }

}

Code is tested and works using current apache poi 5.2.3. Not tested using older versions. Nobody should use old Apache POI versions.

The result looks like so in Google Docs:

enter image description here

Axel Richter
  • 56,077
  • 6
  • 60
  • 87