1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com;
import org.docx4j.Docx4J; import org.docx4j.fonts.IdentityPlusMapper; import org.docx4j.fonts.Mapper; import org.docx4j.fonts.PhysicalFonts; import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import java.io.*;
public class App {
private static void setFontMapper(WordprocessingMLPackage wordPackage) throws Exception { Mapper fontMapper = new IdentityPlusMapper(); fontMapper.put("宋体", PhysicalFonts.get("SimSun")); fontMapper.put("黑体", PhysicalFonts.get("SimHei")); fontMapper.put("楷体", PhysicalFonts.get("KaiTi")); fontMapper.put("隶书", PhysicalFonts.get("LiSu")); fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft YaHei")); fontMapper.put("新宋体", PhysicalFonts.get("NSimSun")); fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai")); fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong")); fontMapper.put("仿宋", PhysicalFonts.get("FangSong")); fontMapper.put("幼圆", PhysicalFonts.get("YouYuan")); fontMapper.put("华文宋体", PhysicalFonts.get("STSong")); fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong")); fontMapper.put("等线", PhysicalFonts.get("DengXian")); fontMapper.put("等线 Light", PhysicalFonts.get("DengXian Light")); wordPackage.setFontMapper(fontMapper); }
public static void main(String[] args) throws Exception { String input = "input.docx"; String output = "output.pdf"; WordprocessingMLPackage wordPackage = Docx4J.load(new File(input)); setFontMapper(wordPackage); Docx4J.toPDF(wordPackage, new FileOutputStream(output)); }
}
|