简体   繁体   中英

Docx to PDF conversion in java

I need to be able to convert docx to pdf using java. I tried using docx4j but although it is quite good, the docx I have are much more complex (formatting) than what it can handle. I decided to see if a PDF printer could handle them and although their not perfect, they're acceptable. 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).

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. 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? (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? 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 Converter can provide reasonable DOCX to ODT conversion and JOD Converter can provide reasonable ODT to pdf output. Printing is something else, but I gather you were only needing to print because of that particular solution you were trying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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