Java Code Examples for org.docx4j.openpackaging.packages.WordprocessingMLPackage#setFontMapper()

The following examples show how to use org.docx4j.openpackaging.packages.WordprocessingMLPackage#setFontMapper() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlConverter.java    From docx4j-template with Apache License 2.0 7 votes vote down vote up
/**
 * 为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 配置中文字体
 *
 * @param wordMLPackage
 * @throws Exception
 */
protected void configSimSunFont(WordprocessingMLPackage wordMLPackage) throws Exception {
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);

    String fontFamily = "SimSun";

    URL simsunUrl = this.getClass().getResource("/org/noahx/html2docx/simsun.ttc"); //加载字体文件(解决linux环境下无中文字体问题)
    PhysicalFonts.addPhysicalFonts(fontFamily, simsunUrl);
    PhysicalFont simsunFont = PhysicalFonts.get(fontFamily);
    fontMapper.put(fontFamily, simsunFont);

    RFonts rfonts = Context.getWmlObjectFactory().createRFonts(); //设置文件默认字体
    rfonts.setAsciiTheme(null);
    rfonts.setAscii(fontFamily);
    wordMLPackage.getMainDocumentPart().getPropertyResolver()
            .getDocumentDefaultRPr().setRFonts(rfonts);
}
 
Example 2
Source File: PhysicalFontUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void setWmlPackageFonts(WordprocessingMLPackage wmlPackage) throws Docx4JException {
try {
	//字体映射;  
	Mapper fontMapper = newFontMapper();
	//设置文档字体库
	wmlPackage.setFontMapper(fontMapper, true);
} catch (Exception e) {
	throw new Docx4JException(e.getMessage(),e.getCause());
}
  }
 
Example 3
Source File: PhysicalFontUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void setPhysicalFont(WordprocessingMLPackage wmlPackage,PhysicalFont physicalFont) throws Exception {
Mapper fontMapper = wmlPackage.getFontMapper() == null ? new IdentityPlusMapper() : wmlPackage.getFontMapper();
//分别设置字体名和别名对应的字体库
fontMapper.put(physicalFont.getName(), physicalFont );
//设置文档字体库
wmlPackage.setFontMapper(fontMapper, true);
  }
 
Example 4
Source File: PhysicalFontUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void setPhysicalFont(WordprocessingMLPackage wmlPackage,String fontName) throws Exception {
//Mapper fontMapper = new BestMatchingMapper();  
Mapper fontMapper = wmlPackage.getFontMapper() == null ? new IdentityPlusMapper() : wmlPackage.getFontMapper();
//获取字体库
PhysicalFont physicalFont = PhysicalFonts.get(fontName);
//分别设置字体名和别名对应的字体库
fontMapper.put(fontName, physicalFont );
//设置文档字体库
wmlPackage.setFontMapper(fontMapper, true);
  }
 
Example 5
Source File: FontMapperHolder.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public static WordprocessingMLPackage useFontMapper(WordprocessingMLPackage wmlPackage) throws Exception {
	if(fontMapper != null) {
		wmlPackage.setFontMapper(fontMapper);
	}
	return wmlPackage;
}