pdf、pfd、img互转工具类

xml 复制代码
    <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.ofdrw</groupId>
            <artifactId>ofdrw-full</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.pansoft.xbrl</groupId>
            <artifactId>xbrl-json</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.27</version>
        </dependency>
java 复制代码
/**
 * @author : maqingbo
 * @email : qingbo.my@gmail.com
 * @date : 2023/8/8:13:04  周二
 */
public class OfdUtils {

    /**
     * ofd->img
     * @param ofdInputFile
     * @param imgOutputFile
     * @throws FileNotFoundException
     */
    public static void ofd2Img(String ofdInputFile, String imgOutputFile) throws FileNotFoundException {
        String tempPdfPath = System.getProperty("java.io.tmpdir") + File.separator + "temp.pdf";
        ofd2Pdf(ofdInputFile, tempPdfPath);
        pdf2Img(tempPdfPath, imgOutputFile,false);
        File file = new File(tempPdfPath);
        FileUtil.del(file);
    }

    /**
     * pdf->img
     * @param pfdInputFile
     * @param imgOutputFile
     * @param outLongImage 是否输出长图
     */
    public static void pdf2Img(String pfdInputFile, String imgOutputFile,boolean outLongImage) {
        if (imgOutputFile.lastIndexOf(".") == -1) {
            throw new RuntimeException("PDF转图片失败");
        }
        String suffix = imgOutputFile.substring(imgOutputFile.lastIndexOf(".") + 1);
        String imgFileName = imgOutputFile.substring(0, imgOutputFile.lastIndexOf("."));
        List<String> imagePath = new ArrayList<>();
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(pfdInputFile));
            PDFRenderer renderer = new PDFRenderer(document);
            for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) {
                BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300);
                String index = String.format("%03d", pageIndex + 1);
                String newFileName = imgFileName.concat("-").concat(index);
                File imgFile = new File(newFileName.concat(".").concat(suffix));
                ImageIO.write(image, suffix,imgFile);
                imagePath.add(imgFile.getAbsolutePath());
            }
            if (outLongImage){
                concatenateImages(imagePath,imgOutputFile);
            }
            System.out.println("OFD文件已成功转换为图片!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("PDF转图片失败");
        } finally {
            if (document != null) {
                try {
                    document.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 多张图片拼接成一张长图
     *
     * @param imagePaths
     */
    public static void concatenateImages(List<String> imagePaths, String outputImagePath) throws IOException {
        // 加载所有图片并获取宽度和高度
        BufferedImage[] images = new BufferedImage[imagePaths.size()];
        int totalHeight = 0;
        int maxWidth = 0;

        for (int i = 0; i < imagePaths.size(); i++) {
            try {
                images[i] = ImageIO.read(new File(imagePaths.get(i)));
            } catch (IOException e) {
                e.printStackTrace();
                throw new IOException();
            }
            totalHeight += images[i].getHeight();
            maxWidth = Math.max(maxWidth, images[i].getWidth());
        }

        // 创建空白长图
        BufferedImage concatImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = concatImage.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, maxWidth, totalHeight);

        // 拼接图片
        int currentHeight = 0;
        for (BufferedImage image : images) {
            g2d.drawImage(image, 0, currentHeight, null);
            currentHeight += image.getHeight();
        }

        g2d.dispose();

        // 保存拼接后的长图
        ImageIO.write(concatImage, "PNG", new File(outputImagePath));

        //删除零散图片
        for (String imagePath : imagePaths) {
            FileUtil.del(new File(imagePath));
        }
    }

    /**
     * ofd->pdf
     * @param ofdInputStream
     * @param pdfOutputStream
     */
    public static void ofd2Pdf(InputStream ofdInputStream, OutputStream pdfOutputStream) {
        ConvertHelper.toPdf(ofdInputStream, pdfOutputStream);
    }

    public static void ofd2Pdf(String ofdPath, String pdfPath) {
        FileInputStream ofdInputStream = null;
        FileOutputStream pdfOutputStream = null;
        try {
            ofdInputStream = new FileInputStream(ofdPath);
            pdfOutputStream = new FileOutputStream(pdfPath);
            ConvertHelper.toPdf(ofdInputStream, pdfOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("OFD转PDF异常");
        } finally {
            if (ofdInputStream != null) {
                try {
                    ofdInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (pdfOutputStream != null) {
                try {
                    pdfOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
相关推荐
一头生产的驴3 小时前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf
开开心心就好1 天前
批量PDF转换工具,一键转换Word Excel
开发语言·前端·学习·pdf·电脑·word·excel
老家的回忆2 天前
jsPDF和html2canvas生成pdf,组件用的elementplus,亲测30多页,20s实现
前端·vue.js·pdf·html2canvas·jspdf
Vertira2 天前
pdf 合并 python实现(已解决)
前端·python·pdf
十一0829932 天前
【PDF-XSS攻击】springboot项目-上传文件-解决PDF文件XSS攻击
spring boot·pdf·xss
qq_393828222 天前
PDF的图片文字识别工具
windows·pdf·电脑·软件需求·图片处理
wsxqaz2 天前
浏览器原生控件上传PDF导致hash值不同
算法·pdf·哈希算法
工业3D_大熊12 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
IDRSolutions_CN12 天前
在 Java 中生成 PDF 缩略图(教程)
java·经验分享·pdf·软件工程·团队开发
IDRSolutions_CN12 天前
用Java将PDF转换成GIF
java·经验分享·pdf·软件工程·团队开发