java Word转PDF
时间: 2023-11-11 10:07:44 浏览: 48
在Java中将Word文档转换为PDF可以使用Apache POI和iText库。以下是一个简单的示例代码:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordToPDFConverter {
public static void main(String[] args) throws Exception {
InputStream in = WordToPDFConverter.class.getResourceAsStream("input.docx");
XWPFDocument document = new XWPFDocument(in);
File outFile = new File("output.pdf");
FileOutputStream out = new FileOutputStream(outFile);
PdfConverter.getInstance().convert(document, out, null);
}
}
```
在这个例子中,我们使用Apache POI和iText库将名为“input.docx”的Word文档转换为PDF,并将其保存为名为“output.pdf”的文件。您需要将输入文件放置在classpath中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)