java生成word文件转pdf文件的代码
时间: 2023-04-05 11:02:58 浏览: 211
您好,以下是Java生成Word文件转PDF文件的代码示例:
```
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PDFConverter;
import org.apache.poi.hwpf.usermodel.Range;
public class WordToPdfConverter {
public static void main(String[] args) {
String wordFilePath = "D:\\test.doc";
String pdfFilePath = "D:\\test.pdf";
try {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(wordFilePath));
Document pdfDocument = new Document();
OutputStream pdfFileOutputStream = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(pdfDocument, pdfFileOutputStream);
pdfDocument.open();
Range range = wordDocument.getRange();
pdfDocument.add(new Paragraph(range.text()));
pdfDocument.close();
pdfFileOutputStream.close();
wordDocument.close();
System.out.println("Word文件转PDF文件成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
希望能对您有所帮助。
阅读全文