"docx to pdf converter in java" Code Answer

2

the main problem with this is that those pdfoptions and pdfconverter are not part of the apache poi project. they are developed by opensagres and first versions were badly named org.apache.poi.xwpf.converter.pdf.pdfoptions and org.apache.poi.xwpf.converter.pdf.pdfconverter. those old classes were not updated since 2014 and needs version 3.9 of apache poi to be used.

do using the much more current fr.opensagres.poi.xwpf.converter.pdf, which works using the latest stable release apache poi 3.17.

then do

import java.io.inputstream;
import java.io.outputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.file;

//needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.1.jar, 
//             fr.opensagres.poi.xwpf.converter.pdf-2.0.1.jar,
//             fr.opensagres.xdocreport.itext.extension-2.0.1.jar,
//             itext-2.1.7.jar                                  
import fr.opensagres.poi.xwpf.converter.pdf.pdfoptions;
import fr.opensagres.poi.xwpf.converter.pdf.pdfconverter;

//needed jars: apache poi and it's dependencies
import org.apache.poi.xwpf.usermodel.xwpfdocument;

public class docxtopdfconvertersamplemin {

 public static void main(string[] args) throws exception {

  string docpath = "./worddocument.docx";
  string pdfpath = "./worddocument.pdf";

  inputstream in = new fileinputstream(new file(docpath));
  xwpfdocument document = new xwpfdocument(in);
  pdfoptions options = pdfoptions.create();
  outputstream out = new fileoutputstream(new file(pdfpath));
  pdfconverter.getinstance().convert(document, out, options);

  document.close();
  out.close();

 }
}

october 2018: this code works using apache poi 3.17. it cannot work using apache poi 4.0.0 due to changings in apache poi which were not taken in account in fr.opensagres.poi.xwpf.converter until now.


february 2019: works for me now using the newest apache poi version 4.0.1 and the newest version 2.0.2 of fr.opensagres.poi.xwpf.converter.core and consorts.

By Abhishek Kaushik on September 3 2022

Answers related to “docx to pdf converter in java”

Only authorized users can answer the Search term. Please sign in first, or register a free account.