将doc文件转换为pdf
我正在使用以下代码:
POIFSFileSystem fs = null;
Document Pdfdocument = new Document();
fs = new POIFSFileSystem(new FileInputStream(srcFile));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
PdfWriter writer = PdfWriter.getInstance(Pdfdocument, new
FileOutputStream(targetFile));
Pdfdocument.open();
writer.setPageEmpty(true);
Pdfdocument.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
Pdfdocument.add(new Paragraph(paragraphs[i]));
}
这会生成一个没有格式和图像的 pdf,即使字体也会丢失。
由于WordExtractor
仅使用文本,因此还有其他方法可以使用字体和图像进行转换。转换表格 doc( HWPFDocument
) 但不在 docx 上
我已经提到了这些 SO 链接
https://stackoverflow.com/a/6210694/6032482
如何以编程方式在java中将doc,docx文件转换为pdf
还有更多,但发现他们都使用 WordExtractor 。
注意:我不能使用图书馆办公室,也不能使用 Aspose
可以使用:
阿帕奇POI
DOCX4j
文本