Recently I have been working on an online teaching platform, and then I need to implement the online preview file function. Before the resource library has been using LibreOffice to achieve Office to PDF. But Libreoffice is single-threaded, so as an online teaching platform it is clearly not viable. So today we share another pure Java implementation of Office to PDF tool Aspose

  • So let’s first introduce the responsivepomConfiguration (The Maven repository does not come down due to default. Download address at the end of the article)
<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>
Copy the code

Office series conversion using factory mode

  • Creating an interfaceIFileConvert
public interface IFileConvert {

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

   default String getResultPath(String filePath, String fileSuffix){
       // What is the PDF path to return
       int i = filePath.lastIndexOf(".");
       String path = filePath.substring(0, i);
       return path + fileSuffix;
   }
   
   String fileToPdf(String filePath);
}
Copy the code
  • implementationWORDFile conversion, newWordsFileConvert
public class WordsFileConvert implements IFileConvert {

   private boolean getLicense(a) {
       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) {
   	// Remove the watermark
       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(); }}}returnpdfPath; }}Copy the code
  • implementationPPTFile conversion, newSlidesFileConvert
public class SlidesFileConvert implements IFileConvert {

   private boolean getLicense(a) {
       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(); }}}returnpdfPath; }}Copy the code
  • implementationEXCELFile conversion, newCellsFileConvert
public class CellsFileConvert implements IFileConvert {

   private boolean getLicense(a) {
       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(); }}}returnpdfPath; }}Copy the code
  • inresourcesCreating a Directorylicense.xmlfile

      
<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/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6 EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
Copy the code
  • Enumeration of new factoriesFileConvertEnum
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(a) {
        return fileFormat;
    }

    public IFileConvert getFileConvert(a) {
        return fileConvert;
    }

    public static IFileConvert getFileConvert(String fileFormat) {
        for (FileConvertEnum fileConvertEnum : values()) {
            if (fileConvertEnum.fileFormat.equals(fileFormat)){
                returnfileConvertEnum.fileConvert; }}return null; }}Copy the code
  • use
public static void main(String[] args) {
	IFileConvert fileConvert = FileConvertEnum.getFileConvert("WORD");
	String pdfUri = fileConvert.fileToPdf("/home/abelethan/Desktop/test.doc"); 
}
Copy the code