我可以使用 docx4j 从 docx 文件生成 pdf。但我需要将 doc 文件转换为 pdf,包括图像和表格。 有没有办法在 java 中将 doc 转换为 docx。或(doc到pdf)?
问问题
12296 次
4 回答
3
docx4j 包含 org.docx4j.convert.in.Doc,它使用 POI 来读取 .doc,但它是概念证明,而不是生产就绪代码。最后我检查了一下,POI 对二进制 .doc 的 HWPF 解析存在限制。
根据 mqchen 的评论,您可以使用 LibreOffice 或 OpenOffice 将 doc 转换为 docx。但如果你打算使用 LibreOffice 或 OpenOffice,你不妨使用它将 .doc 和 .docx 都直接转换为 PDF。谷歌'jodconverter'。
于 2013-03-26T22:06:06.240 回答
2
抄袭POI 单元测试,我想出了这个从 word 文档中提取文本:
public String getText(String document) {
try {
ZipInputStream is = new ZipInputStream(new FileInputStream(document));
try {
is.getNextEntry();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
IOUtils.copy(is, baos);
} finally {
baos.close();
}
byte[] byteArray = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
HWPFDocument doc = new HWPFDocument(bais);
extractor = new WordExtractor(doc);
extractor.getText();
} finally {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我确实希望这会为您指明正确的方向,如果不是完全分类的话。
于 2013-03-26T12:41:49.603 回答
0
您可以为此使用 jWordConvert。
jWordConvert 是一个 Java 库,可以原生读取和渲染 Word 文档以转换为 PDF、转换为图像或自动打印文档。
详细信息可以在以下链接中找到 http://www.qoppa.com/wordconvert/
于 2013-03-26T12:09:50.783 回答
0
https://github.com/guptachunky/Conversion-Work 这个 Github 链接可能对此有所帮助。
public void docToPdf(FileDetail fileDetail, HttpServletResponse response) {
InputStream doc;
try {
File docFile = converterToFile(fileDetail.getFile());
doc = new FileInputStream(docFile);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
File file = File.createTempFile("output", ".pdf");
OutputStream out = new FileOutputStream(file);
PdfConverter.getInstance().convert(document, out, options);
getClaimFiles(file, response);
} catch (IOException e) {
response.setStatus(AppConstant.SOMETHING_WENT_WRONG);
}
}
public void getClaimFiles(File file, HttpServletResponse response) {
try {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"attachment; filename=dummy.pdf");
response.getOutputStream().write(Files.readAllBytes(file.toPath()));
} catch (Exception e) {
response.setStatus(AppConstant.SOMETHING_WENT_WRONG);
}
}
于 2021-08-18T15:26:24.203 回答