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

该博客介绍了一个Java工具类,用于将PDF文件转换为多张图片,并可选择将这些图片打包成ZIP压缩文件。文章详细展示了如何使用Icepdf库读取PDF,将其每一页转换为BufferedImage,然后保存为JPG图片。此外,还提供了对外的API接口供调用,支持PDF到图片的下载和获取PDF的页数。
摘要由CSDN通过智能技术生成

引入所需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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值