首页 > 编程语言 >Java Word转为PNG

Java Word转为PNG

时间:2023-08-03 10:14:10浏览次数:27  
标签:Java newWidth int File new images Word type PNG

  • 主要代码逻辑
//判断生成路径
File fileDir = new File("./generatePng");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String workPermitId = 唯一标识;
Document doc = new Document(word文档路径);
if (doc.getPageCount() > 1) {
//多张
for (int i = 0; i < doc.getPageCount(); i++) {
BufferedImage image = doc.saveToImages(i, ImageType.Bitmap);
File file = new File("./generatePng/" + workPermitId + (i + 1) + ".png");
ImageIO.write(image, "PNG", file);
}
//拼接图片
boolean isOk = mergeImage(workPermitId, doc.getPageCount(), 2, System.getProperty("user.dir") + "/generatePng/" + workPermitId + ".png");
if (!isOk) {
return AjaxResult.error("作业票图片生成失败,请重新尝试或确认是否存在word文档");
}
} else {
//单张
BufferedImage image = doc.saveToImages(0, ImageType.Bitmap);
File file = new File("./generatePng/" + workPermitId + ".png");
ImageIO.write(image, "PNG", file);
}
  • 拼接图片方法
/**
* 拼接图片(注:图片需长宽一致)
*
* @param len 图片数量
* @param type 1:横向拼接 2:纵向拼接
* @param targetFile 合成新的图片地址
*/
public static boolean mergeImage(String workPermitId,Integer len, int type, String targetFile) {
    // File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] ImageArrays = new int[len][];

for (int i = 0; i < len; i++) {
try {
// src[i] = new File(files[i]);
images[i] = ImageIO.read(new File(System.getProperty("user.dir") + "/generatePng/" + workPermitId + (i + 1) + ".png"));
} catch (Exception e) {
log.error(e.getMessage(), e);
}

int width = images[i].getWidth();
int height = images[i].getHeight();
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}

int newHeight = 0;
int newWidth = 0;
for (int i = 0; i < images.length; i++) {
// 横向
if (type == 1) {
newHeight = newHeight > images[i].getHeight() ? newWidth : images[i].getHeight();
// 错误代码,原先没有添加+ 号
newWidth += images[i].getWidth();
} else if (type == 2) {
// 纵向
newWidth = newHeight > images[i].getWidth() ? newWidth : images[i].getWidth();
newHeight += images[i].getHeight();
}
}

if (type == 1 && newWidth < 1) {
return false;
}

if (type == 2 && newHeight < 1) {
return false;
}


try {
BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
int width_i = 0;
for (int i = 0; i < images.length; i++) {
if (type == 1) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0, images[i].getWidth());
width_i += images[i].getWidth();
} else if (type == 2) {
ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
height_i += images[i].getHeight();
}
}
// 输出想要的图片
ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}
 

标签:Java,newWidth,int,File,new,images,Word,type,PNG
From: https://www.cnblogs.com/dream-007/p/17602500.html

相关文章

  • java枚举类模板
    importcom.alibaba.fastjson.JSONObject;importlombok.Getter;@GetterpublicenumMedDoctorStatusEnum{ONLINE(0,"上线"),A_SHORT_BREAK(1,"小憩"),OFFLINE(2,"离线");privateIntegercode;privateStringdesc;MedDoct......
  • 问chatgpt:java或者三方jar包,bean属性复制的,但是两个字段的名称不一致,有没有这样的方
    是的,Java中有一些库和框架可以实现对象属性的复制和映射,而无需手动编写getter和setter方法。其中比较常用的是ApacheCommonsBeanUtils和SpringFramework的BeanUtils。使用ApacheCommonsBeanUtils,你可以使用copyProperties方法来复制对象属性。示例代码如下:点击查......
  • 转载:国产linux系统使用 PageOffice 在线打开 word 文件
    一、客户端环境1、操作系统银河麒麟,中标麒麟,统信UOS2、芯片芯片(CPU):x86(Intel、兆芯),ARM(飞腾、鲲鹏),龙芯3、浏览器360安全浏览器奇安信uos自带浏览器4、wps版本wps专业版(11.8.x.xxxxx)国产版卸载wps命令dpkg-l|grepwps|awk'{print$2}'|xargssudoaptpurge......
  • Java内部类持有外部类会导致内存泄露
    packageorg.example.a;importjava.util.ArrayList;importjava.util.List;classOuter{privateint[]data;publicOuter(intsize){this.data=newint[size];}staticclassInner{}InnercreateInner(){ret......
  • 【Java】多线程面试题总结
    最近在看面试题,所以想用自己的理解总结一下,便于加深印象。为什么使用多线程使用多线程可以充分利用CPU,提高CPU的使用率。提高系统的运行效率,对于一些复杂或者耗时的功能,可以对其进行拆分,比如将某个任务拆分了A、B、C三个子任务,如果子任务之间没有依赖关系,那么就可以使用多线程......
  • java-房屋出租系统实现
    房屋出租系统项目需求能够实现对房屋信息的添加、修改和删除(用数组实现),并能够打印房屋明细表项目界面主菜单新增房源查找房源删除房源修改房源房屋列表退出系统项目设计项目实现显示主菜单在HouseView.java中,编写一个方法mainMenu,显示菜单点击查看代码......
  • JavaScript中的 "return await promise" 与 "return promise"
    原文地址:'returnawaitpromise'vs'returnpromise'inJavaScript原文作者:DmitriPavlutin译文出自:翻译计划当从异步功能中返回时,您可以等待该承诺得到解决,或者您可以直接返回它:returnawaitpromisereturnpromise:jsasyncfunctionfunc1(){constpromise=asyncOperat......
  • 我需要 把 目标数据源中的表 获取到表的字段和字段的类型等信息,然后在目标数据源中创
    当涉及到将Oracle数据库字段类型映射为MySQL数据库字段类型时,考虑到不同数据库的差异和复杂性,以下是一个更全面的映射示例,涵盖了更多的Oracle字段类型及其可能的MySQL对应类型。importjava.util.HashMap;importjava.util.Map;publicclassOracleToMySQLTypeConverter{......
  • WordPress 后台常规设置添加配置项
    需要给用户提供一些设置选项,最常见的就是设置首页描述标签、页脚统计代码,如果仅仅只需要这么几个简单的设置项就专门制作一个主题后台,那有点大动干戈了,我们可以给WordPress默认的常规设置添加配置项。先来看添加后的效果图: 添加配置项代码如下://添加常规选项functionbzg_reg......
  • Java面试题 P42:框架篇:Spring-Spring框架中的单例bean是线程安全的吗?Spring框架中的bea
        ......