Docx4j - chaging language settings for docDefaultStyle/proper applying custom(or changed) stock styles to any text in docx

随声附和 提交于 2019-12-12 04:56:02

问题


I'm trying to use mine defined(changed) styles in

Styles styles = mp.getStyleDefinitionsPart ().getJaxbElement ();

to change styling for my entire table/individual table cells. I managed to make it by setting the properties in RPr runProperties like this:

in method applying style for table cells I use this:
setFontFamily(runProperties, style.getFontFamily());

private static void setFontFamily(RPr runProperties, String fontFamily) {
    if (fontFamily != null) {
        RFonts rf = runProperties.getRFonts();
        if (rf == null) {
            rf = new RFonts();
            runProperties.setRFonts(rf);
        }
        rf.setAscii(fontFamily);
    }
}

the same goes for other style attributes like font size, color etc. and it all works BUT ... The problem is that I use this to dynamically generate documents in Czech language, which has characters like: š, č, ř, ž, ý etc. and I use Verdana font for the table cell content and when I use this way to set the font it only applies characters different from Czech special characters.

So e.g. when I generate String "Pavlovský" into new docx, "Pavlovsk" is in Verdana font (as set) and the "ý" character is in Calibri font.

even if I simply use wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Normal", "íáýěéáěř"); - everything changes to Calibri (when I open the docx, style of paragraph is Normal - font Verdana ... as set in my code)

I even tried to set language like this

StyleDefinitionsPart styles = mp.getStyleDefinitionsPart ();
Style defaultCharacterStyle = styles.getDefaultCharacterStyle();
extracted(defaultCharacterStyle);
Style defaultParagraphStyle = styles.getDefaultParagraphStyle();
extracted(defaultParagraphStyle);
Style defaultTableStyle = styles.getDefaultTableStyle();
extracted(defaultTableStyle);

where:

private static void extracted(Style style) {
    RPr rPr = style.getRPr();
    if (rPr!=null) {
        CTLanguage lang = factory.createCTLanguage();
        lang.setVal("cs-CZ");
        lang.setEastAsia("cs-CZ");
        lang.setBidi("ar-SA");
        rPr.setLang(lang);

        style.setRPr(rPr);
    } else {
        rPr = factory.createRPr();
        CTLanguage lang = factory.createCTLanguage();
        lang.setVal("cs-CZ");
        lang.setEastAsia("cs-CZ");
        lang.setBidi("ar-SA");
        rPr.setLang(lang);
        style.setRPr(rPr);
    }
}

I will be grateful for any hint! Thanks in advance!


回答1:


Have a look at org.docx4j.fonts.RunFontSelector.java

That gives docx4j's understanding of how Word determines what font to use for a given character.

I guess in your case, the hAnsi attribute on the rFonts element is what matters.

Why don't you unzip a docx created in Word, then look at the XML?




回答2:


I faced with the same issue. Solution for me was: set all types of font

rf.setAscii(fontFamily);
rf.setCs(fontFamily);
rf.setHAnsi(fontFamily);


来源:https://stackoverflow.com/questions/29285650/docx4j-chaging-language-settings-for-docdefaultstyle-proper-applying-customor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!