Converting a docx containing a chart to PDF

冷暖自知 提交于 2019-12-14 02:21:43

问题


I've got a docx4j generated file which contains several tables, titles and, finally, an excel-generated curve chart.

I have tried many approaches in order to convert this file to PDF, but did not get to any successful result.

  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".
  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.
  3. Finally, the closest approach was using Apache POI: The resulting PDF was identical to my docx file, but still no chart showing up.
  4. I already know Aspose would solve this pretty easily, but I am looking for an open source, free solution.

The code I am using with Apache POI is as follows:

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?

Thanks in advance.


回答1:


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.

!Word needs to be installed on the computer!

Heres the download-page: 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");


来源:https://stackoverflow.com/questions/30619098/converting-a-docx-containing-a-chart-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!