Page 1 of 1

How to get the text font name

PostPosted: Tue Dec 08, 2009 10:51 am
by robot
Hi,
I want to get the text font name,How can I do?
Code: Select all
<w:r w:rsidRPr="00564CD8">
        <w:rPr>
          <w:rFonts w:ascii="黑体" w:eastAsia="黑体" w:hint="eastAsia"/>
        </w:rPr>
        <w:t>4</w:t>
</w:r>

like this, I know the font name is "黑体".
Code: Select all
<w:r>
        <w:rPr>
          <w:rFonts w:hint="eastAsia"/>
        </w:rPr>
        <w:t>1</w:t>
</w:r>

if the code like this one,How to get the font name?
thanks.

Re: How to get the text font name

PostPosted: Tue Dec 08, 2009 12:59 pm
by jason
At a guess, I'd say look in the docDefaults in styles.xml. You may also need to take themes into account?

Re: How to get the text font name

PostPosted: Wed Dec 09, 2009 1:14 am
by jason
The code below is from http://dev.plutext.org/trac/docx4j/brow ... olver.java

Code: Select all
   /**
    * Returns default document font, by attempting to look at styles/docDefaults/rPrDefault/rPr/rFonts.
    *
    * @return default document font.
    */
   public String getDefaultFont() {
      
      // First look at the defaults
      // 3 look at styles/rPrDefault
      // 3.1 if there is an rFonts element, do what it says (it may refer you to the theme part,
      //     in which case if there is no theme part, default to "internally stored settings"
      //      (there is no normal.dot; see http://support.microsoft.com/kb/924460/en-us )
      //      in this case Calibri and Cambria)
      // 3.2 if there is no rFonts element, default to Times New Roman.
      
      org.docx4j.wml.RFonts rFonts = documentDefaultRPr.getRFonts();
      if (rFonts==null) {
         log.info("No styles/docDefaults/rPrDefault/rPr/rFonts - default to Times New Roman");
         // Yes, Times New Roman is still buried in Word 2007
         return "Times New Roman";                   
      } else {                  
         // Usual case
         if (rFonts.getAsciiTheme()!=null ) {
            // for example minorHAnsi, which I think translates to minorFont/latin
            if (rFonts.getAsciiTheme().equals(org.docx4j.wml.STTheme.MINOR_H_ANSI)) {
               if (themePart!=null) {
                  org.docx4j.dml.BaseStyles.FontScheme fontScheme = themePart.getFontScheme();
                  if (fontScheme.getMinorFont()!=null
                        && fontScheme.getMinorFont().getLatin()!=null) {
                                                   
                     org.docx4j.dml.TextFont textFont = fontScheme.getMinorFont().getLatin();
                     log.info("minorFont/latin font is " + textFont.getTypeface() );
                     return textFont.getTypeface();
                  } else {
                     // No minorFont/latin in theme part - default to Calibri
                     log.info("No minorFont/latin in theme part - default to Calibri");                        
                     return "Calibri";
                  }
               } else {
                  // No theme part - default to Calibri
                  log.info("No theme part - default to Calibri");
                  return "Calibri";
               }
            } else {
               // TODO
               log.error("Don't know how to handle: "
                     + rFonts.getAsciiTheme());
               return null;
            }
         } else if (rFonts.getAscii()!=null ) {
            log.info("rPrDefault/rFonts referenced " + rFonts.getAscii());                        
            return rFonts.getAscii();                      
         } else {
            // TODO
            log.error("Neither ascii or asciTheme.  What to do? ");
            return null;
         }                  
      }
   }



You could extend it to handle w:eastAsia

Hope this helps .. Jason

Re: How to get the text font name

PostPosted: Wed Dec 09, 2009 4:30 am
by robot
thanks