java 修改pdf图片_java代码将pdf 转换成图片加缩略图 -3

写的这些功能主要就是放在定时任务上,每分钟查询是否要有转换的图片,有的换然后就转换,前端展示图片就可以,大家可以浏览图片。不用在去下载哪些文件了。

下面的是pdf转图片加缩略图的

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import org.icepdf.core.exceptions.PDFException;

import org.icepdf.core.exceptions.PDFSecurityException;

import org.icepdf.core.pobjects.Document;

import org.icepdf.core.pobjects.PDimension;

import org.icepdf.core.pobjects.Page;

import org.icepdf.core.util.GraphicsRenderingHints;

/** * @author :liu.lu

* @version 创建时间:2018年4月19日 下午9:07:33

Description:

*/

public class PdfToImages {

public static final String FILETYPE_JPG = "jpg";

public static final String SUFF_IMAGE = "." + FILETYPE_JPG;

public static void main(String[] args) {

try {

tranfer("D:\\temp\\品高工作流用户指南.pdf",1);

} catch (PDFException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (PDFSecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 转换pfd第一页为jpg缩略图大图

*

* @param filepath

* @param zoom

* @throws PDFException

* @throws PDFSecurityException

* @throws IOException

*/

public static void tranfer1(String filepath, float zoom)

throws PDFException, PDFSecurityException, IOException {

// ICEpdf document class

Document document = null;

float rotation = 0f;

document = new Document();

document.setFile(filepath);

// 创建以pdf文件名为名称的文件夹保存pdf缩略图

File file = new File(filepath.substring(0, filepath.lastIndexOf(".")));

if (!file.exists() && !file.isDirectory()) {

System.out.println("//不存在");

file.mkdir();

} else {

System.out.println("//目录存在");

}

String imagepath = "";

// 设置文件名

imagepath = jpgFilename(filepath, 0);

// 转换首页为jpg缩略图

tranferPer(document, rotation, zoom, imagepath, 0);

}

/**

* 转换pfd每一页为jpg缩略图大图

*

* @param filepath

*            pfd文件路径

* @param zoom

*            缩略图缩放比例

* @throws PDFException

* @throws PDFSecurityException

* @throws IOException

*/

public static void tranfer(String filepath, float zoom)

throws PDFException, PDFSecurityException, IOException {

// ICEpdf document class

Document document = null;

float rotation = 0f;

document = new Document();

document.setFile(filepath);

// 获取pdf总页数

int pages = document.getNumberOfPages();

if (pages > 0) {

// 创建以pdf文件名为名称的文件夹保存pdf缩略图

File file = new File(filepath.substring(0, filepath

.lastIndexOf(".")));

if (!file.exists() && !file.isDirectory()) {

System.out.println("//不存在");

file.mkdir();

} else {

System.out.println("//目录存在");

}

String imagepath = "";

String smallimagepath = "";

// 将每一页的pdf转换为jpg缩略图

for (int i = 0; i < pages; i++) {

// 设置文件名

imagepath = jpgFilename(filepath, i);

tranferPer(document, rotation, zoom, imagepath, i);

//由缩略图生成指定宽高的jpg小图

smallimagepath = imagepath.substring(0,imagepath.lastIndexOf("."))+"-small"+".jpg";

zoomImage(imagepath, smallimagepath, 300, 200);

}

}

}

/**

* 设置jpg文件名

*

* @param filepath

* @param index

* @return

*/

public static String jpgFilename(String filepath, int index) {

String jpgFilename = "";

String folder = "";

index++;

if (filepath != null && !filepath.equals("")) {

folder = filepath.substring(filepath.lastIndexOf("\\") + 1,

filepath.lastIndexOf("."));

jpgFilename = filepath.substring(0, filepath.lastIndexOf("."))

+ "\\" + folder + "-" + index + "." + FILETYPE_JPG;

}

return jpgFilename;

}

/**

* 转换一页pdf为jpg缩略图大图

*

* @param document

* @param rotation

* @param zoom

* @param imagepath

* @throws PDFException

* @throws PDFSecurityException

* @throws IOException

*/

public static void tranferPer(Document document, float rotation,

float zoom, String imagepath, int index) throws PDFException,

PDFSecurityException, IOException {

float scale = 1f;

Page page = document.getPageTree().getPage(index, Object.class);

page.init();

PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, rotation, scale);

int pageWidth = (int) sz.getWidth();

int pageHeight = (int) sz.getHeight();

BufferedImage image = new BufferedImage(pageWidth, pageHeight,

BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

page.paint(g, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX,

rotation, scale);

g.dispose();

// capture the page image to file

try {

System.out.println("转换第 " + (index + 1) + " 页");

File file = new File(imagepath);

ImageIO.write(image, "jpg", file);

} catch (Throwable e) {

e.printStackTrace();

}

image.flush();

//由缩略图生成指定宽高的jpg小图

String smallimagepath = imagepath.substring(0,imagepath.lastIndexOf("."))+"-small"+".jpg";

zoomImage(imagepath, smallimagepath, 88, 126);

}

/**

* 由缩略图生成指定宽高的jpg小图

* @param srcFileName

* @param tagFileName

* @param width

* @param height

*/

public static void zoomImage(String srcFileName, String tagFileName,

int width, int height) {

try {

BufferedImage bi = ImageIO.read(new File(srcFileName));

BufferedImage tag = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(bi, 0, 0, width, height, null);

ImageIO.write(tag, "jpg", new File(tagFileName));

} catch (IOException e) {

e.printStackTrace();

}

}

}

测试结果

917eb5fb2b0d

下面在给大家弄个 生成的图片带水印的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值