Java使用Aspose實(shí)現(xiàn)office文件轉(zhuǎn)PDF(20.7)

最近一直在弄在線教學(xué)平臺,然后就需要實(shí)現(xiàn)在線預(yù)覽文件功能.之前資源庫一直用的libreoffice實(shí)現(xiàn)Office轉(zhuǎn)PDF.但是libreoffice是單線程的,作為一個在線教學(xué)平臺顯然是不靠譜的.所以今天分享另一個純Java實(shí)現(xiàn)Office轉(zhuǎn)PDF工具Aspose

  • 首先我們先引入響應(yīng)的pom配置(由于默認(rèn)maven倉庫下不下來.文末提供下載地址)
<dependency>
   <groupId>com.aspose</groupId>
   <artifactId>aspose-words</artifactId>
   <version>20.7</version>
   <classifier>jdk17</classifier>
</dependency>
<dependency>
   <groupId>com.aspose</groupId>
   <artifactId>aspose-slides</artifactId>
   <version>20.7</version>
   <classifier>jdk16</classifier>
</dependency>
<dependency>
   <groupId>com.aspose</groupId>
   <artifactId>aspose-cells</artifactId>
   <version>20.7</version>
</dependency>

采用工廠模式進(jìn)行office系列轉(zhuǎn)換

  • 新建接口而IFileConvert
public interface IFileConvert {

   /**
    * pdf文件后綴
    */
   String FILE_PDF_SUFFIX = ".pdf";

   default String getResultPath(String filePath, String fileSuffix){
       // 什么將要返回的pdf路徑
       int i = filePath.lastIndexOf(".");
       String path = filePath.substring(0, i);
       return path + fileSuffix;
   }
   
   String fileToPdf(String filePath);
}
  • 實(shí)現(xiàn)WORD文件轉(zhuǎn)換,新建WordsFileConvert
public class WordsFileConvert implements IFileConvert {

   private boolean getLicense() {
       boolean result = false;
       InputStream is = null;
       try {
           ClassLoader loader = Thread.currentThread().getContextClassLoader();
           is = loader.getResourceAsStream("license.xml");
           License license = new License();
           license.setLicense(is);
           result = true;
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (is != null) {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return result;
   }

   @Override
   public String fileToPdf(String filePath) {
       //去除水印
       if (!getLicense()) {
           return null;
       }
       String pdfPath = null;
       FileOutputStream os = null;
       try {
           pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
           os = new FileOutputStream(pdfPath);
           Document doc = new Document(filePath);
           doc.save(os, SaveFormat.PDF);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (os != null) {
               try {
                   os.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return pdfPath;
   }
}
  • 實(shí)現(xiàn)PPT文件轉(zhuǎn)換,新建SlidesFileConvert
public class SlidesFileConvert implements IFileConvert {

   private boolean getLicense() {
       boolean result = false;
       InputStream is = null;
       try {
           ClassLoader loader = Thread.currentThread().getContextClassLoader();
           is = loader.getResourceAsStream("license.xml");
           License license = new License();
           license.setLicense(is);
           result = true;
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (is != null) {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return result;
   }

   @Override
   public String fileToPdf(String filePath) {
       if (!getLicense()) {
           return null;
       }
       String pdfPath = null;
       FileOutputStream os = null;
       try {
           pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
           os = new FileOutputStream(pdfPath);
           Presentation pres = new Presentation(filePath);
           pres.save(os, SaveFormat.Pdf);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (os != null) {
               try {
                   os.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return pdfPath;
   }
}
  • 實(shí)現(xiàn)EXCEL文件轉(zhuǎn)換,新建CellsFileConvert
public class CellsFileConvert implements IFileConvert {

   private boolean getLicense() {
       boolean result = false;
       InputStream is = null;
       try {
           ClassLoader loader = Thread.currentThread().getContextClassLoader();
           is = loader.getResourceAsStream("license.xml");
           License license = new License();
           license.setLicense(is);
           result = true;
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (is != null) {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return result;
   }

   @Override
   public String fileToPdf(String filePath) {
       if (!getLicense()) {
           return null;
       }
       String pdfPath = null;
       FileOutputStream os = null;
       try {
           pdfPath = getResultPath(filePath, FILE_PDF_SUFFIX);
           os = new FileOutputStream(pdfPath);
           Workbook workbook = new Workbook(filePath);
           workbook.save(os, SaveFormat.PDF);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           if (os != null) {
               try {
                   os.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
       return pdfPath;
   }
}
  • resources目錄下新建license.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<License>
 <Data>
   <Products>
     <Product>Aspose.Total 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>
  • 新建工廠枚舉FileConvertEnum
public enum FileConvertEnum {

    PPT("PPT", new SlidesFileConvert()),
    WORD("WORD", new WordsFileConvert()),
    EXCEL("EXCEL", new CellsFileConvert()),
    ;

    private String fileFormat;
    private IFileConvert fileConvert;

    FileConvertEnum(String fileFormat, IFileConvert fileConvert) {
        this.fileFormat = fileFormat;
        this.fileConvert = fileConvert;
    }

    public String getFileFormat() {
        return fileFormat;
    }

    public IFileConvert getFileConvert() {
        return fileConvert;
    }

    public static IFileConvert getFileConvert(String fileFormat) {
        for (FileConvertEnum fileConvertEnum : values()) {
            if (fileConvertEnum.fileFormat.equals(fileFormat)){
                return fileConvertEnum.fileConvert;
            }
        }
        return null;
    }
}
  • 使用方式
public static void main(String[] args) {
   IFileConvert fileConvert = FileConvertEnum.getFileConvert("WORD");
   String pdfUri = fileConvert.fileToPdf("/home/abelethan/Desktop/test.doc"); 
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容