本帖最后由 两生花开 于 2018-8-25 09:28 编辑
private static final String FONT_DIR = "/file/font/simsun.ttc,0"; // linux上可能没有中文字体库
public static void word2PDF(XWPFDocument doc, OutputStream target) {
try {
PdfOptions options = PdfOptions.create();
options.fontProvider((s, s1, v, i, color) -> {
try {
com.lowagie.text.pdf.BaseFont chinese =
com.lowagie.text.pdf.BaseFont.createFont(FONT_DIR, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return new com.lowagie.text.Font(chinese, v, i, color);
} catch (com.lowagie.text.DocumentException | IOException e) {
e.printStackTrace();
}
return com.lowagie.text.FontFactory.getFont(s, s1, v, i, color);
});
PdfConverter.getInstance().convert(doc, target, options);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(doc);
}
}
maven依赖:
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.poi.xwpf.converter.pdf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
<version>2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
之前写的word转pdf |