java word excel ppt 图片转pdf

第一步将jar导入mvn库
下载地址 0积分
https://download.csdn.net/download/qq_35908944/18549670

mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-words-18.6-jdk16.jar
mvn install:install-file -DgroupId=com.itextpdf -DartifactId=itextpdf-itextpdf -Dversion=5.5.6 -Dpackaging=jar -Dfile=E:/wordpdf/itextpdf-5.5.6.jar
mvn install:install-file -DgroupId=com.aspose.slides -DartifactId=aspose-slides -Dversion=15.9.0 -Dpackaging=jar -Dfile=E:/wordpdf/aspose.slides-15.9.0.jar
mvn install:install-file -DgroupId=com.cells -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-cells-8.5.2.jar
mvn install:install-file -DgroupId=com.itext -DartifactId=itext-asian -Dversion=5.2.0 -Dpackaging=jar -Dfile=E:/wordpdf/itext-asian-5.2.0.jar
mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=E:/wordpdf/aspose-words-15.8.0-jdk16.jar

<!-- word转pdf -->
		<dependency>
			<groupId>com.aspose</groupId>
			<artifactId>aspose-words</artifactId>
			<version>18.8</version>
		</dependency>

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.6</version>
		</dependency>

		<dependency>
			<groupId>com.aspose.slides</groupId>
			<artifactId>aspose-slides</artifactId>
			<version>15.9.0</version>
		</dependency>

		<dependency>
			<groupId>com.cells</groupId>
			<artifactId>aspose-cells</artifactId>
			<version>8.5.2</version>
		</dependency>

		<dependency>
			<groupId>com.itext</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>

上代码

import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;

/**
 * @author Administrator
 * @USER:小清新
 * @date: 2021年05月08日 16:02
 * @PROJECT_NAME: CGCP
 **/

public class PdfUtil {
    public  File Pdf(ArrayList<String> imageUrllist,
                           String mOutputPdfFileName) {
        //Document doc = new Document(PageSize.A4, 20, 20, 20, 20); // new一个pdf文档
        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        try {
            // pdf写入
            PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
            // 打开文档
            doc.open();
            // 循环图片List,将图片加入到pdf中
            for (int i = 0; i < imageUrllist.size(); i++) {
                // 在pdf创建一页
                doc.newPage();
                // 通过文件路径获取image
                Image png1 = Image.getInstance(imageUrllist.get(i));
                float heigth = png1.getHeight();
                float width = png1.getWidth();
                int percent = getPercent2(heigth, width);
                png1.setAlignment(Image.MIDDLE);
                // 表示是原来图像的比例;
                png1.scalePercent(percent + 3);
                doc.add(png1);
            }
            doc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File mOutputPdfFile = new File(mOutputPdfFileName); // 输出流
        if (!mOutputPdfFile.exists()) {
            mOutputPdfFile.deleteOnExit();
            return null;
        }
        return mOutputPdfFile; // 反回文件输出流
    }

    public  int getPercent(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        if (h > w) {
            p2 = 297 / h * 100;
        } else {
            p2 = 210 / w * 100;
        }
        p = Math.round(p2);
        return p;
    }

    public  int getPercent2(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 530 / w * 100;
        p = Math.round(p2);
        return p;
    }


    /**
     * 图片文件转PDF
     * @param filepath
     * @param request
     * @return
     */
    public static String imgOfPdf(String filepath, HttpServletRequest request) {
        boolean result = false;
        String pdfUrl = "";
        String fileUrl = "";
        try {
            // 图片list集合
            ArrayList<String> imageUrllist = new ArrayList<String>();
            imageUrllist.add(request.getSession().getServletContext()
                    // 添加图片文件路径
                    .getRealPath(File.separator + filepath));
            String fles = filepath.substring(0, filepath.lastIndexOf("."));
            // 输出pdf文件路径
            pdfUrl = request.getSession().getServletContext()
                    .getRealPath(File.separator +fles + ".pdf");
            fileUrl =fles+".pdf";
            result = true;
            // 生成pdf
            if (result) {
                PdfUtil pdfUtil=new PdfUtil();
                File file = pdfUtil.Pdf(imageUrllist, pdfUrl);
                file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileUrl;
    }




	/*public static void doc2pdf(String Address, String outPath) {
		if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
			return;
		}
		try {
			long old = System.currentTimeMillis();
			File file = new File(outPath); // 新建一个空白pdf文档
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(Address); // Address是将要被转化的word文档
			doc.save(
					os,
					SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
			// OpenDocument, PDF, EPUB, XPS, SWF
			// 相互转换
			long now = System.currentTimeMillis();
			System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

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


    public  String docOfPdf(String filePath, HttpServletRequest request) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return "PDF格式转化失败";
        }
        try {
            long old = System.currentTimeMillis();
            String filePath1 = filePath;
            String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));
            String pdfPathString = filePath2+".pdf";
            // 输出pdf文件路径
            // 新建一个空白pdf文档
            File file = new File(filePath2+".pdf");
            FileOutputStream os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(filePath1);
            // 全面支持DOC, DOCX, OOXML, RTF HTML,
            doc.save(os, SaveFormat.PDF);
            // OpenDocument, PDF, EPUB, XPS, SWF
            // 相互转换
            long now = System.currentTimeMillis();
            // 转化用时
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
            os.close();
            return pdfPathString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "PDF格式转化失败";
    }


    public static boolean getLicense1() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = PdfUtil.class.getClassLoader()
                    .getResourceAsStream("license.xml");
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    public  String exceOfPdf(String filePath, HttpServletRequest request) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense1()) {
            return "PDF格式转化失败";
        }
        try {
            //long old = System.currentTimeMillis();
            //获取路径参数
            String filePath1 = filePath;
            String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));
            String pdfSPath = filePath2+".pdf";
            // 输出pdf文件路径
            filePath2 = filePath2+".pdf";
            //文件操作
            // 新建一个空白pdf文档
            File file = new File(filePath2);
            FileOutputStream os = new FileOutputStream(file);
            // 原始excel路径
            Workbook wb = new Workbook(filePath1);
            FileOutputStream fileOS = new FileOutputStream(file);
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
            // long now = System.currentTimeMillis();
            return pdfSPath;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "PDF格式转化失败";
    }


    public  boolean getLicense2() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = PdfUtil.class.getClassLoader()
                    .getResourceAsStream("license.xml");
            com.aspose.slides.License aposeLic = new com.aspose.slides.License();
            aposeLic.setLicense(is);
            result = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public  String pptUpdatePdf(String filePath, HttpServletRequest request){
        // 验证License
        if (!getLicense2()) {
            return "PDF格式转化失败";
        }
        try {
            long old = System.currentTimeMillis();
            //File file = new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf");// 输出pdf路径
            //com.aspose.slides.Presentation pres = new  com.aspose.slides.Presentation(Address);//输入pdf路径
            String filePath1 = filePath;
            String filePath2 = filePath.substring(0, filePath.lastIndexOf("."));
            String pdfPathString  = filePath2 + ".pdf";
            // 输出pdf文件路径
//            filePath2 = request.getSession().getServletContext()
//                    .getRealPath(File.separator  + filePath2 + ".pdf");
            //文件操作
            // 新建一个空白pdf文档
            File file = new File(filePath2+".pdf");
            //输入pdf路径
            Presentation pres = new  Presentation(filePath1);
            FileOutputStream fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.close();
            long now = System.currentTimeMillis();
            //转化过程耗时
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath());
            return pdfPathString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "PDF格式转化失败";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值