BasedOn style properties not inherited by the style

Hi Team,

We are trying to update the styles in the document and few properties we override directly and for others we want it to be inherited from the basedOnStyle .Could you please let us know if something is missing

For example the font name isn;t getting updated to be same as BasedOnStyle

This below is our source code :

  @Override
  public void process(Document document, StylingInstructions stylingInstructions) {
    if (stylingInstructions.getStyles() == null) {
      return;
    }
    stylingInstructions
        .getStyles()
        .keySet()
        .forEach(
            styleName -> createOrUpdateStyle(styleName, stylingInstructions.getStyles(), document));
  }

  private void createOrUpdateStyle(
      String styleName, Map<String, StyleData> styleNameToStyleData, Document document) {
    var styleData = styleNameToStyleData.get(styleName);
    var style = getOrCreateStyle(styleName, styleData, document);

    if (styleData.getBasedOn() != null) {
      var basedOnStyleName = styleData.getBasedOn();
      createStyleIfNotExist(basedOnStyleName, styleNameToStyleData, document);
      style.setBaseStyleName(basedOnStyleName);
    }
    if (styleData.getUiPriority() != null) {
      style.setPriority(styleData.getUiPriority());
    }
    if (styleData.getLink() != null) {
      var linkedStyleName = styleData.getLink();
      createStyleIfNotExist(linkedStyleName, styleNameToStyleData, document);
      style.setLinkedStyleName(linkedStyleName);
    }
    if (styleData.getQuickFormat() != null) {
      style.isQuickStyle(styleData.getQuickFormat());
    }
    if (styleData.getFont() != null) {
      updateFont(style, styleData);
    }
    if (styleData.getParagraph() != null) {
      updateParagraph(style, styleData);
    }
  }

  private Style getOrCreateStyle(String styleName, StyleData styleData, Document document) {
    var styleType = styleData.getType();

    if (isStyleExist(styleName, document)) {
      return document.getStyles().get(styleName);
    }
    return document.getStyles().add(StyleType.fromName(styleType.name()), styleName);
  }

  private boolean isStyleExist(String name, Document document) {
    return document.getStyles().get(name) != null;
  }

  private void createStyleIfNotExist(
      String name, Map<String, StyleData> styleNameToStyleData, Document document) {
    if (!isStyleExist(name, document)) {
      createOrUpdateStyle(name, styleNameToStyleData, document);
    }
  }

  private void updateFont(Style style, StyleData styleData) {
    var fontData = styleData.getFont();
    var font = style.getFont();
    if (fontData.getBold() != null) {
      font.setBold(fontData.getBold());
    }
    if (fontData.getUnderline() != null) {
      font.setUnderline(Underline.fromName(fontData.getUnderline().name()));
    }
    if (fontData.getFontAscii() != null) {
      font.setNameAscii(fontData.getFontAscii());
    }
    if (fontData.getFontHighAnsi() != null) {
      font.setNameOther(fontData.getFontHighAnsi());
      font.setNameFarEast(fontData.getFontHighAnsi());
      font.setNameBi(fontData.getFontHighAnsi());
    }
    if (fontData.getAllCaps() != null) {
      font.setAllCaps(fontData.getAllCaps());
    }
    if (fontData.getSmallCaps() != null) {
      font.setSmallCaps(fontData.getSmallCaps());
    }
    if (fontData.getFontColor() != null) {
      font.setColor(Color.decode(fontData.getFontColor()));
    }
    if (fontData.getFontSize() != null) {
      font.setSize(fontData.getFontSize());
    }
    if (fontData.getItalic() != null) {
      font.setItalic(fontData.getItalic());
    }
  }

  private void updateParagraph(Style style, StyleData styleData) {
    var paragraphData = styleData.getParagraph();
    var paragraph = style.getParagraphFormat();

    if (paragraphData.getNumbering() != null) {
      if (paragraphData.getNumbering().getLevel() != null) {
        style.getListFormat().setListLevelNumber(paragraphData.getNumbering().getLevel());
      }
    }
    if (paragraphData.getSpaceAfter() != null) {
      paragraph.setSpaceAfter(paragraphData.getSpaceAfter());
    }
    if (paragraphData.getSpaceBefore() != null) {
      paragraph.setSpaceBefore(paragraphData.getSpaceBefore());
    }
    if (paragraphData.getAlignment() != null) {
      paragraph.setAlignment(ParagraphAlignment.fromName(paragraphData.getAlignment().name()));
    }
    if (paragraphData.getKeepNext() != null) {
      paragraph.setKeepWithNext(paragraphData.getKeepNext());
    }
    if (paragraphData.getLeftIndent() != null) {
      paragraph.setLeftIndent(paragraphData.getLeftIndent());
    }
    if (paragraphData.getRightIndent() != null) {
      paragraph.setRightIndent(paragraphData.getRightIndent());
    }
    if (paragraphData.getOutlineLevel() != null) {
      paragraph.setOutlineLevel(paragraphData.getOutlineLevel());
    }
    if (paragraphData.getPageBreakBefore() != null) {
      paragraph.setPageBreakBefore(paragraphData.getPageBreakBefore());
    }
    if (paragraphData.getLineSpacing() != null) {
      paragraph.setLineSpacing(paragraphData.getLineSpacing());
    }
    if (paragraphData.getContextualSpacing() != null) {
      // TODO No sure about this setting
      paragraph.setNoSpaceBetweenParagraphsOfSameStyle(paragraphData.getContextualSpacing());
    }
    if (paragraphData.getFirstLineIndent() != null) {
      paragraph.setFirstLineIndent(paragraphData.getFirstLineIndent());
    }

  }
}

