Search code examples
fontsms-wordapache-poidocxxwpf

How to find out what font family is used by a paragraph in OOXML?


Lets say that a particular style is applied to a paragraph. I want to find out what the font family of the style is. I tried opening styles.xml file inside the docx. I cannot find the w:ascii attribute set for a lot of the styles. For example, consider the style Heading1. The OOXML corresponding to this style is

<w:style w:type="paragraph" w:styleId="Heading1">
    <w:name w:val="heading 1"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:link w:val="Heading1Char"/>
    <w:uiPriority w:val="9"/>
    <w:qFormat/>
    <w:rsid w:val="00FB3E81"/>
    <w:pPr>
        <w:keepNext/>
        <w:keepLines/>
        <w:spacing w:before="240"/>
        <w:outlineLvl w:val="0"/>
    </w:pPr>
    <w:rPr>
        <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
        <w:color w:val="2E74B5" w:themeColor="accent1" w:themeShade="BF"/>
        <w:sz w:val="32"/>
        <w:szCs w:val="32"/>
    </w:rPr>
</w:style>

In the w:rFonts tag there is no w:ascii attribute to extract the font family. Where is the font family details for this style is stored and how to extract it for use in a java application? I am using Apache POI for parsing the document.


Solution

  • Examine w:rFonts. In the example you cite w:asciiTheme="majorHAnsi" tells you that it is using the font which has been defined in the theme as majorHAnsi , which is the theme font used for headings. So you would need to determine the font family specified in the theme file, which is likely to be theme1.xml

    Edit: In the theme file you will find the fontScheme which defines two fonts, majorFont and minorFont. These correspond to majorHAnsi and minorHAnsi If the theme being used is one of the standard themes that come with Office there may be numerous entries dependent on the language script being used.

            <a:fontScheme name="Office Theme">
            <a:majorFont>
                <a:latin typeface="Calibri Light" panose="020F0302020204030204" />
                <a:ea typeface="" />
                <a:cs typeface="" />
                <a:font script="Jpan" typeface="游ゴシック Light" />
                <a:font script="Hang" typeface="맑은 고딕" />
                <a:font script="Hans" typeface="等线 Light" />
                <a:font script="Hant" typeface="新細明體" />
                <a:font script="Arab" typeface="Times New Roman" />
                <a:font script="Hebr" typeface="Times New Roman" />
                <a:font script="Thai" typeface="Angsana New" />
                <a:font script="Ethi" typeface="Nyala" />
                <a:font script="Beng" typeface="Vrinda" />
                <a:font script="Gujr" typeface="Shruti" />
                <a:font script="Khmr" typeface="MoolBoran" />
                <a:font script="Knda" typeface="Tunga" />
                <a:font script="Guru" typeface="Raavi" />
                <a:font script="Cans" typeface="Euphemia" />
                <a:font script="Cher" typeface="Plantagenet Cherokee" />
                <a:font script="Yiii" typeface="Microsoft Yi Baiti" />
                <a:font script="Tibt" typeface="Microsoft Himalaya" />
                <a:font script="Thaa" typeface="MV Boli" />
                <a:font script="Deva" typeface="Mangal" />
                <a:font script="Telu" typeface="Gautami" />
                <a:font script="Taml" typeface="Latha" />
                <a:font script="Syrc" typeface="Estrangelo Edessa" />
                <a:font script="Orya" typeface="Kalinga" />
                <a:font script="Mlym" typeface="Kartika" />
                <a:font script="Laoo" typeface="DokChampa" />
                <a:font script="Sinh" typeface="Iskoola Pota" />
                <a:font script="Mong" typeface="Mongolian Baiti" />
                <a:font script="Viet" typeface="Times New Roman" />
                <a:font script="Uigh" typeface="Microsoft Uighur" />
                <a:font script="Geor" typeface="Sylfaen" />
            </a:majorFont>
            <a:minorFont>
                <a:latin typeface="Calibri" panose="020F0502020204030204" />
                <a:ea typeface="" />
                <a:cs typeface="" />
                <a:font script="Jpan" typeface="游ゴシック" />
                <a:font script="Hang" typeface="맑은 고딕" />
                <a:font script="Hans" typeface="等线" />
                <a:font script="Hant" typeface="新細明體" />
                <a:font script="Arab" typeface="Arial" />
                <a:font script="Hebr" typeface="Arial" />
                <a:font script="Thai" typeface="Cordia New" />
                <a:font script="Ethi" typeface="Nyala" />
                <a:font script="Beng" typeface="Vrinda" />
                <a:font script="Gujr" typeface="Shruti" />
                <a:font script="Khmr" typeface="DaunPenh" />
                <a:font script="Knda" typeface="Tunga" />
                <a:font script="Guru" typeface="Raavi" />
                <a:font script="Cans" typeface="Euphemia" />
                <a:font script="Cher" typeface="Plantagenet Cherokee" />
                <a:font script="Yiii" typeface="Microsoft Yi Baiti" />
                <a:font script="Tibt" typeface="Microsoft Himalaya" />
                <a:font script="Thaa" typeface="MV Boli" />
                <a:font script="Deva" typeface="Mangal" />
                <a:font script="Telu" typeface="Gautami" />
                <a:font script="Taml" typeface="Latha" />
                <a:font script="Syrc" typeface="Estrangelo Edessa" />
                <a:font script="Orya" typeface="Kalinga" />
                <a:font script="Mlym" typeface="Kartika" />
                <a:font script="Laoo" typeface="DokChampa" />
                <a:font script="Sinh" typeface="Iskoola Pota" />
                <a:font script="Mong" typeface="Mongolian Baiti" />
                <a:font script="Viet" typeface="Arial" />
                <a:font script="Uigh" typeface="Microsoft Uighur" />
                <a:font script="Geor" typeface="Sylfaen" />
            </a:minorFont>
        </a:fontScheme>