Search in sources :

Example 1 with PhysicalFont

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);
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) PhysicalFont(org.docx4j.fonts.PhysicalFont) RFonts(org.docx4j.wml.RFonts) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) URL(java.net.URL)

Example 2 with PhysicalFont

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);
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) PhysicalFont(org.docx4j.fonts.PhysicalFont) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper)

Aggregations

IdentityPlusMapper (org.docx4j.fonts.IdentityPlusMapper)2 Mapper (org.docx4j.fonts.Mapper)2 PhysicalFont (org.docx4j.fonts.PhysicalFont)2 URL (java.net.URL)1 RFonts (org.docx4j.wml.RFonts)1