java ofd转pdf和jpg

最近刚遇到ofd转jpg百度好友也找不到 好不容易在git上找到的又不全所以自己整理了一下给大家探探路

首先要导入jar包依赖


    org.ofdrw
    ofdrw-full
    1.8.6

如果jar下载不下来的话就可以自行导入

 
  


mvn install:install-file -Dfile=ofdrw-full-1.8.6.jar -DgroupId=org.ujmp -DartifactId=ofdrw-full -Dversion=1.8.6 -Dpackaging=jar

我把jar放到百度云盘链接:https://pan.baidu.com/s/17DCEN0li3Izhgfh5T6KtFg   提取码:1234

把它导入maven依赖后就大功告成你只需要调用里面的方法即可

package com.sh.untils;

import org.ofdrw.converter.GeneralConvertException;
import org.ofdrw.converter.ImageMaker;
import org.ofdrw.reader.DLOFDReader;
import org.ofdrw.reader.OFDReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.ofdrw.converter.ConvertHelper.ofd2pdf;

/**
 * @program: electroniclicense_xinxiang
 * @description: ofd转jpg和pdf
 * @author: Mr.JZQ
 * @create: 2021-04-17 15:53
 **/

public class OfdConver {
    public static void main(String[] args) throws IOException {
        toPng("D:/downloadFile/ceshi.ofd", "D:/downloadFile","aa");
    }


    /**
     * @Description: ofd转jpg
     * @Param: * @param null:
     * @return: * @return: null
     * @Author: Mr.JZQ
     * @Date: 2021年04月17日 0017
     */
    public static void toPng(String filename, String dirPath, String jpgName) throws IOException {
        Files.createDirectories(Paths.get(dirPath));
        //filename文件路径到文件名
        Path src = Paths.get(filename);

        ImageMaker imageMaker = new ImageMaker(new OFDReader(src), 15);
        imageMaker.config.setDrawBoundary(false);
        for (int i = 0; i < imageMaker.pageSize(); i++) {
            BufferedImage image = imageMaker.makePage(i);
            //文件要存的路径 第二个是文件名
            Path dist = Paths.get(dirPath, jpgName + ".jpg");
            ImageIO.write(image, "PNG", dist.toFile());
        }
    }

    /**
     * 转PDF
     *
     * @param input  OFD输入文件
     * @param output PDF输出文件路径
     * @throws IllegalArgumentException 参数错误
     * @throws GeneralConvertException  文档转换过程中异常
     */
    public static void toPdf(Path input, Path output) {
        ofd2pdf(input, output);
    }
}

这样就大功告成 开箱即用

你可能感兴趣的:(java,maven,ofd)