java pdf,word,ppt转图片

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.8
         
    
    com.test
    local-service
    0.0.1-SNAPSHOT
    local-service
    local-service
    
        8
        8
        8
        UTF-8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-websocket
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
        
            org.apache.commons
            commons-lang3
            3.13.0
        

        
            com.alibaba
            fastjson
            2.0.32
        

        
        
            org.apache.commons
            commons-compress
            1.24.0
        

        
        
            org.apache.pdfbox
            pdfbox-app
            2.0.16
        

        
        
            words
            aspose-words
            1.0.0
        


        
        
            org.apache.poi
            poi
            4.1.2
        
        
            org.apache.poi
            poi-ooxml
            4.1.2
        
        
            org.apache.poi
            poi-scratchpad
            4.1.2
        
        
            org.apache.xmlgraphics
            batik-bridge
            1.9.1
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


文件转换统一工具类 FileConvertUtil.java

调用 FileConvertUtil.convert2Images(filePath)即可输出图片信息



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.python.jline.internal.Log;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Base64Utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yangguang
 * @date 2023年11月01日 17:01
 */
@Slf4j
public class FileConvertUtil {
    private static Map>> convertFactory = new HashMap<>();

    static {
        convertFactory.put("ppt", PPTUtil::ppt2image);
        convertFactory.put("pptx", PPTUtil::pptx2image);
        convertFactory.put("doc", WordUtil::word2image);
        convertFactory.put("docx", WordUtil::word2image);
        convertFactory.put("pdf", PdfUtil::pdf2image);
    }

    public static List convert2Images(String filePath) {
        File file = new File(filePath);
        String name = file.getName().substring(0,file.getName().lastIndexOf("."));
        String targetDir = file.getParentFile().getAbsolutePath()+File.separator+name;
        if(new File(targetDir).exists()){
            return Arrays.stream(new File(targetDir).listFiles()).map(File::getAbsolutePath)
                    .sorted((name1,name2)->Integer.compare(getFileIndex(name1),getFileIndex(name2)))
                    .map(FileConvertUtil::file2ImageInfo)
                    .collect(Collectors.toList());
        }else{
            new File(targetDir).mkdirs();
        }
        String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
        List list = Optional.ofNullable(convertFactory.get(suffix))
                .map(f -> f.apply(filePath,targetDir))
                .orElseThrow(() -> new CustomException("不支持得文件类型"));
        return list;
    }

    public static List convert2ImagesBase64(String filePath) {
        return convert2Images(filePath).stream()
                .map(ImageInfo::getPath)
                .map(FileConvertUtil::base64Encoding)
                .collect(Collectors.toList());
    }

    public static void clearWorkPath(File dir) {
        if (dir == null) {
            File file = new File(Constants.TEMP_PATH);
            for (File f : file.listFiles()) {
                clearWorkPath(f);
            }
        } else if (dir.isDirectory()) {
            for (File file : dir.listFiles()) {
                clearWorkPath(file);
            }
            dir.delete();
        } else {
            dir.delete();
        }
    }


    private static String base64Encoding(String imagePath) {
        InputStream in = null;
        try (
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            //建一个空的字节数组
            byte[] result = null;
            in = new FileInputStream(imagePath);
            byte[] buf = new byte[1024];
            //用来定义一个准备接收图片总长度的局部变量
            int len;
            //将流的内容读取到buf内存中
            while ((len = in.read(buf)) > 0) {
                //将buf内存中的内容从0开始到总长度输出出去
                out.write(buf, 0, len);
            }
            //将out中的流内容拷贝到一开始定义的字节数组中
            result = out.toByteArray();
            //通过util包中的Base64类对字节数组进行base64编码
            String base64 = Base64.getEncoder().encodeToString(result);
            return "data:Image/" + "PNG" + ";base64," + base64;
        } catch (Exception e) {
            log.error("image to base64 error",e);
           throw new CustomException("image to base64 error");
        }
    }

    private static int getFileIndex(String filePath){
        String fileName = new File(filePath).getName();
        return Integer.parseInt(fileName.substring(0,fileName.lastIndexOf(".")));
    }


    private static ImageInfo file2ImageInfo(String filePath) {
        ImageInfo.ImageInfoBuilder builder = ImageInfo.builder();
        BufferedImage img = null;
        File f = null;
        try {
            f = new File(filePath);
            img = ImageIO.read(f);
            return builder.width(img.getWidth()).height(img.getHeight()).path(filePath).build();
        }
        catch (IOException e) {
            log.error("读取文件失败",e);
           throw new CustomException("读取文件失败");
        }
    }

}

图片对象ImageInfo.java

@Data
@Builder
public class ImageInfo {
    private String path;
    private Integer width;
    private Integer height;

}
常量文件输出路径Constants.java
/**
 * @author yangguang
 * @date 2023年11月01日 15:35
 */
