使用Java进行Docx转PDF格式转换

4

我需要使用Java将docx转换为pdf。 我尝试过docx4j,虽然它相当不错,但我有的docx格式比它可以处理的要复杂得多。 我决定尝试使用PDF打印机来处理它们,虽然效果不完美,但是还算可以接受。 现在我面临的问题是如何从JAVA自动化实现,唯一找到的解决方案是使用MS Word的COM(我尝试使用Open Office API,但也无法处理docx格式)。

我找到了几个Java COM映射程序,例如jacob,并尝试了它们,但是我似乎找不到适当的COM命令来打印Word文件。 我正在使用以下代码(我从网上找到的几个代码片段组合在一起):

        String sInputDoc = "fi.docx";
        boolean tVisible = false; 
        ActiveXComponent oWord = new ActiveXComponent("Word.Application"); 
        oWord.setProperty("Visible", new Variant(tVisible));
        Object oDocuments = oWord.getProperty("Documents").toDispatch(); 
        Object oDocument = Dispatch.call((Dispatch)oDocuments, "Open", sInputDoc).toDispatch();

        Dispatch oSelection = oWord.getProperty("Selection").toDispatch();
        Dispatch oFind = oWord.call(oSelection, "Find").toDispatch();
        Dispatch oWordBasic = (Dispatch) Dispatch.call(oWord, "WordBasic").getDispatch();
        Dispatch.call(oWordBasic, "FilePrint");

然而,这段代码只会使程序尝试保存文件,而不是打印(不确定为什么)。

现在我的问题是:如何将打印操作发送到Word?(我做了研究,但大多数评论基本上是打开Word并打印为PDF,但从来没有真正说明如何打印)如果有更好的选择,它们是什么?我很乐意使用任何具有免费许可证的东西。

提前感谢。


关于docx4j的更新:现在除了基于开源XSL FO的转换之外,还有一种高保真度的非FO商业替代品。请参见http://www.docx4java.org/forums/pdf-output-f27/higher-fidelity-pdf-output-now-available-t2117.html。 - JasonPlutext
2个回答

0

您可以使用docto将docx文件转换为PDF / CSV /文本。 下载.exe文件并将其放置在外部位置或项目中。

下面的代码片段包含两种将docx转换为PDF的方法,一种是使用Soffice,需要在文件系统上安装LibreOffice,另一种是Docto(仅适用于Windows机器)。

private String convertToPDF(String docPath) throws CustomException, IOException {
    try {
        File docFile = new File(docPath);
        String tempDirectory = docFile.getParent();
        String cmdLinePDFConvertionCommand = "soffice --convert-to pdf -outdir " + tempDirectory + " " + docPath;
        if (OSUtil.OS.WINDOWS.equals(OSUtil.getOS())) {
           
            cmdLinePDFConvertionCommand = "." + File.separator + tempDirectory + File.separator + "Docto -f \"" + docPath + "\" -o \"" + tempDirectory + "\" -t wdFormatPDF";
        }
        log.info("Command to convert docx to pdf: {}", cmdLinePDFConvertionCommand);
        Process process = Runtime.getRuntime().exec(cmdLinePDFConvertionCommand);
        int output = process.waitFor();
        log.info("Conversion process output: {}", output);
        String baseFileName = FilenameUtils.getBaseName(docPath);
        String pdfFilePath = tempDirectory + File.separator + baseFileName + ".pdf";
        File pdfFile = new File(pdfFilePath);
        log.info("PDF File {} Exists: {}", pdfFilePath, pdfFile.exists());
        return pdfFilePath;
    } catch (IOException | InterruptedException e) {
        log.error("Some error while converting to PDF", e);
        throw new CustomException(e.getMessage());
    } finally {
        Files.deleteIfExists(Path.of(docPath));
    }
}

0
你可以尝试使用ODF转换器和JOD转换器的组合。ODF转换器可以提供合理的DOCX到ODT的转换,而JOD转换器可以提供合理的ODT到PDF的输出。打印是另外一回事,但我猜你只需要打印是因为你之前尝试的那个特定解决方案。

你可能不需要ODF转换器; 最近的OpenOffice可以很好地打开docx文件。所以只需使用JOD转换器即可。 - JasonPlutext
内置的DocX转换到目前为止一直很差。几个月前,ODFConverter要好得多。我还没有评估最新版本。 - Paul Jowett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接