Bootstrap

java实现office转pdf文件

java实现office文件转pdf文件

一、jar包下载和依赖引入
依赖jar百度云地址:https://pan.baidu.com/s/16w7k3X4yifDEvufKAq5R_g
提取码:pktr

		<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>ToPDF</artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/aspose-cells-20.7.jar</systemPath>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>worldToPDF</artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/aspose-words-18.5.0718-jdk16.jar</systemPath>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>ppt2Pdf</artifactId>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/aspose-slides-19.6.jar</systemPath>
            <version>1.0</version>
        </dependency>

二、ExcelGOPdfUtil 工具类

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * excel转pdf工具类
 */
public class Excel2PdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {
//            InputStream is = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            String license =
                    "<License>\n" +
                            "  <Data>\n" +
                            "    <Products>\n" +
                            "      <Product>Aspose.Total for Java</Product>\n" +
                            "      <Product>Aspose.Words for Java</Product>\n" +
                            "    </Products>\n" +
                            "    <EditionType>Enterprise</EditionType>\n" +
                            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                            "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                            "  </Data>\n" +
                            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                            "</License>";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
     */
    public static Boolean convertToPdf(String excelFilePath, String pdfFilePath){
        //验证License
        System.out.println(excelFilePath + " -> " + pdfFilePath);
        if (!getLicense()){
            return false;
        }
        try {
            Workbook wb = new Workbook(excelFilePath);
            File pdfFile = new File(pdfFilePath);
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
//            wb.save(fileOS, SaveFormat.PDF);
//            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(fileOS, pdfSaveOptions);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Boolean excelConvertToPdf(InputStream inputStream,OutputStream os){
        //验证License
        if (!getLicense()){
            return false;
        }
        try {
            Workbook wb = new Workbook(inputStream);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
//            wb.save(fileOS, SaveFormat.PDF);
//            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(os, pdfSaveOptions);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

三、WordGOPdfUtil 工具类

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

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Version 1.0
 * @Desc:
 */
public class World2PdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {
            String license =
                    "<License>\n" +
                            "  <Data>\n" +
                            "    <Products>\n" +
                            "      <Product>Aspose.Total for Java</Product>\n" +
                            "      <Product>Aspose.Words for Java</Product>\n" +
                            "    </Products>\n" +
                            "    <EditionType>Enterprise</EditionType>\n" +
                            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                            "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                            "  </Data>\n" +
                            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                            "</License>";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
            if(is!=null){
                License aposeLic = new License();
                aposeLic.setLicense(is);
                result = true;
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean docTopdf(String inPath, String outPath)  {
        boolean ret=false;
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
            return ret;
        }
        System.out.println(inPath + " -> " + outPath);
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath);
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // word文档
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return ret;
        }
    }

    public static boolean docTopdf(InputStream inputStream, OutputStream outputStream)  {
        boolean result=false;
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
            return result;
        }
        try {
            long old = System.currentTimeMillis();
            Document nodes = new Document(inputStream);
            nodes.save(outputStream,SaveFormat.PDF);
            System.out.println("convert word2pdf OK! " + ((System.currentTimeMillis() - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return true;
    }

四、WordGOPdfUtil 工具类

import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Xiaoyu
 * @data 2021/8/10 15:45
 * @Version 1.0
 * @Desc:
 */
public class PPT2PdfUtil {
    public static boolean getLicense(){
        boolean result = false;
        try {
            String license =
                    "<License>\n" +
                            "  <Data>\n" +
                            "    <Products>\n" +
                            "      <Product>Aspose.Total for Java</Product>\n" +
                            "      <Product>Aspose.Words for Java</Product>\n" +
                            "    </Products>\n" +
                            "    <EditionType>Enterprise</EditionType>\n" +
                            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                            "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                            "  </Data>\n" +
                            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                            "</License>";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static boolean pptxTopdf(String sourcePath, String savePath){
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
//            throw new Exception("com.aspose.words lic ERROR!");
            return false;
        }
        try {
            //读取ppt文件
            FileInputStream fileInput = new FileInputStream(sourcePath);
            Presentation pres = new Presentation(fileInput);
            //指定输出路径
            FileOutputStream outputStream = new FileOutputStream(new File(savePath));
            //输出
            pres.save(outputStream, SaveFormat.Pdf);
            outputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean pptx2pdf(InputStream inputStream, OutputStream outputStream){
        boolean result = false;
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
            return result;
        }
        try {
            long old=System.currentTimeMillis();
            Presentation pres = new Presentation(inputStream);
            pres.save(outputStream, SaveFormat.Pdf);
            System.out.println("convert pptx2pdf OK! " + ((System.currentTimeMillis() - old) / 1000.0) + "秒");
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }
        return true;
    }
;