public interface Constants {
    String CONF_PATH = System.getProperty("user.dir") + File.separator + "conf";
    String TEMP_PATH = System.getProperty("user.dir") + File.separator + "temp";
}

WordUtil.java



import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.tomcat.util.http.fileupload.IOUtils;


@Slf4j
public class WordUtil {

    /**
     * 验证aspose.word组件是否授权:无授权的文件有水印标记
     * 需要使用(aspose-words-15.8.0-jdk16.jar),版本要对应。无水印
     * @return
     */
    private static boolean isWordLicense()
    {
        boolean result = false;
        try {
            String s = "Aspose.Total for JavaAspose.Words for JavaEnterprise20991231209912318bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=";
            ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());
            com.aspose.words.License license = new com.aspose.words.License();
            license.setLicense(inputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //outputStream转inputStream
    private static ByteArrayInputStream parse(OutputStream out) throws Exception
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }

    /**
     * word和txt文件转换图片
     * @param inputStream
     * @param
     * @return
     * @throws Exception
     */
    private static List wordToImg(InputStream inputStream) throws Exception
    {
        if (!isWordLicense())
        {
            return null;
        }
        try {
            Date start = new Date();
            Document doc = new Document(inputStream);
            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
            options.setPrettyFormat(true);
            options.setUseAntiAliasing(true);
            options.setUseHighQualityRendering(true);
            int pageCount = doc.getPageCount();
            //生成前pageCount张,这可以限制输出长图时的页数(方法入参可以传值pageNum)
           /*if (pageCount > pageNum) {
               pageCount = pageNum;
           }*/
            List imageList = new ArrayList();
            for (int i = 0; i < pageCount; i++)
            {
                OutputStream output = new ByteArrayOutputStream();
                options.setPageIndex(i);
                doc.save(output, options);
                ImageInputStream imageInputStream = javax.imageio.ImageIO.createImageInputStream(parse(output));
                imageList.add(javax.imageio.ImageIO.read(imageInputStream));
            }
            List imageList2 = new ArrayList();
            //这个重新生成新的图片是因为直接输出的图片底色为红色
            for(int j=0; j imgs) throws IOException
    {
        // 生成新图片
        BufferedImage destImage = null;
        // 计算新图片的长和高
        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
        // 获取总长、总宽、最长、最宽
        for (int i = 0; i < imgs.size(); i++)
        {
            BufferedImage img = imgs.get(i);
            allw += img.getWidth();
            if (imgs.size() != i + 1)
            {
                allh += img.getHeight() + 5;
            } else {
                allh += img.getHeight();
            }
            if (img.getWidth() > allwMax)
            {
                allwMax = img.getWidth();
            }
            if (img.getHeight() > allhMax)
            {
                allhMax = img.getHeight();
            }
        }
        // 创建新图片
        if (isHorizontal)
        {
            destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
        } else {
            destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
        }
        Graphics2D g2 = (Graphics2D) destImage.getGraphics();
        g2.setBackground(Color.LIGHT_GRAY);
        g2.clearRect(0, 0, allw, allh);
        g2.setPaint(Color.RED);

        // 合并所有子图片到新图片
        int wx = 0, wy = 0;
        for (int i = 0; i < imgs.size(); i++)
        {
            BufferedImage img = imgs.get(i);
            int w1 = img.getWidth();
            int h1 = img.getHeight();
            // 从图片中读取RGB
            int[] ImageArrayOne = new int[w1 * h1];
            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
            if (isHorizontal) { // 水平方向合并
                destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            } else { // 垂直方向合并
                destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            }
            wx += w1;
            wy += h1 + 5;
        }
        return destImage;
    }


    public static List word2image(String wordPath,String targetDir){
        List list = Lists.newArrayList();
        File file = new File(wordPath);
        InputStream inStream =null;
        try {
            inStream = new FileInputStream(file);
            List wordToImg = wordToImg(inStream);

            IntStream.range(0,wordToImg.size()).forEach(i->{
                //可以保存图片(每页保存为一张)
                try {
                    String filePath = (targetDir==null?Constants.TEMP_PATH:targetDir)+File.separator+ i +".png";
                    BufferedImage image = wordToImg.get(i);
                    ImageInfo imageInfo = ImageInfo.builder().width(image.getWidth()).height(image.getHeight()).path(filePath).build();
                    ImageIO.write(image, "jpg", new File(filePath)); //将其保存在C:/imageSort/targetPIC/下
                    synchronized (list){
                        list.add(imageInfo);
                    }
                }catch (Exception e){
                    log.error("word2pdf error 212",e);
                }
            });
        }catch (Exception e){
            log.error("word2pdf error",e);
        }finally {
            IOUtils.closeQuietly(inStream);
        }
        return list;
    }

    // 测试工具类
    public static void main(String[] args){
       String path = "E:\\网页\\test.docx";
       new WordUtil().word2image(path,null);
    }

}

PPTUtil.java




import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.sl.usermodel.Slide;
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.*;
import java.util.List;

/**
 * @author yangguang
 * @date 2023年11月01日 16:11
 */
@Slf4j
public class PPTUtil {
    private final static double IMAGE_SCALE = 8;

    public static List ppt2image(String pptPath, String targetDir){
        List list = Lists.newArrayList();
        InputStream is = null;
        HSLFSlideShow ppt = null;
        try {
            File file = new File(pptPath);
            is = new FileInputStream(file);
            ppt =new HSLFSlideShow(is);
            Dimension pgSize = ppt.getPageSize();
            int i=0;
            for (HSLFSlide slide : ppt.getSlides()) {
                ImageInfo imageInfo = toPNG(targetDir,pgSize.width, pgSize.height, slide,i);
                list.add(imageInfo);
                i++;
            }
        } catch (IOException e) {
            log.error("ppt转换图片失败,{}", e.getMessage());
            throw new RuntimeException("ppt转换图片失败" + e.getMessage());
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ppt != null) {
                    ppt.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List pptx2image(String pptPath,String targetDir){
        InputStream is = null;
        XMLSlideShow ppt = null;
        List list = Lists.newArrayList();
        try {
            is = new FileInputStream(pptPath);
            ppt = new XMLSlideShow(is);
            Dimension pgSize = ppt.getPageSize();
            int i=0;
            for (XSLFSlide slide : ppt.getSlides()) {
                ImageInfo imageInfo = toPNG(targetDir,pgSize.width, pgSize.height, slide,i);
                list.add(imageInfo);
                i++;
            }
        } catch (IOException e) {
            log.error("pptx转换图片失败,{}", e.getMessage());
            throw new RuntimeException("pptx转换图片失败" + e.getMessage());
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ppt != null) {
                    ppt.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }



    private static ImageInfo toPNG(String targetDir,int pgWidth, int pgHeight, Slide slide,int i) throws IOException {
        int imageWidth = (int) Math.floor(IMAGE_SCALE * pgWidth);
        int imageHeight = (int) Math.floor(IMAGE_SCALE * pgHeight);

        BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgWidth, pgHeight));
        graphics.scale(IMAGE_SCALE, IMAGE_SCALE);
        slide.draw(graphics);
        // save the output
        String filePath = (targetDir==null?Constants.TEMP_PATH:targetDir)+ File.separator+ i +".png";

        ImageInfo imageInfo = ImageInfo.builder().width(img.getWidth()).height(img.getHeight()).path(filePath).build();
        ImageIO.write(img, "jpg", new File(filePath));
        return imageInfo;
    }

    public static void main(String[] args) {
        new PPTUtil().ppt2image("E:\\网页test.ppt",null);
    }
}

PdfUtil.java




import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author yangguang
 * @date 2022年12月20日 9:56
 */
@Component
@Slf4j
public class PdfUtil {


    private static ImageInfo pdftoimageByNum(PDFRenderer renderer, Integer pageNum, String imagePath)throws IOException{
        BufferedImage image = renderer.renderImageWithDPI(pageNum-1, 296);
        //image = FileUtil.bufferedImage2Transparent(image);
        //          BufferedImage image = renderer.renderImage(i, 2.5f);
        File file1 = new File(imagePath);
        if(!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }
        ImageInfo imageInfo = ImageInfo.builder().width(image.getWidth()).height(image.getHeight()).path(imagePath).build();
        ImageIO.write(image, "PNG", new File(imagePath));
        return imageInfo;
    }

    private static String getFileName(String filepath) {
        String fileName;
        filepath = filepath.replaceAll("\\\\",File.separator);
        if(filepath.contains(File.separator)){
            fileName = filepath.substring(filepath.lastIndexOf(File.separator) + 1);
        }else{
            fileName = filepath;
        }
        if(!fileName.endsWith(".pdf")){
            throw new CustomException("文件格式不正确");
        }
        return fileName.substring(0,fileName.lastIndexOf("."));
    }

    public static List pdf2image(String pdfPath,String targetDir){
        List list = Lists.newArrayList();
        File file = new File(pdfPath);
        PDDocument doc=null;
        try {
            doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            IntStream.range(0,pageCount).forEach(i->{
                try {
                    String imagePath = (targetDir==null?Constants.TEMP_PATH:targetDir)+File.separator+ i + ".png";
                    ImageInfo imageInfo = pdftoimageByNum(renderer, i + 1, imagePath);
                    synchronized (list){
                        list.add(imageInfo);
                    }
                }catch (Exception e){
                    log.error("转换图片失败",e);
                }
            });
        } catch (IOException e) {
            log.error("转换失败,io异常",e);
            throw new CustomException("转换失败,io异常");
        }finally {
            try {
                doc.close();
            } catch (Exception e) {
                log.error("io异常",e);
            }
        }
        return list;
    }

    public static void main(String[] args)
    {
        List a = new PdfUtil().pdf2image("C:\\test.pdf",null);

        System.out.println(a);
    }
}

你可能感兴趣的:(java,pdf,word)