Java实现pdf转图片案例
程序员文章站
2022-06-25 12:33:00
工程加入依赖:org.apache.pdfbox pdfbox<...
工程加入依赖:
<dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version>2.0.15</version> </dependency> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox-tools</artifactid> <version>2.0.15</version> </dependency>
pdf文件转图片:
public static list<string> pdf2img(file pdffile) { if (pdffile == null || !pdffile.exists()) { throw new runtimeexception("pdf文件不能为空"); } string name = pdffile.getname().substring(0, pdffile.getname().lastindexof(".")); string targetpath = pdffile.getparent() + file.separator + name; list<string> imglist = new arraylist<>(); try { pddocument doc = pddocument.load(pdffile); // 页数 int pagecount = doc.getnumberofpages(); pdfrenderer pdfrenderer = new pdfrenderer(doc); for (int i = 0; i < pagecount; i++) { file targetfile = new file(targetpath + file.separator + name + "-" + (i + 1) + ".jpg"); if (!targetfile.getparentfile().exists()) { fileutil.mkdir(targetfile.getparentfile()); } pdfrenderer.renderimage(i); bufferedimage image = pdfrenderer.renderimagewithdpi(i, 105, imagetype.rgb); imageioutil.writeimage(image, targetfile.getpath(), 105); imglist.add(targetfile.getpath()); } } catch (ioexception e) { log.error("文件转换异常", e); throw new runtimeexception("文件转换异常,err=" + e.getmessage()); }
pdf转成一张图片:
/** * pdf转成一张图片 * * @param pdffile pdf图片文件 * @return 图片地址 */ public static string pdf2oneimg(file pdffile) { list<string> imgs = pdf2img(pdffile); int len = imgs.size(); 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(imgs.get(i)); if (!src[i].exists()) { throw new runtimeexception("文件【" + imgs.get(i) + "】不存在"); } images[i] = imageio.read(src[i]); } catch (exception e) { log.error("", e); throw new runtimeexception(e); } int width = images[i].getwidth(); int height = images[i].getheight(); // 从图片中读取rgb 像素 imagearrays[i] = new int[width * height]; imagearrays[i] = images[i].getrgb(0, 0, width, height, imagearrays[i], 0, width); } int dst_height = 0; int dst_width = images[0].getwidth(); // 合成图片像素 for (int i = 0; i < images.length; i++) { dst_width = dst_width > images[i].getwidth() ? dst_width : images[i].getwidth(); dst_height += images[i].getheight(); } if (dst_height < 1) { throw new runtimeexception("文件合成失败,合成后的图片文件高度=" + dst_height); } string name = pdffile.getname().substring(0, pdffile.getname().lastindexof(".")); string targetpath = pdffile.getparent() + file.separator + name; // 输出路径 file outfile = new file(targetpath + file.separator + name + "-bigone.jpg"); // 生成新图片 try { dst_width = images[0].getwidth(); bufferedimage imagenew = new bufferedimage(dst_width, dst_height, bufferedimage.type_int_rgb); int height_i = 0; for (int i = 0; i < images.length; i++) { imagenew.setrgb(0, height_i, dst_width, images[i].getheight(), imagearrays[i], 0, dst_width); height_i += images[i].getheight(); } imageio.write(imagenew, "jpg", outfile); } catch (exception e) { log.error("图片合并异常=", e); throw new runtimeexception(e); } return outfile.getpath(); }
到此这篇关于java实现pdf转图片案例的文章就介绍到这了,更多相关java实现pdf转图片内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!