-1

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.

1 Answers1

0

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

Alexey Noskov
  • 1,722
  • 1
  • 7
  • 13