最近,需要實現在linux服務器上將Word文檔轉成PDF文檔的功能,接手其他人項目使用的是Jacob,但是需要往jdk里面添加文件,所以想換一個方法實現,根據前者和相關資料決定使用的aspose,因此記錄一下使用這個第三方組件的步驟。
一、環境搭建
1、首先需要下載一個aspose插件jar包放進項目中,使用的IDEA,jar包可以在網盤下載:
鏈接:https://pan.baidu.com/s/1jISO-TPEyLgC8RTmMJGRQw 提取碼:9ju8
2、下載好所需要的jar包,idea需要引入jar包,從編譯的層面考慮將將jar包安裝到本地倉庫,解決編譯打包時出錯的問題。
A.首先確定 mvn -v 能否使用,將下載好的jar包放到項目外的本地文件夾。
B.其次執行mvn install 安裝本地jar包到本地倉庫,如下所示:
mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar -Dfile=aspose-words-15.8.0-jdk16.jar
執行完成后可到本地倉庫查看是否有這個包存在即可。
3、在項目中添加本地倉庫的依賴:
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>15.8.0</version> </dependency>
二、工具類編寫和測試
1、在項目靜態資源路徑下添加一個license.xml文件,不然生成的pdf會有水印
<?xml version="1.0" encoding="UTF-8" ?> <License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature> </License>
2、添加Word2PdfAsposeUtil工具類
創建pdf文件包:pdfbox.jar
word轉pdf包:aspose.jar
import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; public class Word2PdfAsposeUtil {
/**
* 水印驗證
* @return
*/
public static boolean getLicense() {
boolean result = false; InputStream is = null; try { /*Resource resource = new ClassPathResource("license.xml"); is = resource.getInputStream();*/ //InputStream is = Word2PdfAsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); is = Word2PdfAsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml應放在..\WebRoot\WEB-INF\classes路徑下 License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); }finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
/**
* word轉換pdf
* @param inPath
* @param outPath
* @return
*/
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 return false; } FileOutputStream os = null; try { long old = System.currentTimeMillis(); File file = new File(outPath); // 新建一個空白pdf文檔 os = new FileOutputStream(file); Document doc = new Document(inPath); // Address是將要被轉化的word文檔 doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互轉換 long now = System.currentTimeMillis(); System.out.println("pdf轉換成功,共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時 } catch (Exception e) { e.printStackTrace(); return false; }finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; }
/**
* 創建pdf文件
* @param pdfPath
*/
public static void createPdf(String pdfPath) { // Creating PDF document object PDDocument document = new PDDocument(); // Add an empty page to it document.addPage(new PDPage()); // Saving the document try { document.save(pdfPath); //document.save("E:/BlankPdf.pdf"); // Closing the document document.close();//原文出自【易百教程】,商業轉載請聯系作者獲得授權,非商業請保留原文鏈接:https://www.yiibai.com/javaexamples/create_empty_pdf_document.html } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("PDF created"); } public static void main(String[] arg){ String docPath = "E:\\測試word.docx"; String pdfPath = "E:\\1.pdf"; //Word2PdfAsposeUtil.doc2pdf(docPath,pdfPath); //createPdf(); String str ="232ljsfsf.sdfl23.ljsdfsdfsdfss.23423.sdfsdfsfd"; //獲得第一個點的位置 int index=str.indexOf("."); System.out.println(index); //根據第一個點的位置 獲得第二個點的位置 //index=str.indexOf(".", index+1); //根據第二個點的位置,截取 字符串。得到結果 result String result=str.substring(0,index); //輸出結果 System.out.println(result); } }
3、后續可直接調用該工具類的方法即可實現Word轉Pdf的功能。
然后在把從網盤下載的jacob-1.17-M2-x64.dll放到jdk中的bin目錄下
附:jar包下載地址 https://pan.baidu.com/s/1_oqKKeU1S1y4ff0V_0utcg 提取碼:uy63
轉載:https://www.cnblogs.com/adolph2715/p/14346140.html