赞
踩
将ofd文件拆分多张图像,pom文件引入依赖包
<dependency> <groupId>org.ofdrw</groupId> <artifactId>ofdrw-converter</artifactId> <version>1.19.1</version> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> </exclusion> <exclusion> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> </exclusion> <exclusion> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> </exclusion> </exclusions> </dependency>
以下两种方式将ofd文件图拆分成多张图像
方式一
try (OFDReader reader = new OFDReader("填写文件路径")) { ImageMaker imageMaker = new ImageMaker(reader, 15); for (int i = 0; i < imageMaker.pageSize(); i++) { // 指定页码转换图片 num = num + 1; BufferedImage image = imageMaker.makePage(i); //保存jpg图像 File imgFile = new File(String.join(File.separator, baseFilePath, "filePath", path, num + ".jpg")); // 存储为指定格式图片 ImageIO.write(image, "JPG", imgFile); } catch (Exception e) { e.printStackTrace(); }
方式二(比较推荐)
import org.apache.commons.io.FileUtils; import org.ofdrw.converter.ImageMaker; import org.ofdrw.reader.OFDReader; import org.springframework.util.Base64Utils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; public class OfdUtils { public static final Base64.Decoder DECODER = Base64.getDecoder(); private OfdUtils() { } public static void main(String[] a) throws Throwable{ String ofd = "/project/00001.ofd"; File imageFile = new File(ofd); String img = Base64Utils.encodeToString(FileUtils.readFileToByteArray(imageFile)); ofdToImage(img); } /*** * ofdBase64转图片,返回图片对象 * * @param ofdBase64 pdf类型base64 * @return BufferedImage */ public static void ofdToImage(String ofdBase64){ byte[] decode = DECODER.decode(ofdBase64.getBytes(StandardCharsets.UTF_8)); int num = 0; try( ByteArrayInputStream stream = new ByteArrayInputStream(decode); OFDReader reader = new OFDReader(stream); ) { ImageMaker imageMaker = new ImageMaker(reader, 15); for (int i = 0; i < imageMaker.pageSize(); i++) { // 指定页码转换图片 //list.add(imageMaker.makePage(i)); num = num + 1; BufferedImage image = imageMaker.makePage(i); //保存jpg 数据信息 File imgFile = new File("/project" + File.separator + num + ".jpg"); // 5. 存储为指定格式图片 ImageIO.write(image, "JPG", imgFile); } } catch (IOException e) { e.printStackTrace(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。