将包含图表的docx转换为PDF [英] Converting a docx containing a chart to PDF

查看:160
本文介绍了将包含图表的docx转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docx4j生成的文件,其中包含几个表,标题以及最后一个由excel生成的曲线图.

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

我尝试了许多方法来将该文件转换为PDF,但未获得任何成功的结果.

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

  1. 带有xsl-fo的docx4j无法正常工作,docx文件中包含的大多数内容尚未实现,并以红色文本显示为未实现".
  2. JODConverter也不起作用,我得到了一个生成的PDF,其中的所有内容都很好(只是很少的格式/样式问题),但是图形没有显示.
  3. 最后,最接近的方法是使用Apache POI:生成的PDF与我的docx文件相同,但仍然没有显示图表.
  4. 我已经知道Aspose可以轻松解决此问题,但是我正在寻找一种开源,免费的解决方案.

我与Apache POI一起使用的代码如下:

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());
}

我不知道如何在PDF中获取图表,有人可以告诉我如何进行吗?

I do not know what to do to get the chart inside the PDF, could anybody tell me how to proceed?

谢谢.

推荐答案

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

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

在此下载页面: 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");

这篇关于将包含图表的docx转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