@ManasaVTR Could you please attach your input document here for testing? Most likely font is explicitly set in your style or directly in text run properties. That is why font applied in base style is not applied.

Thank you for the response. I have attached the input document.
Get_Styled_Document-sample-1729774024186-date-fontsize.zip (53.3 KB)

The sch 1 Number styles is based upon the Normal style which has Arial as font style and Arial Font . But this font property has not got overriden

@ManasaVTR Sch 1 Number style in your document is based on Sch1Heading style:

<w:style w:type="paragraph" w:customStyle="1" w:styleId="Sch1Number">
	<w:name w:val="Sch 1 Number" />
	<w:basedOn w:val="Sch1Heading" />

And has it’s own font formatting:

<w:rPr>
	<w:b w:val="0" />
	<w:caps w:val="0" />
	<w:smallCaps />
	<w:u w:val="none" />
</w:rPr>

Sch1Heading also has it’s font formatting:

<w:rPr>
	<w:rFonts w:ascii="Arial Bold" w:hAnsi="Arial Bold" />
	<w:b />
	<w:caps />
	<w:smallCaps />
	<w:u w:val="single" />
</w:rPr>

Since font name is not explicitly specified in Sch 1 Number style, it is inherited from the base style and is Arial Bold.

If you need too override font name in the Sch 1 Number you can explicitly set font in the style properties:

Document doc = new Document("C:\\Temp\\in.docx");

Style sch1Number = doc.getStyles().get("Sch 1 Number");
sch1Number.getFont().setName("Calibri");
        
doc.save("C:\\Temp\\out.docx");

If it is required to change the font in the base style, you can use the following code:

Document doc = new Document("C:\\Temp\\in.docx");

Style sch1Number = doc.getStyles().get("Sch 1 Number");
Style baseStyle = doc.getStyles().get(sch1Number.getBaseStyleName());
baseStyle.getFont().setName("Calibri");
        
doc.save("C:\\Temp\\out.docx");

Both cases works as expected.

Thanks ! For Sch1Heading also we are setting the base style as Normal which has font style as Arial Sch 1 Heading is not updated

@ManasaVTR It should not be updated because Sch 1 Heading has explicitly set font properties. Explicitly set properties override properties set in the base style.