Word、Excel、Ppt轉成PDF文件,Java代碼和小小的demo

 

首先是word轉pdf,這個是可行的,沒有水印,沒有

Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd這段文字,可以用的,後面我會給你們貼圖,把那個license.xml文件放到同個包下面就可以了,如圖所示:

如果是spring boot Web項目,就放到resources裏面,同樣如圖所示:

當然本文教程主要是一般的java項目,所以根據圖一結構放置,直接放在同個包下就好了

WordToPDF.java

import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class WordToPDF {

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = WordToPDF.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void word2pdf(String docPath, String pdfPath) {
        if (!getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();

            com.aspose.words.Document document = new com.aspose.words.Document(docPath);
            document.save(new FileOutputStream(new File(pdfPath)), SaveFormat.PDF);

            long now = System.currentTimeMillis();
            System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


ExcelToPDF.java

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class ExcelToPDF {

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ExcelToPDF.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void excel2pdf(String xlsPath, String pdfPath){
        if (!getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();

            Workbook wb = new Workbook(xlsPath);// 原始excel路徑
            wb.save(new FileOutputStream(new File(pdfPath)),SaveFormat.PDF);

            long now = System.currentTimeMillis();
            System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

PptToPDF.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class PptToPDF {

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = PptToPDF.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void ppt2pdf(String pptPath, String pdfPath){
        if (!getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();

            Presentation pres = new Presentation(pptPath);
            pres.save(new FileOutputStream(new File(pdfPath)),SaveFormat.Pdf);

            long now = System.currentTimeMillis();
            System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

裏面三種轉換格式分別對應三個jar包,所以我用了三個類來封裝,簡單封裝了一下,稍後我會把三個jar包都傳上去吧,單獨word轉pdf的jar包鏈接:word轉pdf的jar包下載點擊此處,三個jar包和小demo一起下載鏈接全套demo下載,無水印,在我的資源裏面都有的

下面是證據圖片

轉換後

不過excel在列比較多的時候,會分成兩頁,很醜,所以excel列最好不要太多,不然醜得不行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章