?1.背景:
通過spire.doc.free將word轉(zhuǎn)換成PDF時存在缺陷:只能獲取前3頁。獲取全文另外需支付費用。
2.解決辦法
使用documents4j,documents4j會保留原word文件中更多的樣式,如修訂模式下的差異化字體顏色、文檔右側(cè)修訂記錄等。
3.具體步驟
1.引入Pom
<dependency>
? ? <groupId>com.documents4j</groupId>
? ? <artifactId>documents4j-local</artifactId>
? ? <version>1.0.3</version>
</dependency>
<dependency>
? ? <groupId>com.documents4j</groupId>
? ? <artifactId>documents4j-transformer-msoffice-word</artifactId>
? ? <version>1.0.3</version>
</dependency>
2.??xml2pdf方法如下,xmlpath是xml文件地址,pdfPath是生成的pdf地址。文章來源:http://www.zghlxwxcb.cn/news/detail-832570.html
public void xml2pdf(String xmlPath,String pdfPath) throws IOException {
// 參考:https:
//blog.csdn.net/ka3p06/article/details/125476270 通過documents4j實現(xiàn)
InputStream docxInputStream = null;
OutputStream outputStream = null;
try {
// 原word地址
docxInputStream = new FileInputStream(xmlPath);
// 轉(zhuǎn)換后pdf生成地址
outputStream = new FileOutputStream(pdfPath);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.XML)
.to(outputStream)
.as(DocumentType.PDF).execute();
// 關閉
converter.shutDown();
// 關閉
outputStream.close();
// 關閉
docxInputStream.close();
} catch (Exception e) {
System.out.println("[documents4J] word轉(zhuǎn)pdf失敗:" + e.toString());
} finally {
if (outputStream != null) {
outputStream.close();
}
if (docxInputStream != null) {
docxInputStream.close();
}
}
}
4. documents4j也可以把word轉(zhuǎn)為pdf
只需改如下文章來源地址http://www.zghlxwxcb.cn/news/detail-832570.html
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
到了這里,關于java 使用documents4j將XML轉(zhuǎn)為pdf文件的方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!