代碼:
package com.word;
import java.io.*;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
/**
*@author:WHW
*@CreateTime:2019年6月2日
**/
public class WordTopdf {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) {
long startime=System.currentTimeMillis();
String docPath = "E:\\桌面\\LMIS流程發起.docx";
String pdfPath = "D:\\htmTopdf\\LMIS流程發起.pdf";
try {
XWPFDocument document;
InputStream doc = new FileInputStream(docPath);
document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(pdfPath);
PdfConverter.getInstance().convert(document, out, options);
System.out.println("恭喜你終於word->pdf 轉換成功了!");
doc.close();
out.close();
System.out.println("耗時:"+(System.currentTimeMillis()-startime));
} catch (IOException e) {
e.printStackTrace();
}
}
}
pom.xml 使用的依賴
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.4</version>
</dependency>