简体   繁体   English

java中的Docx到PDF转换

[英]Docx to PDF conversion in java

I need to be able to convert docx to pdf using java. 我需要能够使用java将docx转换为pdf。 I tried using docx4j but although it is quite good, the docx I have are much more complex (formatting) than what it can handle. 我尝试使用docx4j但是虽然它非常好,但我拥有的docx比它能处理的要复杂得多(格式化)。 I decided to see if a PDF printer could handle them and although their not perfect, they're acceptable. 我决定看看PDF打印机是否可以处理它们,虽然它们并不完美,但它们是可以接受的。 Now I am facing a problem of how to automated from JAVA and the only solution I found was COM with MS Word (I tried using Open Office API it also was unable to handle the docx formatting). 现在我面临着如何从JAVA自动化的问题,我找到的唯一解决方案是COM与MS Word(我尝试使用Open Office API,它也无法处理docx格式)。

I found several COMs mapping for java such as jacob and tried them but I can't seem to find the appropriate COM commands for the Word to print. 我找到了几个用于java的COM映射,例如jacob并尝试了它们,但我似乎无法找到适合Word打印的COM命令。 Code I am using (I stack several pieces I found over the web together) is: 我正在使用的代码(我在网上找到了几件我在网上找到的代码)是:

        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");

This code however only causes the program to try and save the file and not print (not sure why). 但是,此代码仅导致程序尝试保存文件而不打印(不确定原因)。

Now may questions are: How to send the operation of print to Word? 现在可能有的问题是:如何将打印操作发送到Word? (I did my research but most of the time the comments are basically open word and print to pdf, but never HOW to actually print) If there are better options, what are they? (我做了我的研究,但大多数时候评论基本上是开放的单词和打印到pdf,但从未如何实际打印)如果有更好的选择,它们是什么? I am happy to work with anything that is has a free License. 我很高兴与任何有免费许可证的人合作。

Thanks in advance. 提前致谢。

You could try a combination of ODF Converter and JOD Converter. 您可以尝试ODF转换器和JOD转换器的组合。 ODF Converter can provide reasonable DOCX to ODT conversion and JOD Converter can provide reasonable ODT to pdf output. ODF转换器可以为ODT转换提供合理的DOCX,JOD转换器可以为pdf输出提供合理的ODT。 Printing is something else, but I gather you were only needing to print because of that particular solution you were trying. 打印是另一回事,但我认为你只需要打印,因为你正在尝试的特定解决方案。

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

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