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