Java ppt,pdf 首页自动生成缩略图

工具类

package org.jeecg.modules.common.autoGenerateImages;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author DLXL9664
 */
@Slf4j
public class BimPDFFileImage{


    public static void main(String[] args) throws Exception {
        generateThumbnail(new File("D:\\fileText\\BIMIndustry.pdf"),new File("D:\\fileText\\ceshi.png"));
    }

    /**
     * 增加时间戳的文件名(不带后缀)
     *
     * @param filePath 文件路径
     * @return java.lang.String
     * @create 2023-01-09
     */
    private static String getFileName(String filePath) {
        String fileName = new File(filePath).getName();
        String res = System.currentTimeMillis() + "-" + fileName.substring(0, fileName.lastIndexOf("."));
        return res;
    }


    /**
     * 引用分类
     *
     * @param inputFile  需要读取的文件
     * @param outputFile  缩略图文件
     * @create 2023-05-9
     * */
    public static void generateThumbnail(File inputFile, File outputFile) {
        try {
            int thumbWidth = 1600;
            int thumbHeight = 900;
            String fileType = getFileType(inputFile);
            if ("pdf".equals(fileType)) {
                generatePdfThumbnail(inputFile, outputFile, thumbWidth, thumbHeight);
            } else if ("pptx".equals(fileType)) {
//               PPT 宽高重绘不了   暂时写死
                generatePPTThumbnail(inputFile, outputFile, 900, 500);
            } else {
                throw new IllegalArgumentException("Unsupported file type");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * PPT文件生成缩略图
     * */
    private static void generatePPTThumbnail(File pptFile, File thumbnailFile, int width, int height) throws Exception {
        // 读取 PowerPoint 文件内容
        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(pptFile));

        // 渲染 PowerPoint 内容为图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取第一张幻灯片
        XSLFSlide slide = ppt.getSlides().get(0);

        Graphics2D graphics2D = image.createGraphics();
        graphics2D.setPaint(Color.WHITE);
        graphics2D.fill(new Rectangle2D.Float(0,0,width,height));

        slide.draw(graphics2D);

        // 将图片写入文件
        ImageIO.write(image, "png", thumbnailFile);

        // 释放资源
        graphics2D.dispose();
        ppt.close();
    }

    /**
     * pdf文件生成缩略图
     * */
    private static void generatePdfThumbnail(File inputFile, File outputFile, int thumbWidth, int thumbHeight) {
        try {
            PDDocument document = PDDocument.load(inputFile);
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            BufferedImage image = pdfRenderer.renderImageWithDPI(0, 100);
            BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = thumbImage.createGraphics();
            graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
            ImageIO.write(thumbImage, "png", outputFile);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getFileType(File file) {
        String fileName = file.getName();
        int index = fileName.lastIndexOf(".");
        if (index > 0 && index < fileName.length() - 1) {
            return fileName.substring(index + 1);
        }
        return "";
    }

    public static String getFileTypeByName(String fileName) {
        int index = fileName.lastIndexOf(".");
        if (index > 0 && index < fileName.length() - 1) {
            return fileName.substring(index + 1);
        }
        return "";
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值