引入所需maven依赖

org.icepdf.os

icepdf-core

6.2.2

javax.media

jai_core

JAVA代码工具类package com.util;

import com.lowagie.text.pdf.PdfReader;

import lombok.extern.slf4j.Slf4j;

import org.icepdf.core.pobjects.Document;

import org.icepdf.core.pobjects.Page;

import org.icepdf.core.util.GraphicsRenderingHints;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletResponse;

import java.awt.image.BufferedImage;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.net.URLEncoder;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

//java 项目 www.fhadmin.org

@Slf4j

public class PdfToImageUtil {

//支持文件格式

public static final String SUPPORT_FILE = "pdf";

//图片文件格式

public static final String IMAGE_SUFFIX = "jpg"; //png

//压缩文件格式

public static final String ZIP_SUFFIX = "zip";

//PDF是否为一页

private static boolean isImage;

/**

* 对外的开放接口,用于将PDF文件转换为图片文件压缩包进行下载

* @param file SpringMVC获取的图片文件

*/

public static synchronized void pdfToTransformation(MultipartFile file, HttpServletResponse response) throws Exception {

String fileName = file.getOriginalFilename();

if(null == fileName) return;

String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

log.info("文件名称:" + fileName + ",文件后缀:" + suffix);

if(!SUPPORT_FILE.equals(suffix)) return;

isImage = false;

File imageFile = generateFile(file);

log.info("文件生成成功!");

downloadFile(imageFile, response);

}

/**

* 将PDF文件转换为多张图片并放入一个压缩包中

* @param file SpringMVC获取的图片文件

* @return 图片文件压缩包

* @throws Exception 抛出异常

*/

private static File generateFile(MultipartFile file) throws Exception {

String fileName = file.getOriginalFilename();

if(null == fileName) return null;

Document document = new Document();

document.setByteArray(file.getBytes(), 0, file.getBytes().length, fileName);

log.info("PDF页数:" + document.getNumberOfPages());

isImage = 1 == document.getNumberOfPages();

File imageReturnFile = null;

List fileList = new ArrayList<>();

for (int i = 0; i

BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,

Page.BOUNDARY_CROPBOX, 0F, 2.5F);

File imageFile = new File((i + 1) + "." + IMAGE_SUFFIX);

ImageIO.write(image, IMAGE_SUFFIX, imageFile);

image.flush();

if(isImage){

imageReturnFile = imageFile;

break;

}

fileList.add(imageFile);

}

document.dispose();

if(isImage) return imageReturnFile;

//压缩图片文件

String directoryName = fileName.substring(0, fileName.lastIndexOf("."));

File zipFile = new File(directoryName + "." + ZIP_SUFFIX);

ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile));

zipFile(fileList, zipOutputStream);

zipOutputStream.close();

return zipFile;

}

/**

* 下载image/zip文件

* @param downloadFile 文件

* @param response HttpServletResponse

* @throws IOException IO异常

*/

private static void downloadFile(File downloadFile, HttpServletResponse response) throws IOException {

FileInputStream fileInputStream = new FileInputStream(downloadFile);

byte[] bytes = new byte[fileInputStream.available()];

fileInputStream.read(bytes);

fileInputStream.close();

//设置response参数

response.reset();

if(isImage){

response.setContentType("image/jpeg");

} else {

response.setContentType("application/zip");

}

response.setCharacterEncoding("UTF-8");

response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(downloadFile.getName(), "UTF-8"));

OutputStream outputStream = response.getOutputStream();

outputStream.write(bytes);

outputStream.flush();

outputStream.close();

if(!isImage) downloadFile.delete();

}

/**

* 压缩文件

* @param inputFiles 具体需要压缩的文件集合

* @param zipOutputStream ZipOutputStream对象

* @throws IOException IO异常

*/

private static void zipFile(List inputFiles, ZipOutputStream zipOutputStream) throws IOException {

byte[] buffer = new byte[1024];

for (File file : inputFiles) {

if (file.exists()) {

if (file.isFile()) {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

zipOutputStream.putNextEntry(new ZipEntry(file.getName()));

int size = 0;

while ((size = bis.read(buffer)) > 0) {

zipOutputStream.write(buffer, 0, size);

}

zipOutputStream.closeEntry();

bis.close();

file.delete();

} else {

File[] files = file.listFiles();

if(null == files) continue;

List childrenFileList = Arrays.asList(files);

zipFile(childrenFileList, zipOutputStream);

}

}

}

}

/**

* 获取PDF页数

* @throws IOException

*/

public static String getPdfPageSize(MultipartFile file) throws IOException {

PdfReader pdfReader = new PdfReader(file.getBytes());

int pages = pdfReader.getNumberOfPages();

return String.valueOf(pages);

}

}

生成图片PdfController.javapackage com.web.transformation.controller;

import com.util.PdfToImageUtil;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

//java项目 www.fhadmin.org

@Controller

