0

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!

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • This question is related to http://stackoverflow.com/questions/29607496/how-to-handle-special-characters-when-converting-from-html-to-docx – Cláudio Apr 13 '15 at 14:16

2 Answers2

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?

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • thank you for responding! i tried digging in it, but unfortunatelly there was no way for me to set the font for Czech special characters even though i had all the languange settings right, it kept setting the characters to Calibri font ... I've simply resolved this issue by using library Apache POI. It is much simplier and easier to work with and finally works! – Linh Vu Manh Mar 31 '15 at 13:02
  • Of course there is a way to set the font appropriately. You just need to know the correct attributes to set (which you get from your Word docx). Sounds like you fluked it when you started using POI. Good for you! – JasonPlutext Apr 01 '15 at 05:03
  • What i wasn't able to remotely achieve in docx4j in 2 weeks, i managed to finish in 3 days with POI :) so yes, there sure is a way to fix it in docx4j, just not worth the time i'd have to spend on it searching and trying. For me huge plus to POI. Thanks for your help anyway good Sir! – Linh Vu Manh Apr 09 '15 at 13:40
  • No worries :-) The fundamental skills and concepts required for working with POI's XWPF and docx4j are largely the same, so the time you spent with docx4j would have made POI seem that much easier. – JasonPlutext Apr 09 '15 at 23:20
0

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

rf.setAscii(fontFamily);
rf.setCs(fontFamily);
rf.setHAnsi(fontFamily);
ifedik
  • 1