遇到word轉(zhuǎn)pdf相關(guān)的問題,記錄一下相關(guān)的使用
1 Spire
spire.doc是一款國產(chǎn)的,專業(yè)的 Java Word 組件,使用它可以輕松地將 Word 文檔創(chuàng)建、讀取、編輯、轉(zhuǎn)換和打印等功能集成到自己的 Java 應(yīng)用程序中.
其中的免費版本, 有特殊限制,在加載或保存 Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉(zhuǎn)換為 PDF 和 XPS 等格式時,僅支持轉(zhuǎn)換前三頁.
收費版本: 也可使用,首頁頁眉會出現(xiàn)警告信息,且最多支持十頁,每頁中間還有警告信息.
以免費版本演示為例:
1 添加依賴
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
2 測試
public class DemoController {
public static void main(String[] args) {
// word文檔地址
String docFile = "D:\\PublicSoftware\\jar\\2xs.docx";
// 導出pdf地址
String pdfFile = "D:\\PublicSoftware\\jar\\xs4.pdf";
//實例化Document類的對象
Document doc = new Document();
//加載Word
doc.loadFromFile(docFile);
// 開始時間
long begin = System.currentTimeMillis();
//保存為PDF格式
doc.saveToFile(pdfFile, FileFormat.PDF);
// 結(jié)束時間
long end = System.currentTimeMillis();
System.out.println("word轉(zhuǎn)pdf完成: 耗時 : "+ ((end-begin)/1000) + "秒");
}
}
// word轉(zhuǎn)pdf完成: 耗時 : 6秒
2 poi
強大的poi,在文檔處理方面,很全面,但是組件多,各種版本之間存在沖突.
查詢網(wǎng)上一個可使用的一個版本如下:
1 添加依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>2.0.1</version>
</dependency>
2 測試
public class DemoController {
public static void main(String[] args) throws IOException {
// 開始時間
long begin = System.currentTimeMillis();
String docFile = "D:\\PublicSoftware\\jar\\xs.docx";
String pdfFile = "D:\\PublicSoftware\\jar\\xs6.pdf";
InputStream doc = new FileInputStream(docFile);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(pdfFile);
PdfConverter.getInstance().convert(document, out, options);
doc.close();
out.close();
// 結(jié)束時間
long end = System.currentTimeMillis();
System.out.println("word轉(zhuǎn)pdf完成: 耗時 : "+ ((end-begin)/1000) + "秒");
}
}
// word轉(zhuǎn)pdf完成: 耗時 : 15秒
3 總結(jié)
上述兩個方法都可以將word轉(zhuǎn)為pdf文件,各自都用特點.
Spire,依賴少,但免費版本限制過多,而收費版本價格不低,直接使用又有警告信息.(使用公司郵箱申請,可免費使用一個月,但不是長久之計);文章來源:http://www.zghlxwxcb.cn/news/detail-495345.html
poi文檔處理,免費,但依賴多,且各個版本之間可能存在沖突,且對于生成pdf的一些排版,篇幅的處理,效果沒有spire好.文章來源地址http://www.zghlxwxcb.cn/news/detail-495345.html
到了這里,關(guān)于關(guān)于word轉(zhuǎn)pdf功能實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!