package com.ces.component.pictrueCut.entity;
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;
public class pdfToImage { 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);
/** 1f~0.01f是提高生成的图片质量 */
encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);
/** JPEG编码 */
out.close();
}
channel.close();
raf.close();
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;
}
});
}
}

java实现pdf按页切分成图片的更多相关文章

  1. 【PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异常】

    public static void main(String[] args) throws IOException { /** * PDF单页转化为图片输出 注意:英文或图片类的PDF可转化,中文抛异 ...

  2. java操作pdf添加页眉条码添加水印图片

    添加条码页眉以及图片水印 1. 引入jar包     1. itext-4.2.1.jar     2. itext-asian-5.2.0.jar     3. jbarcode-0.2.8.jar ...

  3. Java 设置PDF跨页表格重复显示表头行

    在创建表格时,如果表格内容出现跨页显示的时候,默认情况下该表格的表头不会在下一页显示,在阅读体验上不是很好.下面分享一个方法如何在表格跨页是显示表格的表头内容,这里只需要简单使用方法 grid.set ...

  4. java实现截取PDF指定页并进行图片格式转换

    1.引入依赖 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox& ...

  5. Java 处理PDF图章(印章)——图片图章、动态图章

    图章(印章)是一种在合同.票据.公文等文件中表明法律效应.部门机关权威的重要指示物,常见于各种格式的文件.文档中.对于纸质文档可以手动盖章,但对于电子文档,则需要通过特定的方法来实现.本篇文档分享通过 ...

  6. Java 读取PDF中的文本和图片

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取.   使用工具:Free Spire.PDF for Ja ...

  7. JAVA将一个pdf按页码拆分成多个pdf

    引入maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf< ...

  8. Java PDF转换成图片并输出给前台展示

    首先需要导入所需工具类 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fo ...

  9. iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片

    本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...

随机推荐

  1. 使用CURL进行模拟登录

    在信息采集的时候,要采集的站点可能需要登录,这样使用简单的采集方式(例如file_get_contents)就无法做到了,我们可以利用PHP的CURL扩展库来进行模拟登录,下面给出代码示例: < ...

  2. librdkafka安装和php扩展php-rdkafka安装

    1.安装librdkafka mac下   brew install librdkafka linux下 git clone https://github.com/edenhill/librdkafk ...

  3. noj快排

    1003.快速排序 时限:1000ms 内存限制:10000K  总时限:3000ms 描述 给定一个数列,用快速排序算法把它排成升序.   输入 第一行是一个整数n(n不大于10000),表示要排序 ...

  4. Tex_安装_在Ubuntu系统下

    $\LaTeX$是一个强大的排版软件,在数学公式.表格.甚至是科学绘图方面有着独特优势.本文在Ubuntu系统下,整理Tex安装相关的操作,以为备忘.所引链接都未同作者商量,如有不妥望及时告知. 命令 ...

  5. Post Order traverse binary tree using non-recursive way

    Process analysis Stack = 5,  Push 3, Stack = 5, 3.    Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...

  6. [LeetCode&Python] Problem 844. Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  7. js调用app启动页

    第一步:添加js $(function () { var ua = window.navigator.userAgent.toLowerCase(); //微信 if(ua.match(/MicroM ...

  8. jquery绑定回车事件

    //回车事件绑定 $(".left-content").keyup(function(event){ var theEvent = event || window.event; v ...

  9. 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)

    排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...

  10. IntelliJ IDEA 2018.3 升级功能介绍

    |0前言 2018.11.28 IntelliJ IDEA 2018.3 正式版发布.对于一个忠实爱好者,迫不及待的我下载了最新版本来体验下.而且 IDEA 今年的第三次重大更新提供了不容错过的显著功 ...