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

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

  • 首先我們先引入響應(yīng)的pom配置(由于默認(rèn)maven倉(cāng)庫(kù)下不下來(lái).文末提供下載地址)
<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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凝化,一起剝皮案震驚了整個(gè)濱河市揩徊,隨后出現(xiàn)的幾起案子似芝,更是在濱河造成了極大的恐慌,老刑警劉巖赐稽,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叫榕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡姊舵,警方通過(guò)查閱死者的電腦和手機(jī)晰绎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)括丁,“玉大人荞下,你說(shuō)我怎么就攤上這事∈贩桑” “怎么了尖昏?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)构资。 經(jīng)常有香客問(wèn)我抽诉,道長(zhǎng),這世上最難降的妖魔是什么吐绵? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任迹淌,我火速辦了婚禮塞帐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘巍沙。我一直安慰自己,他們只是感情好荷鼠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布句携。 她就那樣靜靜地躺著,像睡著了一般允乐。 火紅的嫁衣襯著肌膚如雪矮嫉。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天牍疏,我揣著相機(jī)與錄音蠢笋,去河邊找鬼。 笑死鳞陨,一個(gè)胖子當(dāng)著我的面吹牛昨寞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播厦滤,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼援岩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了掏导?” 一聲冷哼從身側(cè)響起享怀,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎趟咆,沒(méi)想到半個(gè)月后添瓷,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡值纱,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年鳞贷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虐唠。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡悄晃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出凿滤,到底是詐尸還是另有隱情妈橄,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布翁脆,位于F島的核電站眷蚓,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏反番。R本人自食惡果不足惜沙热,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一叉钥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧篙贸,春花似錦投队、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至寝贡,卻和暖如春扒披,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背圃泡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工碟案, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人颇蜡。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓价说,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親风秤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子熔任,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容