java导出pdf报告之八:word文档转成pdf

在此我就只介绍我使用的方法:xdocreport。xdocreport适合对转换后的pdf格式要求不高的情况,我们需求中的pdf虽然内容种类和格式都挺多,但最后实现出来,基本可以满足需要,并且xdocreport使用起来确实方便。

public void makePdf(String basePath, String fileName) throws Exception {
        File docxFile = null;
        InputStream docxStream = null;
        File outFile = null;
        OutputStream out = null;
        try {
            long startTime = System.currentTimeMillis();
            docxFile = new File(basePath + fileName + ".docx");
            docxStream = new FileInputStream(docxFile);
            XWPFDocument document = new XWPFDocument(docxStream);
            outFile = new File(basePath + fileName + ".pdf");
            outFile.getParentFile().mkdirs();
            out = new FileOutputStream(outFile);
            PdfOptions options = PdfOptions.create(); // gb2312】
            PdfConverter.getInstance().convert(document, out, options);          
        } catch (Exception e) {
            logger.error("读取模板docx文件并转换pdf异常", e);
        } finally {
            try {
                if (docxStream != null) {
                    docxStream.close();
                    logger.info("读取模板转换pdf关闭docxStream流正常");
                }
            } catch (Exception e) {
                logger.error("读取模板转换pdf关闭流异常", e);
            }
            if (out != null) {
                out.close();
            }
        }
    }

对于xdocreport使用,我感觉我还处于最初级的使用,其更高级的一些设置还有待学习,也希望和大家多多交流学习.

这里遇到的主要问题就是,转换后表格边框的颜色太深、文字靠下显示,有压下边框的情况。暂时通过调整word中边框的颜色或是在文字后边加换行符\n进行了处理。如果大家谁有更好的解决方案,烦请留言交流。谢谢!

猜你喜欢

转载自blog.csdn.net/wzl19870309/article/details/103440118