1、引入依赖

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.16</version>
</dependency>
jar包下载地址:
https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox
 
2、实现DEMO
package com.dddpeter.app;

import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.ListIterator; public class PDFUtils {
public static String splitPdf(int pageNum, String source, String dest) {
File indexFile = new File(source);
File outFile = new File(dest);
PDDocument document = null;
try {
document = PDDocument.load(indexFile);
// document.getNumberOfPages();
Splitter splitter = new Splitter();
splitter.setStartPage(pageNum);
splitter.setEndPage(pageNum);
List<PDDocument> pages = splitter.split(document);
ListIterator<PDDocument> iterator = pages.listIterator();
while (iterator.hasNext()) {
PDDocument pd = iterator.next();
if (outFile.exists()) {
outFile.delete();
}
pd.save(outFile);
pd.close();
if (outFile.exists()) {
return outFile.getPath();
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void pdfFileToImage(File pdffile,String targetPath){
try {
FileInputStream instream = new FileInputStream(pdffile);
InputStream byteInputStream=null;
try {
PDDocument doc = PDDocument.load(instream);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
if (pageCount > 0) {
BufferedImage image = renderer.renderImage(0, 4.0f);
image.flush();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut;
imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "png", imOut);
byteInputStream = new ByteArrayInputStream(bs.toByteArray());
byteInputStream.close();
}
doc.close();
}
catch (IOException e) {
e.printStackTrace();
}
File uploadFile = new File(targetPath);
FileOutputStream fops;
fops = new FileOutputStream(uploadFile);
fops.write(readInputStream(byteInputStream));
fops.flush();
fops.close();
}
catch (Exception e) {
e.printStackTrace();
}
} public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
} public static void main(String[] args) {
String path = splitPdf(4,"D:\\data\\11.pdf","D:\\data\\out11.pdf");
File file =new File(path);
//上传的是png格式的图片结尾
String targetfile="D:\\data\\out11.png";
pdfFileToImage(file,targetfile); }
}

其中指定第几页可配合https://www.cnblogs.com/xsdty/p/11463174.html

实现动态获取关键字页码,动态截取转换为自己想要的格式图片。

java实现截取PDF指定页并进行图片格式转换的更多相关文章

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

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

  2. JAVA将Excel中的报表导出为图片格式(一)问题背景

    如题所示,先抛出一个问题,如何使用JAVA将Excel中的报表导出为图片格式? 首先说一下这个问题的背景,也就是为什么博主会碰到这个问题 随着微信,易信之流大行其道,企业内部的办公交流.绩效考评甚至考 ...

  3. C# / VB.NET合并PDF指定页

    在前面的文章中,我们已经知道如何合并.拆分多个PDF文件,在这篇文章中的合并.拆分PDF文档主要是以方便文档管理的目的来操作文档,在文档查阅.管理及存储上很方便实用.但是我们如果想要合并多个文档中的部 ...

  4. word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法

    公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...

  5. java实现pdf按页切分成图片

    package com.ces.component.pictrueCut.entity; import java.awt.Image; import java.awt.Rectangle; impor ...

  6. Java 给Word每一页设置不同图片水印效果

    Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以Java代码为例, ...

  7. 一个简单的定向python爬虫爬取指定页面的jpg图片

    import requests as r import re resul=r.get("http://www.imooc.com/course/list") urlinfo=re. ...

  8. JAVA将Excel中的报表导出为图片格式(三)换一种实现

    上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...

  9. JAVA将Excel中的报表导出为图片格式(二)实现思路

    接上文,一封类似于下方设计的Excel报表,如何将它指定的区域导出为样式一模一样的JPG图片呢? 要实现这个功能没有现成的解决方案,谷歌度娘了好久也没有,最终自己想了几条思路: 思路1:将报表中的背景 ...

随机推荐

  1. 从三个语言(C++,Java,.Net)的几个性能测试案例来看性能优化

    随着时间的发展,现在的虚拟机技术越来越成熟了,在有些情况下,Java,.Net等虚拟机密集计算的性能已经和C++相仿,在个别情况下,甚至还要更加优秀.本文详细分析几个性能测试案例,探讨现象背后的原因. ...

  2. 我的ubuntu kylin中mentohust的使用历程

    1首先下载mentohus 最新版下载(包括源码):http://code.google.com/p/mentohust/downloads/list 2打开终端(Ctrl+Alt+T) 输入sudo ...

  3. 非UI线程更新界面

    package com.caterSys.Thread; import java.text.SimpleDateFormat; import java.util.Date; import org.ec ...

  4. x32下PsSetLoadImageNotifyRoutine的逆向

    一丶简介 纯属兴趣爱好.特来逆向玩玩. PsSetLoadImageNotifyRoutine 是内核中用来监控模块加载.操作系统给我们提供的回调. 我们只需要填写对应的回调函数原型即可进行加监控. ...

  5. BFS DFS模板

    转载于https://blog.csdn.net/alalalalalqp/article/details/9155419 BFS模板: #include<cstdio> #include ...

  6. opencv图像直方图均衡化及其原理

    直方图均衡化是什么有什么用 先说什么是直方图均衡化,通俗的说,以灰度图为例,原图的某一个像素为x,经过某个函数变为y.形成新的图.新的图的灰度值的分布是均匀的,这个过程就叫直方图均衡化. 图像直方图均 ...

  7. 低版本IE兼容 H5+CSS3 方案

    [主要是针对ie6 7 8对支持和让老浏览器支持html5+css3的一些js脚本] html5shiv.js  // 让IE8及耕地版本的IE识别section,article,nav等html5元 ...

  8. python random 模块及验证码功能

    random模块 import random random.random() random.randint(1,3) # 1-3的整数包括3 import random print(random.ra ...

  9. pycharm的安装配置及思维导图

    1.1 计算机基础知识 主板:人的骨架,用于扩展设备的 cpu:人的大脑,用于计算和逻辑处理的 硬盘:存储数据(永久存储) 电源:人的心脏 内存:存储数据(临时存储) 断电即消失 操作系统 xp wi ...

  10. Java学习|HTTP请求头

    https://www.cnblogs.com/honghong87/articles/6941436.html 常见http请求报文头属性      Accept:告诉服务端,客户端接受什么类型的响 ...