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.
June 2021:
Works using apache poi
version 4.1.2
and the newest version 2.0.2
of fr.opensagres.poi.xwpf.converter.core and consorts.
Cannot work using apache poi
version 5.0.0
because XDocReport
needs ooxml-schemas
which apache poi 5
does not support anymore.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…