将包含图表的docx转换为PDF



我得到了一个docx4j生成的文件,其中包含几个表格、标题,最后还有一个excel生成的曲线图。

我已经尝试了很多方法来把这个文件转换成PDF,但是没有得到任何成功的结果。

  1. Docx4j with xsl-fo没有工作,docx文件中包含的大部分内容尚未实现,并以红色文本显示为"未实现"。
  2. JODConverter也不工作,我得到了一个结果PDF,其中一切都很好(只是一点格式/样式问题),但图表没有显示。最后,最接近的方法是使用Apache POI:生成的PDF与我的docx文件相同,但仍然没有显示图表。
  3. 我已经知道Aspose会很容易地解决这个问题,但我正在寻找一个开源的,免费的解决方案。

我使用Apache POI的代码如下:

public static void convert(String inputPath, String outputPath)
        throws XWPFConverterException, IOException {
    PdfConverter converter = new PdfConverter();
    converter.convert(new XWPFDocument(new FileInputStream(new File(
            inputPath))), new FileOutputStream(new File(outputPath)),
            PdfOptions.create());
}

我不知道该怎么做才能得到PDF里面的图表,有人能告诉我怎么做吗?

我不知道这是否对你有帮助,但你可以使用"jacob"(我不知道它是否可能与apache poi或docx4j)使用此解决方案,您可以自己打开"Word"并将其导出为pdf。

!需要在计算机上安装Word !

下载页面:http://sourceforge.net/projects/jacob-project/

try {           
        if (System.getProperty("os.arch").contains("64")) {
            System.load(DLL_64BIT_PATH);
        } else {
            System.load(DLL_32BIT_PATH);
        }
    } catch (UnsatisfiedLinkError e) {
        //TODO          
    } catch (IOException e) {
        //TODO          
    }
 ActiveXComponent oleComponent = new ActiveXComponent("Word.Application");
 oleComponent.setProperty("Visible", false);
 Variant var = Dispatch.get(oleComponent, "Documents");
 Dispatch document = var.getDispatch();
 Dispatch activeDoc = Dispatch.call(document, "Open", fileName).toDispatch();
// https://msdn.microsoft.com/EN-US/library/office/ff845579.aspx
Dispatch.call(activeDoc, "ExportAsFixedFormat", new Object[] { "path to pdfFile.pdf", new Integer(17), false, 0 });
Object args[] = { new Integer(0) };//private static final int DO_NOT_SAVE_CHANGES = 0;
Dispatch.call(activeDoc, "Close", args); 
Dispatch.call(oleComponent, "Quit");

最新更新