Java - Word转PDF
前提:Windows平台
pom.xml
<!-- documents4j word转pdf -->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.5</version>
</dependency>
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.*;
public class PdfUtil {
/**
* word转pdf
* @param wordFilePath word文件路径
* @param pdfFilePath pdf文件路径
* @return 成功或失败
*/
public static boolean docxToPdf(String wordFilePath, String pdfFilePath) {
boolean result = false;
File inputFile = new File(wordFilePath);
File outputFile = new File(pdfFilePath);
try {
InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
outputStream.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}

浙公网安备 33010602011771号