use of org.docx4j.fonts.PhysicalFont in project docx4j-template by vindell.
the class HtmlConverter method configSimSunFont.
/**
* 为 {@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";
// 加载字体文件(解决linux环境下无中文字体问题)
URL simsunUrl = this.getClass().getResource("/org/noahx/html2docx/simsun.ttc");
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);
}
use of org.docx4j.fonts.PhysicalFont in project docx4j-template by vindell.
the class PhysicalFontUtils method setPhysicalFont.
/**
* 为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 增加新的字体
*/
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);
}
Aggregations