java利用renderer将pdf按页转换为图片
2018-01-17 10:12
579 查看
项目中遇到了需要把用户上传的word,execl,ppt每页截图保存。需要先用到jacob把资源转换为pdf,在通过pdf-renderer把每页截图下来。首先下载相关jar包:http://download.csdn.net/download/papima/10208770import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
//如果com.sun.image找不到,就是Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class pdfToImage {
public static void main(String[] args) {
String instructiopath="E:/临时文件1.pdf";
String picturepath = "E:/临时文件1/";
changePdfToImg(instructiopath,picturepath);
}
public static int changePdfToImg(String instructiopath,String picturepath) {
int countpage =0;
try {
//instructiopath ="D:/instructio/2015-05-16/Android 4编程入门经典.pdf"
//picturepath = "D:/instructio/picture/2015-05-16/";
File file = new File(instructiopath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
//创建图片文件夹
File dirfile = new File(picturepath);
if(!dirfile.exists()){
dirfile.mkdirs();
}
//获得图片页数
countpage = pdffile.getNumPages();
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox()
.getWidth()), ((int) page.getBBox().getHeight()));
int n = 2;
/** 图片清晰度(n>0且n<7)【pdf放大参数】 */
Image img = page.getImage(rect.width * n, rect.height * n,
rect, /** 放大pdf到n倍,创建图片。 */
null, /** null for the ImageObserver */
true, /** fill background with white */
true /** block until drawing is done */
);
BufferedImage tag = new BufferedImage(rect.width * n,
rect.height * n, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width * n,
rect.height * n, null);
/**
* File imgfile = new File("D:\\work\\mybook\\FilesNew\\img\\" +
* i + ".jpg"); if(imgfile.exists()){
* if(imgfile.createNewFile()) { System.out.println("创建图片:"+
* "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else {
* System.out.println("创建图片失败!"); } }
*/
FileOutputStream out = new FileOutputStream(picturepath+"/" + i
+ ".png");
/** 输出到文件流 */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
param2.setQuality(1f, true);
9c74
/** 1f~0.01f是提高生成的图片质量 */
encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);
/** JPEG编码 */
out.close();
}
channel.close();
raf.close();
/*unmap(buf);*/ //pdf转化成图片后,释放MappedByteBuffer资源。调用unmap(buf);无效。
/** 如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法 */
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return countpage;
}
@SuppressWarnings("unchecked")
public static void unmap(final Object buffer) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod(
"cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}成功释放MappedByteBuffer资源
Method m = FileChannelImpl.class.getDeclaredMethod("unmap",
MappedByteBuffer.class);
m.setAccessible(true);
m.invoke(FileChannelImpl.class, buf);
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
//如果com.sun.image找不到,就是Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class pdfToImage {
public static void main(String[] args) {
String instructiopath="E:/临时文件1.pdf";
String picturepath = "E:/临时文件1/";
changePdfToImg(instructiopath,picturepath);
}
public static int changePdfToImg(String instructiopath,String picturepath) {
int countpage =0;
try {
//instructiopath ="D:/instructio/2015-05-16/Android 4编程入门经典.pdf"
//picturepath = "D:/instructio/picture/2015-05-16/";
File file = new File(instructiopath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
//创建图片文件夹
File dirfile = new File(picturepath);
if(!dirfile.exists()){
dirfile.mkdirs();
}
//获得图片页数
countpage = pdffile.getNumPages();
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox()
.getWidth()), ((int) page.getBBox().getHeight()));
int n = 2;
/** 图片清晰度(n>0且n<7)【pdf放大参数】 */
Image img = page.getImage(rect.width * n, rect.height * n,
rect, /** 放大pdf到n倍,创建图片。 */
null, /** null for the ImageObserver */
true, /** fill background with white */
true /** block until drawing is done */
);
BufferedImage tag = new BufferedImage(rect.width * n,
rect.height * n, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width * n,
rect.height * n, null);
/**
* File imgfile = new File("D:\\work\\mybook\\FilesNew\\img\\" +
* i + ".jpg"); if(imgfile.exists()){
* if(imgfile.createNewFile()) { System.out.println("创建图片:"+
* "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else {
* System.out.println("创建图片失败!"); } }
*/
FileOutputStream out = new FileOutputStream(picturepath+"/" + i
+ ".png");
/** 输出到文件流 */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
param2.setQuality(1f, true);
9c74
/** 1f~0.01f是提高生成的图片质量 */
encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);
/** JPEG编码 */
out.close();
}
channel.close();
raf.close();
/*unmap(buf);*/ //pdf转化成图片后,释放MappedByteBuffer资源。调用unmap(buf);无效。
/** 如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法 */
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return countpage;
}
@SuppressWarnings("unchecked")
public static void unmap(final Object buffer) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod(
"cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}成功释放MappedByteBuffer资源
Method m = FileChannelImpl.class.getDeclaredMethod("unmap",
MappedByteBuffer.class);
m.setAccessible(true);
m.invoke(FileChannelImpl.class, buf);
相关文章推荐
- pdf-renderer 把 pdf 转换成图片时报:java.io.IOException: Unknown CMap: UniGB-UCS2-H 的解决办法
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
- 一个非常不错的纯源码“PDF转换swf”完全脱离安装第三方插件,直接使用java调用Flash api将PDF转换成图片,在将图片转换成swf。
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- java 将pdf多页转换成一张图片,支持指定页数
- Java利用OpenOffice将word等office文档转换成PDF
- Java利用OpenOffice将word等office文档转换成PDF
- Ubuntu下Java使用pdfbox将pdf转换为图片的方法及问题
- Java利用OpenOffice将word等office文档转换成PDF
- 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片
- 在Java代码中使用pdfBox将PDF转换为图片
- java实现PDF转图片(每页转换成一张图片,可单页转换或指定页数)
- 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片
- Java利用OpenOffice将word等office文档转换成PDF
- 利用pdfbox和itext包将pdf转换为图片
- JAVA 将图片转换成pdf文件
- java使用itextpdf将图片转换成pdf时DPI的问题
- 用java将pdf转换成jpg图片的代码
- JAVA使用pdfbox将pdf转换成图片
- java 利用jacob将word转换成PDF