Java 批量导出PPT为图片,并合并生成PDF

Java 批量导出PPT为图片,并合并生成PDF

最近有小伙伴需要把PPT打印,但苦于PPT太多,浪费纸张,所以特别写了这个合并多张PPT到一张A4纸上并生成PDF方便打印的程序:


前期准备

  • 把PPT评论导出为图片
  • 搭建项目,导入必要Jar
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-scratchpad</artifactId>
          <version>3.0.2-FINAL</version>
      </dependency>

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

Java代码

package com.qfh;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * PPT转PDF工具类
 * 作者:邱飞虎
 * 时间:2016年09月04日12:09:01
 */
public class PPTToPDFUtil {


    /**
     * 生成PDF
     * @param path01 导出PPT图片地址
     * @return
     */
    public static Boolean createPDF(String path01){

        try{
            String path02 = path01+"合并后/";
            File file = new File(path02);
            if(!file.exists()){
                file.mkdir();
            }
            String pdfPath = path02+"合并后.pdf";
            List<String> srcList = imgJoint(path01,path02);  //合并图片
            imgsToPdf(srcList,pdfPath); //生成PDF
            //删除图片
            for(File f : file.listFiles()){
                if(f.getPath().endsWith(".jpg") || f.getPath().endsWith(".png")){
                    f.delete();
                }
            }
            System.out.print("运行完毕创建成功!pdf地址:"+pdfPath);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 多张图片合并为一张图片
     * @param path01 图片目录
     * @param path02 合并后的图片目录
     * @return
     * @throws Exception
     */
    public static List<String> imgJoint(String path01,String path02) throws Exception{


        File file=new File(path01);
        File[] tempList = file.listFiles();

        List<String> imgList = new ArrayList<String>();
        for(File f : tempList){
            if(f.getPath().endsWith(".jpg")||f.getPath().endsWith(".png")){
                imgList.add(f.getPath());
            }
        }
        if(imgList.size() == 0){
            return null;
        }
        Image firstImg = ImageIO.read(new File(imgList.get(0)));
        int imgWidth = firstImg.getWidth(null);
        int imgHeight = firstImg.getHeight(null);
        System.out.println("imgWidth:"+imgWidth+",imgHeight:"+imgHeight);
        //A4是2480*3508象素 210*297毫米
        int pageWidth = 2480;
        int pageHeigth = 3508;
        int count = imgList.size();

        int rowSize = pageWidth/imgWidth;
        int colSize = pageHeigth/imgHeight;

        System.out.println("rowSize:"+rowSize+",colSize:"+colSize);
        int pageSize = rowSize*colSize;
        int pageTotal = count%pageSize == 0 ? count/pageSize : count/pageSize+1;
        System.out.println("pageSize:"+pageSize+",pageTotal:"+pageTotal);
        int index = 0;
        int end = pageSize;

        List<String> srcImgList = new ArrayList<String>();
        for(int j=1;j<=pageTotal;j++){

            BufferedImage tag = new BufferedImage(pageWidth,pageHeigth,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = tag.createGraphics();
            g2d.setColor(Color.white);
            g2d.fillRect(0, 0,pageWidth,pageHeigth);
            System.out.println("第:"+j+"张");
            System.out.println("index:"+index+",end:"+end);

            int row = 0;
            int col = 0;

            for(int i=index;i<end;i++){
                System.out.println("行:"+row+",列:"+col);

                Image img = ImageIO.read(new File(imgList.get(i)));
                int x =  img.getWidth(null)*col;
                int y = img.getHeight(null)*row;

                g2d.drawImage(img, x, y,img.getWidth(null),img.getHeight(null),null);
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,1.0f)); //透明度设置开始

                if((i+1)%rowSize == 0){
                    row++;col = 0;
                }else {
                    col++;
                }
                System.out.println(imgList.get(i));

            }
            index += pageSize;
            end = index + pageSize;
            end = end > count ? count : end;

            String imgSrc = path02+j+".jpg";
            srcImgList.add(imgSrc);
            FileOutputStream out = new FileOutputStream(imgSrc);

            ImageIO.write(tag,"jpg", out);//写图片

            out.close();

        }
        System.out.println("PPT总数:"+count);
        System.out.println("图片数:"+pageTotal);

        return srcImgList;
    }

    /**
     * 图片批量转pdf
     * @param imagList 图片地址数组
     * @param pdfPath pdf地址
     * @return
     */
    public static File imgsToPdf(List<String> imagList,String pdfPath) {

        Document doc = new Document(PageSize.A4,0,0,0,0);
        try {
            PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
            doc.open();
            for (int i = 0; i < imagList.size(); i++) {
                doc.newPage();
                String  src = imagList.get(i);
                com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(src);

                float heigth = img.getHeight();
                float width = img.getWidth();
                int percent = getPercent2(heigth, width);
                System.out.println(src+" 比例:"+percent*3);
                img.setAlignment(com.itextpdf.text.Image.MIDDLE);
                img.scalePercent(percent+3);// 表示是原来图像的比例;
                doc.add(img);
            }
            doc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File mOutputPdfFile = new File(pdfPath);
        if (!mOutputPdfFile.exists()) {
            mOutputPdfFile.deleteOnExit();
            return null;
        }
        return mOutputPdfFile;
    }

    /**
     * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
     *
     * @param h
     * @param w
     * @return
     */

    public static 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 static int getPercent2(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 530 / w * 100;
        p = Math.round(p2);
        return p;
    }

    /**
     * 测试
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        String path01 = "/Users/QiuFeihu/Documents/Word/2/";

        createPDF(path01);

    }

}

运行结果:

imgWidth:720,imgHeight:540
rowSize:3,colSize:6
pageSize:18,pageTotal:41
第:1张
index:0,end:18
行:0,列:0
/Users/QiuFeihu/Documents/Word/2/幻灯片001.jpg
行:0,列:1
.....
行:4,列:0
/Users/QiuFeihu/Documents/Word/2/幻灯片733.jpg
行:4,列:1
/Users/QiuFeihu/Documents/Word/2/幻灯片734.jpg
PPT总数:734
图片数:41
.....
/Users/QiuFeihu/Documents/Word/2/合并后/39.jpg 比例:63
/Users/QiuFeihu/Documents/Word/2/合并后/40.jpg 比例:63
/Users/QiuFeihu/Documents/Word/2/合并后/41.jpg 比例:63
运行完毕创建成功!pdf地址:/Users/QiuFeihu/Documents/Word/2/合并后/合并后.pdf

备注

导出图片尺寸越小,生成PDF页数越少,因为每页可以放更多的PPT,但考虑到清晰度,建议导出图片不要太小。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第1章 Web应用程序基础 Java Web程序设计 Java-Web程序设计(PPT)全文共389页,当前为第1页。 课程概述 终点 起点 1 2 3 4 5 6 7 1.Web应用程序基础 2.JSP基础 3.JSP内置对象 5. Servlet技术 4.JavaBean和标准动作 6. Servlet技术进阶 8. 会话跟踪技术进阶 7. 会话跟踪技术 8 9 9.EL表达式 10 10.JSTL标签库 Java-Web程序设计(PPT)全文共389页,当前为第2页。 本章内容 Web 应用程序简介 HTTP 协议 Tomcat 简介 使用Eclipse开发Java Web 应用程序 静态的登录页面制作 使用网络抓包的方式查看网络状态 修改Tomcat服务器端口 编写404页面程序并运行此程序 Java-Web程序设计(PPT)全文共389页,当前为第3页。 1.1 Web 应用程序 Web 应用程序概述 Web 应用的工作原理 使用Eclipse创建一个静态登录页面 20 25 Java-Web程序设计(PPT)全文共389页,当前为第4页。 1.1.1 Web 应用程序概述 随着Internet和网络应用程序的发展,其开发体系结构主要分为两种: 基于客户端 / 服务器端的 C/S 体系结构。 基于浏览器 / 服务器的 B/S 体系结构。 Java-Web程序设计(PPT)全文共389页,当前为第5页。 1.1.1 B/S结构优势 B/S 体系结构相对于 C/S 体系结构而言具有更多的优势,目前大量的应用程序开始移到应用 B/S 体系结构,其主要优势在于以下几点: 安装维护升级方便,仅部署服务器 对客户机配置要求不高,满足服务器配置即可 访问范围更广 在我们常用的软件中,哪些是 B/S 体系结构?哪些是 C/S 体系结构? Java-Web程序设计(PPT)全文共389页,当前为第6页。 1.1.2 Web 应用程序的工作原理 Web 应用程序大致可以分为两种,即静态网站和动态网站。 静态网站采用Html语言编写,放置于 Web 服务器上,用户通过浏览器直接请求解析显示。 缺点:内容固定不变,改变显示必须修改html代码 Java-Web程序设计(PPT)全文共389页,当前为第7页。 1.1.2 Web 应用程序的工作原理 随着网络技术的发展,基于 Internet 的 Web 应用程序也变得越来越复杂,更多的内容需根据用户的请求动态生成页面信息,即动态网站。 动态网站指在Html静态页面中嵌入Java、C#、Php等脚本代码,将编写后的页面放入Web服务器,由服务器编译换为最终Html返回给客户端。 Java-Web程序设计(PPT)全文共389页,当前为第8页。 1.1.3 学生实践练习 使用 Eclipse 创建一个静态的登录页面。 20 Java-Web程序设计(PPT)全文共389页,当前为第9页。 1.1.3 学生实践练习 (1)在 Eclipse 中,点击"File",显示菜单,选择"New" "Other"。 (2)点击"Other"菜单项,显示"New(新建)"对话框,展开"Web"节点,选择"Static Web Project"创建css目录并在css目录中添加style.css文件。 (3)点击"Static Web Project"节点,弹出"New Static Web Project"界面,创建静态 Web 项目 LoginProject。 (4)点击"Finish"按钮后,新建"LoginProject"项目成功,在该项目的"WebContent"目录中,新建HTML"login.html"页面,添加html代码。 (5)新建 login.css 文件,定义整个登录页面的背景。 Java-Web程序设计(PPT)全文共389页,当前为第10页。 1.2 HTTP协议 URL 简介 HTTP 协议概述 HTTP 处理流程 HTTP 请求方式 使用网络抓包查看网络状态 20 25 Java-Web程序设计(PPT)全文共389页,当前为第11页。 1.2.1 URL简介 URL 是 UniformResource Locator 的缩写,表示统一资源定位器,它是专为标识网络上的资源位置而设定的一种编码地址方式,即俗称:网址。 URL组成:应用层协议、主机 IP 地址或域名、协议端口号、资源路径 / 文件名。 应用层协议 :// 主机 IP 地址或域名、协议端口号 / 资源所在路径 / 文件名 http://www.oracle.com:80/hk/index.htm 浏览网页时输入的域名最终会换为IP地址 由于 80 端口是 HTTP 协议默认的端口号,所以在访问网络地址时可以省略该

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值