import 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;
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;
/**
* pdf转html
* @author Administrator
*
*/
public class PDFchangToImage {
public static void main(String[] args) {
PDFchangToImage.changePdfToImg();
}
private static void changePdfToImg() {
try {
File file = new File("D:\\aa1\\WS1-(X-003)-2009Z.pdf");
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);
Long randStr = 0l;
randStr = System.currentTimeMillis();
FileOutputStream fos;
StringBuffer buffer = new StringBuffer();
buffer.append("<!doctype html>\r\n");
buffer.append("<head>\r\n");
buffer.append("<meta charset=\"UTF-8\">\r\n");
buffer.append("</head>\r\n");
buffer.append("<body style=\"background-color:gray;\">\r\n");
buffer.append("<style>\r\n");
buffer.append("img {background-color:#fff; text-align:center; width:100%; max-width:100%;margin-top:6px;}\r\n");
buffer.append("</style>\r\n");
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 = 6;
/** 图片清晰度(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("D:\\aa1\\" + i+ "1003.jpg");
/** 输出到文件流 */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);
param2.setQuality(1f, true);
/** 1f~0.01f是提高生成的图片质量 */
encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);
/** JPEG编码 */
out.close();
buffer.append("<img src=\"" + "D:\\aa1\\" + i + "1003.jpg\"/>\r\n");
}
channel.close();
raf.close();
unmap(buf);
buffer.append("</body>\r\n");
buffer.append("</html>");
// 生成网页文件
fos = new FileOutputStream("D:\\aa1\\"+ randStr + ".html");
System.out.println("D:\\aa1\\" + randStr + ".html");
fos.write(buffer.toString().getBytes());
fos.flush();
fos.close();
/** 如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法 */
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private 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;
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论



















收起资源包目录


















































共 40 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论

- 孤独的行走2019-09-03确定是拆分单页面吗????
tds411255224
- 粉丝: 1
- 资源: 3

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
