Convert docx file to PDF using Java

I am looking for some β€œstable” way to convert a DOCX file from MS WORD to PDF. Since then I have used OpenOffice installed as a listener, but it often freezes. The problem is that we have situations where many users want to convert SXW, DOCX to PDF files at the same time. Is there any other possibility? I tried examples from this site: https://angelozerr.wordpress.com/2012/12/06/how-to-convert-docxodt-to-pdfhtml-with-java/ , but the result of the result is not very good (in converted documents errors and layout are much changed).

here is the "source" docx document: enter image description here

here is a document converted using docx4j with some exception text inside the document. Also missing text in the upper right corner.

enter image description here

This PDF file was created with OpenOffice as a converter from docx to pdf. Some text missing the "upper right corner"

enter image description here

Is there any other way to convert docx to pdf with Java?

+5
source share
1 answer

There are many conversion methods. One of the methods used is using POI and DOCX4j.

InputStream is = new FileInputStream(new File("your Docx PAth")); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage .load(is); List sections = wordMLPackage.getDocumentModel().getSections(); for (int i = 0; i < sections.size(); i++) { wordMLPackage.getDocumentModel().getSections().get(i) .getPageDimensions(); } Mapper fontMapper = new IdentityPlusMapper(); PhysicalFont font = PhysicalFonts.getPhysicalFonts().get( "Comic Sans MS");//set your desired font fontMapper.getFontMappings().put("Algerian", font); wordMLPackage.setFontMapper(fontMapper); PdfSettings pdfSettings = new PdfSettings(); org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion( wordMLPackage); //To turn off logger List<Logger> loggers = Collections.<Logger> list(LogManager .getCurrentLoggers()); loggers.add(LogManager.getRootLogger()); for (Logger logger : loggers) { logger.setLevel(Level.OFF); } OutputStream out = new FileOutputStream(new File("Your OutPut PDF path")); conversion.output(out, pdfSettings); System.out.println("DONE!!"); 

This works fine and is even tested on multiple DOCX files.

+2
source

Source: https://habr.com/ru/post/1261182/


All Articles