简体   繁体   中英

Which Java Library can help to convert an MS Word Document containing shapes and Word drawings to PDF?

I am trying to convert files with .docx extension to PDF using Java. I need to convert files with shapes and drawings in MS Word. Which libraries(open source or licensed) will serve the purpose?

Currently I have been using "org.apache.poi.xwpf.converter.pdf.PdfConverter" for the purpose, but it skips to convert the shapes or drawings in my Word Document. I am unable to test it using Aspose.words. Any help with that will also be appreciated.

The method I used for conversion is:

public static void createPDFFromIMG(String sSourceFilePath,String sFileName, String sDestinationFilePath) throws Exception {
        logger.debug("Entered into createPDFFromIMG()\n");
        logger.info("### Started PDF Conversion..");
        System.out.println("### Started PDF Conversion..");
        try {

        if(sFileName.contains(".docx")) {
            InputStream doc = new FileInputStream(new File(sSourceFilePath));
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(
                    new File(sDestinationFilePath + "/" + sFileName.split("\\.")[0] + ".pdf"));
            PdfConverter.getInstance().convert(document, out, options);
            doc.close();
            out.close();
            System.out.println("### Completed PDF Conversion..");
            logger.info("### Completed PDF Conversion..");
            logger.debug("Exited from createPDFFromIMG()");
            return;
        }
}

I expect the complete Word file to be converted to PDF, but the file converted using the mentioned Java library does not contain drawings or shapes present in the docx file.

It is not actually clear why you unable to test with Aspose.Words. Code is quite simple

Document doc = new Document("in.docx");
Doc.save("out.pdf");

Also you can test with free Aspose App (which is actually based on Aspose.Words) https://products.aspose.app/words/conversion

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