写的这些功能主要就是放在定时任务上,每分钟查询是否要有转换的图片,有的换然后就转换,前端展示图片就可以,大家可以浏览图片。不用在去下载哪些文件了。
下面的是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();
}
}
}
测试结果
下面在给大家弄个 生成的图片带水印的