public class PdfController {

//PDF转图片或者ZIP

@RequestMapping("/pdfToImage")

@ResponseBody

public void pdfToImage(MultipartFile file, HttpServletResponse response) throws Exception{

PdfToImageUtil.pdfToTransformation(file,response);

}

//获取PDF页数

@RequestMapping("/getPdfPageSize")

@ResponseBody

public String pdfToImage(MultipartFile file) throws IOException {

return PdfToImageUtil.getPdfPageSize(file);

}

}

java读取pdf三维图片_java 读取PDF文件生成图片形式相关推荐

  1. java pdf 插入图片_java在pdf模板的指定位置插入图片

    个人感觉pdf的操作比word舒心多了 java操作pdf有个非常好用的库itextpdf,maven: com.itextpdf itextpdf 5.5.6 com.itextpdf itext- ...

  2. java导出pdf 含图片_java 生成PDF含图片和中文件实现代码

    1,所需包 iText.jar iTextAsian.ar(支持中包) 2,列子 package com.pdf; import java.awt.Color; import java.io.File ...

  3. java读取1g超大图片_java读取大文件1G+ | 学步园

    正常读取大文件,可能会想到用缓存 如: package base; import java.io.BufferedInputStream; import java.io.BufferedReader; ...

  4. Java实现多个图片转化成PDF

    Java实现多个图片转化成PDF 依赖 <dependency><groupId>com.lowagie</groupId><artifactId>it ...

  5. java pdf转图片 pdfbox_JAVA基于PDF box将PDF转为图片

    在一项目中用到,本身我是.NET的,团队中有用到JAVA,故此我处理这个功能,记录以下备用. 1.引用:fontbox-2.0.16.jar.pdfbox-app-2.0.16.jar 版本一定要正确 ...

  6. POI根据模板导出word文件,以及word转PDF,PDF转图片再插入PDF中(防止PDF被修改)

    POI操作word和PDF POI根据模板导出word文件 word转PDF PDF转图片再插入PDF中(防止PDF被修改) POI根据模板导出word文件 一.制作word模版,${xxxx}是一会 ...

  7. java 读取pdf签名域_Java给PDF签名

    1.引入依赖 com.e-iceblue e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.pdf ...

  8. java oracle 图片_JAVA读取Oracle中的blob图片字段并显示

    JAVA读取Oracle中的blob图片字段并显示 近期,在给客户做一个Demo页面时,需要用JAVA读取Oracle中的blob图片字段并显示,在此过程中,遇到一些问题,例如:连接Oracle数据库 ...

  9. java pdf 图片_java实现PDF转图片的方法

    本文实例为大家分享了java实现PDF转图片的具体代码,供大家参考,具体内容如下 1.首先利用maven引入所需jar包 org.apache.pdfbox fontbox 2.0.1 org.apa ...

最新文章

  1. 叶杰平:主流强化学习过分简化假设,与真实场景差距较大
  2. oracle的39082,ORA-39082 导入数据遇到的问题
  3. Py之logging:logging的简介、安装、使用方法之详细攻略
  4. 进击的Android Hook 注入术《二》
  5. 我仅仅想安安静静的做一个编程的美男子
  6. nodejs中的文件系统
  7. JAVA学习Swing绝对局部简单学习
  8. ASP.NET Ajax编程技术学习
  9. Python入门(03) -- 字典
  10. 【BZOJ1901】Dynamic Rankings,树状数组套主席树
  11. linux能秒创虚拟机吗,linux下5秒创建rhel7虚拟机
  12. 软件测试适合女生学吗?
  13. 01.26 小组功能初步总结
  14. linux上mysql定时备份数据库数据_Linux下如何实现MySQL数据库每天定时自动备份
  15. 【故障分析】基于matlab ICA故障监测【含Matlab源码 1590期】
  16. 华为服务器机柜的型号,华为N63E-22,华为交换机柜,300*600*2200,华为21英寸服务器机柜,华为OLT MA5680T波分机柜...
  17. 微信公众平台注册流程
  18. kgm转mp3安卓_酷狗KGM转MP3格式工具电脑版
  19. MySQL 百分比排序
  20. oracle 乘法表,ORACLE SQl——9*9 乘法表的实现方法

热门文章

  1. 日语中「よう」的用法整理
  2. 你不知道Python多能干,小伙子骨骼惊奇不来学习吗?
  3. win11修改wifi mac地址
  4. python画箭头_Python matplotlib绘制图形,包括点、曲线、注释和箭头
  5. 以豌豆荚为例,用 Scrapy 爬取分类多级页面
  6. 【JavaWeb】本地文件传输协议(File://)
  7. Python matplotlib常见问题
  8. C语言高考志愿填报管理系统,高考志愿智能填报系统
  9. html form提交前md5,javascript实现MD5加密-JavaScript获取HTML元素的三种方...-兼容IE与firefox的js回车提交表单_169IT.COM...
  10. uni-app 为组件 uni-icons 制作可拓展的自定义图标库(超实用)