1.代码比较简单,本人踩的坑是 在转换的时候 报空指针 :先写错误:Numbering ,在写代码
org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:59)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:37)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
at com.ys.test.WordToPdf.wordConverterToPdf(WordToPdf.java:85)
at com.ys.test.WordToPdf.main(WordToPdf.java:116)
Caused by: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getXWPFNum(XWPFDocumentVisitor.java:468)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.getNumPr(XWPFDocumentVisitor.java:335)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitParagraph(XWPFDocumentVisitor.java:280)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:242)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:194)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55)
... 4 more
问题产生的原因:word 有问题(我用的是自己用poi合成的word,后面解决了:链接)
2.代码功能:word转pdf,转换同时可以替换word中的文本(参考链接:没找到了,嘿嘿嘿~~~·)
/**
* 将word文档, 转换成pdf, 中间替换掉变量
*
* @param source
* 源为word文档, 必须为docx文档
* @param target
* 目标输出
* @param params
* 需要替换的变量
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source, OutputStream target, Map<String, String> params)
throws Exception {
wordConverterToPdf(source, target, null, params);
}
/**
* 将word文档, 转换成pdf, 中间替换掉变量
*
* @param source
* 源为word文档, 必须为docx文档
* @param target
* 目标输出
* @param params
* 需要替换的变量
* @param options
* PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source, OutputStream target, PdfOptions options,
Map<String, String> params) throws Exception {
XWPFDocument doc = new XWPFDocument(source);
paragraphReplace(doc.getParagraphs(), params);
for (XWPFTable table : doc.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
paragraphReplace(cell.getParagraphs(), params);
}
}
}
// 中文字体处理
options.fontProvider(new IFontProvider() {
public Font getFont(String familyName, String encoding, float size, int style, Color color) {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, size, style, color);
if (familyName != null)
fontChinese.setFamily(familyName);
return fontChinese;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
});
PdfConverter.getInstance().convert(doc, target, options);
}
/** 替换段落中内容 */
private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
if (params != null && !params.isEmpty()) {
for (XWPFParagraph p : paragraphs) {
for (XWPFRun r : p.getRuns()) {
String content = r.getText(r.getTextPosition());
if (StringUtils.isNotEmpty(content) && params.containsKey(content)) {
r.setText(content.replace("keyword", params.get(content)), 0);
}
}
}
}
}