寫的這些功能主要就是放在定時任務(wù)上,每分鐘查詢是否要有轉(zhuǎn)換的圖片那婉,有的換然后就轉(zhuǎn)換板甘,前端展示圖片就可以,大家可以瀏覽圖片详炬。不用在去下載哪些文件了盐类。
下面的是pdf轉(zhuǎn)圖片加縮略圖的
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 創(chuàng)建時間: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();
}
}
/**
* 轉(zhuǎn)換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);
// 創(chuàng)建以pdf文件名為名稱的文件夾保存pdf縮略圖
File file = new File(filepath.substring(0, filepath.lastIndexOf(".")));
if (!file.exists() && !file.isDirectory()) {
System.out.println("http://不存在");
file.mkdir();
} else {
System.out.println("http://目錄存在");
}
String imagepath = "";
// 設(shè)置文件名
imagepath = jpgFilename(filepath, 0);
// 轉(zhuǎn)換首頁為jpg縮略圖
tranferPer(document, rotation, zoom, imagepath, 0);
}
/**
* 轉(zhuǎn)換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總頁數(shù)
int pages = document.getNumberOfPages();
if (pages > 0) {
// 創(chuàng)建以pdf文件名為名稱的文件夾保存pdf縮略圖
File file = new File(filepath.substring(0, filepath
.lastIndexOf(".")));
if (!file.exists() && !file.isDirectory()) {
System.out.println("http://不存在");
file.mkdir();
} else {
System.out.println("http://目錄存在");
}
String imagepath = "";
String smallimagepath = "";
// 將每一頁的pdf轉(zhuǎn)換為jpg縮略圖
for (int i = 0; i < pages; i++) {
// 設(shè)置文件名
imagepath = jpgFilename(filepath, i);
tranferPer(document, rotation, zoom, imagepath, i);
//由縮略圖生成指定寬高的jpg小圖
smallimagepath = imagepath.substring(0,imagepath.lastIndexOf("."))+"-small"+".jpg";
zoomImage(imagepath, smallimagepath, 300, 200);
}
}
}
/**
* 設(shè)置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;
}
/**
* 轉(zhuǎn)換一頁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("轉(zhuǎn)換第 " + (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();
}
}
}
測試結(jié)果
下面在給大家弄個 生成的圖片帶水印的