1.在項目中有這樣的需求就是,對一些word、excel、ppt文檔進行預覽,但是這個無法直接預覽,這邊可以實現這么個思路,就是先將這些文件轉換為pdf然后就可以預覽了
2.基於以上思路這邊將工具類的邏輯梳理一下:首先maven需要導入的依賴如下:由於這個jar包導不進來就本地導進去了,有興趣的朋友可以討論下,我這個包就是導不進去,去maven官網寫對應的版本也沒用,暫時這樣寫大佬勿噴;
lib包在這里下載
鏈接:https://pan.baidu.com/s/1m2LcUWO5lNDrG2-2Uq_UGQ
提取碼:6gxr
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>18.6</version> <scope>system</scope> <systemPath>${project.basedir}/lib/aspose-words-18.6-jdk16.jar</systemPath> </dependency> <!-- ppt轉pdf --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-slides</artifactId> <version>15.9.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/aspose-slides-15.9.0.jar</systemPath> </dependency> <!-- excel轉pdf --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cells</artifactId> <version>8.5.2</version> <scope>system</scope> <systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath> </dependency>
本地的jar放再跟src同一級別下:
然后就是工具類的梳理了:就不多說直接上代碼可以自己測試看看:
package file; import com.aspose.cells.Workbook; import com.aspose.slides.Presentation; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; class FileTransForUtils { private static final Logger logger = LoggerFactory.getLogger(FileTransForUtils.class); //word轉PDF public synchronized static boolean word3Pdf(String wordPath, String pdfPath) { if (!getLicense("word")) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 return false; } try { long old = System.currentTimeMillis(); File file = new File(pdfPath); //新建一個pdf文檔 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(wordPath); //Address是將要被轉化的word文檔 doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, // XPS, SWF 相互轉換 long now = System.currentTimeMillis(); os.close(); logger.info("word共耗時:" + ((now - old) / 1000.0) + "秒"); //轉化用時 return true; } catch (Exception e) { logger.error(String.valueOf(e)); e.printStackTrace(); return false; } } //excel轉PDF public synchronized static boolean excel3pdf(String excelPath, String pdfPath) { if (!getLicense("excel")) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 return false; } try { long old = System.currentTimeMillis(); File pdfFile = new File(pdfPath); //新建一個pdf文檔 FileOutputStream os = new FileOutputStream(pdfFile); Workbook wb = new Workbook(excelPath);// 原始excel路徑 wb.save(os,com.aspose.cells.SaveFormat.PDF); long now = System.currentTimeMillis(); os.close(); logger.info("excel共耗時:" + ((now - old) / 1000.0) + "秒"); //轉化用時 return true; } catch (Exception e) { logger.error(String.valueOf(e)); e.printStackTrace(); return false; } } //ppt轉PDF public synchronized static boolean ppt3pdf(String pptPath, String pdfPath) { // 驗證License if (!getLicense("ppt")) { return false; } FileOutputStream os = null; try { long old = System.currentTimeMillis(); File pdfFile = new File(pdfPath); //新建一個pdf文檔 os = new FileOutputStream(pdfFile); Presentation pres = new Presentation(pptPath);//輸入ppt路徑 //IFontsManager fontsManager = pres.getFontsManager(); pres.save(os,com.aspose.slides.SaveFormat.Pdf); long now = System.currentTimeMillis(); logger.info("ppt共耗時:" + ((now - old) / 1000.0) + "秒"); //轉化用時 return true; } catch (Exception e) { logger.error(String.valueOf(e)); e.printStackTrace(); return false; }finally { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } //剔除水印 private static boolean getLicense(String type) { boolean result = false; try { // 憑證 String license = "<License>\n" + " <Data>\n" + " <Products>\n" + " <Product>Aspose.Total for Java</Product>\n" + " <Product>Aspose.Words for Java</Product>\n" + " </Products>\n" + " <EditionType>Enterprise</EditionType>\n" + " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" + " <LicenseExpiry>20991231</LicenseExpiry>\n" + " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" + " </Data>\n" + " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" + "</License>"; InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8")); if(type.equals("word")){ License asposeLic = new License(); asposeLic.setLicense(is); }else if (type.equals("excel")){ com.aspose.cells.License asposeLic = new com.aspose.cells.License(); asposeLic.setLicense(is); }else if (type.equals("ppt")){ com.aspose.slides.License aposeLic = new com.aspose.slides.License(); aposeLic.setLicense(is); } result = true; } catch (Exception e) { logger.error(String.valueOf(e)); e.printStackTrace(); return false; } return result; } /** * 判斷資源類型文檔類 */ private static String getResourceTypesDocument(String suffix) { String type = null; switch (suffix) { //文檔類型 case ".doc": case ".docx": case ".txt": type = "word"; break; case ".xls": case ".xlsx": type = "excel"; break; case ".ppt": case ".pptx": type = "ppt"; break; } return type; } public static void main(String[] args) { String inputPath = "E:/test/測試.xlsx"; String outputPath = "E:/test/11.pdf"; String suffix = inputPath.substring(inputPath.lastIndexOf(".")); String type = getResourceTypesDocument(suffix); if("word".equals(type)){ word3Pdf(inputPath,outputPath); }else if("excel".equals(type)){ excel3pdf(inputPath,outputPath); }else if("ppt".equals(type)){ ppt3pdf(inputPath,outputPath); } } }
目前支持doc、docx、txt、xls、xlsx、ppt以及pptx轉換為pdf其他格式可能需要調整下有深入研究的大佬歡迎騷擾:以上功能自測是完全沒有問題的,目前研究應該都是皮毛很多深入的還待研究
下一篇文章將介紹音頻的轉換為MP4格式,便於前端能預覽視頻