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列最好不要太多,不然丑得不行

打赏二维码,多谢支持

猜你喜欢

转载自blog.csdn.net/gonepoo/article/details/93975139