简体   繁体   English

将包含图表的docx转换为PDF

[英]Converting a docx containing a chart to PDF

I've got a docx4j generated file which contains several tables, titles and, finally, an excel-generated curve chart. 我有一个docx4j生成的文件,其中包含几个表,标题以及一个excel生成的曲线图。

I have tried many approaches in order to convert this file to PDF, but did not get to any successful result. 我尝试了许多方法来将该文件转换为PDF,但未获得任何成功的结果。

  1. Docx4j with xsl-fo did not work, most of the things included in the docx file are not yet implemented and show up in red text as "not implemented". 带有xsl-fo的docx4j无法正常工作,docx文件中包含的大多数内容尚未实现,并以红色文本显示为“未实现”。
  2. JODConverter did not work either, I got a resulting PDF in which everything was pretty good (just little formatting/styling issues) BUT the graph did not show up. JODConverter也不起作用,我得到了一个生成的PDF,其中的所有内容都很好(只是很少的格式/样式问题),但是该图没有显示。
  3. Finally, the closest approach was using Apache POI: The resulting PDF was identical to my docx file, but still no chart showing up. 最后,最接近的方法是使用Apache POI:生成的PDF与我的docx文件相同,但仍然没有显示图表。
  4. I already know Aspose would solve this pretty easily, but I am looking for an open source, free solution. 我已经知道Aspose可以很轻松地解决这个问题,但是我正在寻找一个开源,免费的解决方案。

The code I am using with Apache POI is as follows: 我与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());
}

I do not know what to do to get the chart inside the PDF, could anybody tell me how to proceed? 我不知道如何在PDF中获取图表,有人可以告诉我如何进行吗?

Thanks in advance. 提前致谢。

I don't know if this helps you but you could use "jacob" (I don't know if its possible with apache poi or docx4j) With this solution you open "Word" yourself and export it as pdf. 我不知道这是否对您有帮助,但是您可以使用“ jacob”(我不知道是否可以将其用于apache poi或docx4j)使用此解决方案,您可以自己打开“ Word”并将其导出为pdf。

! Word needs to be installed on the computer ! Word需要安装在计算机上

Heres the download-page: http://sourceforge.net/projects/jacob-project/ 此处是下载页面: 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM