Search in sources :

Example 1 with Mapper

use of org.docx4j.fonts.Mapper 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 Mapper

use of org.docx4j.fonts.Mapper in project docx4j-template by vindell.

the class PhysicalFontUtils method setPhysicalFont.

/**
 * 为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 增加新的字体
 */
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);
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper)

Example 3 with Mapper

use of org.docx4j.fonts.Mapper in project docx4j-template by vindell.

the class PhysicalFontUtils method newFontMapper.

private static Mapper newFontMapper() throws Exception {
    // Set up font mapper (optional)
    // example of mapping font Times New Roman which doesn't have certain Arabic glyphs
    // eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
    // eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
    // to a font which does
    PhysicalFonts.get("Arial Unicode MS");
    /*
		 * This mapper uses Panose to guess the physical font which is a closest fit for the font used in the document.
		 * (这个映射器使用Panose算法猜测最适合这个文档使用的物理字体。)
		 * 
		 * Panose是一种依照字体外观来进行分类的方法。我们可以通过PANOSE体系将字体的外观特征进行整理,并且与其它字体归类比较。
		 * Panose的原形在1985年由Benjamin Bauermeister开发,当时一种字体由7位16进制数字定义,现在则发展为10位,也就是字体的十种特征。这每一位数字都给出了它定义的一种视觉外观的量度,如笔划的粗细或是字体衬线的样式等。
		 * Panose定义的范围:Latin Text,Latin Script,Latin Decorative,Iconographic,Japanese Text,Cyrillic Text,Hebrew。
		 * 	
		 * It is most likely to be suitable on Linux or OSX systems which don't have Microsoft's fonts installed.
		 * (它很可能适用于没有安装Microsoft字体的Linux或OSX系统。)
		 * 
		 * 1、获取Microsoft字体我们需要这些:a.在Microsoft平台上,嵌入PDF输出; b. docx4all - 所有平台 - 填充字体下拉列表
		 * setupMicrosoftFontFilenames();
		 * 2、 自动检测系统上可用的字体
		 * PhysicalFonts.discoverPhysicalFonts();
		 * 
		 */
    // Mapper fontMapper = new BestMatchingMapper();
    /*
		 * 
		 * This mapper automatically maps document fonts for which the exact font is physically available.  
	     * Think of this as an identity mapping.  For  this reason, it will work best on Windows, or a system on 
	     * which Microsoft fonts have been installed.
	     * (此映射器自动映射确切可用的文档字体,将此视为标识映射;基于这个原因,它在Windows系统或安装了微软字体库的系统运行的更好。)
	     * You can manually add your own additional mappings if you wish.
	     * 如果需要,你可以手动添加自己的字体映射
		 * 
		 * 1、 自动检测系统上可用的字体
		 * PhysicalFonts.discoverPhysicalFonts();
		 * 
		 */
    Mapper fontMapper = new IdentityPlusMapper();
    // 进行中文字体兼容处理
    fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
    fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
    fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
    fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
    fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
    fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
    fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
    fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
    fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
    fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
    fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
    fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
    fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
    fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
    return fontMapper;
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper)

Example 4 with Mapper

use of org.docx4j.fonts.Mapper in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method toPdf.

public byte[] toPdf() throws Exception {
    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);
    org.docx4j.convert.out.pdf.PdfConversion c = // = new org.docx4j.convert.out.pdf.viaHTML.Conversion(wordMLPackage);
    new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
    // = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);
    // PdfConversion writes to an output stream
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    c.output(os, new PdfSettings());
    return os.toByteArray();
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) PdfSettings(org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings)

Example 5 with Mapper

use of org.docx4j.fonts.Mapper in project docx4j-template by vindell.

the class PhysicalFontUtils method setWmlPackageFonts.

/**
 * 为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 配置中文字体;解决中文乱码问题
 */
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());
    }
}
Also used : Mapper(org.docx4j.fonts.Mapper) IdentityPlusMapper(org.docx4j.fonts.IdentityPlusMapper) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Aggregations

IdentityPlusMapper (org.docx4j.fonts.IdentityPlusMapper)6 Mapper (org.docx4j.fonts.Mapper)6 PhysicalFont (org.docx4j.fonts.PhysicalFont)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URL (java.net.URL)1 PdfSettings (org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 RFonts (org.docx4j.wml.RFonts)1