pdf转成图片
http://blog.csdn.net/wydhao123/article/details/51596028
jar commons-logging.jar、 pdfbox-2.0.1.jar、fontbox-2.0.1.jar
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer; public class PDFBOXCreate { public static void main(String[] args)throws Exception {
long start = System.currentTimeMillis();
String filepath = "D:/Users/ex-wangyide001/workspace/testsrc/1.pdf";
PDDocument document = new PDDocument();
File pdfFile = new File(filepath);
document = PDDocument.load(pdfFile, (String)null);
int size = document.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList();
for(int i=0 ; i < size; i++){
BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i,130,ImageType.RGB);
piclist.add(image);
}
document.close();
yPic(piclist,"D:/Users/ex-wangyide001/workspace/testsrc/PDFBox1.jpg");
/* FileOutputStream out = new FileOutputStream("D:/Users/ex-wangyide001/workspace/testsrc/yanglaoxian/PDFBox-"+i+".jpg");
ImageIO.write(image, "jpg", out);
out.close();*/
long end = System.currentTimeMillis();
System.out.println(end-start);
} /**
* 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同
*
* @param piclist
* 文件流数组
* @param outPath
* 输出路径
*/
public static void yPic(List<BufferedImage> piclist, String outPath) {// 纵向处理图片
if (piclist == null || piclist.size() <= 0) {
System.out.println("图片数组为空!");
return;
}
try {
int height = 0, // 总高度
width = 0, // 总宽度
_height = 0, // 临时的高度 , 或保存偏移高度
__height = 0, // 临时的高度,主要保存每个高度
picNum = piclist.size();// 图片的数量
File fileImg = null; // 保存读取出的图片
int[] heightArray = new int[picNum]; // 保存每个文件的高度
BufferedImage buffer = null; // 保存图片流
List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB
int[] _imgRGB; // 保存一张图片中的RGB数据
for (int i = 0; i < picNum; i++) {
buffer = piclist.get(i);
heightArray[i] = _height = buffer.getHeight();// 图片高度
if (i == 0) {
width = buffer.getWidth();// 图片宽度
}
height += _height; // 获取总高度
_imgRGB = new int[width * _height];// 从图片中读取RGB
_imgRGB = buffer
.getRGB(0, 0, width, _height, _imgRGB, 0, width);
imgRGB.add(_imgRGB);
}
_height = 0; // 设置偏移高度为0
// 生成新图片
BufferedImage imageResult = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
for (int i = 0; i < picNum; i++) {
__height = heightArray[i];
if (i != 0)
_height += __height; // 计算偏移高度
imageResult.setRGB(0, _height, width, __height, imgRGB.get(i),
0, width); // 写入流中
}
File outFile = new File(outPath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(imageResult, "jpg", out);// 写图片
byte[] b = out.toByteArray();
FileOutputStream output = new FileOutputStream(outFile);
output.write(b);
out.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
pdf转成图片的更多相关文章
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)
原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...
- C#技术分享【PDF转换成图片——11种方案】
1.[iTextSharp.dll],C# 开源PDF处理工具,可以任意操作PDF,并可以提取PDF中的文字和图片,但不能直接将PDF转换成图片. DLL和源码 下载地址:http://downloa ...
- 在linux环境下使用icepdf或pdfbox将pdf转化成图片是乱码解决
在linux环境下使用icepdf或pdfbox将pdf转化成图片是出现乱码,网上查发下是itextpdf生成pdf引用"STSong-Light"字体而linux环境下没有这个字 ...
- 【PyMuPDF和pdf2image】Python将PDF转成图片
前言: 在最近的测试中遇到一个与PDF相关的测试需求,其中有一个过程是将PDF转换成图片,然后对图片进行测试. 粗略的试了好几种方式,其中语言尝试了Python和Java,总体而言所找到的Python ...
- C# Asp.Net 实现PPT/PDF转成图片(不依赖office)
最近公司有个需求,将PPT课件转成图片列表,然后在前端展示成轮播图,于是一开始通过Microsoft.Office.Interop.PowerPoint包实现了这个需求具体代码如下: /// < ...
- php 将pdf转成图片且将图片拼接
说明: 1.pdf转图片通过安装php扩展imagick实现. 2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装. 3.为Centos 安装Imag ...
- Java PDF转换成图片并输出给前台展示
首先需要导入所需工具类 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fo ...
- C#pdf 切割成图片
引用 using Ghostscript.NET;using Ghostscript.NET.Rasterizer; 需要安装 exe文件 public static GhostscriptVersi ...
- ImageMagick之PDF转换成图片(image)
安装完ImageMagick之后,直接执行“magick convert f:\parseWord\tmp\testpdf.pdf f:\parseWord\tmp\testpdf.jpg”,会报错: ...
随机推荐
- LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2
https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...
- ASP通过ADODB读取Access数据库
<% On Error Resume Next set conn=server.createobject("adodb.connection") mypath=server. ...
- Linux中的历史命令
Linux中的历史命令一般保存在用户 /root/.bash_history history 选项 历史命令保存文件夹 选项 -c:清空历史命令 -w :把缓存中的历史命令写入历 ...
- vmware虚拟机网络自动断开的问题
最近搭建一个集群环境,因此用vmware安装了几台虚拟机,系统是centos7.2. 但是发现网络总是不经意间自动断开,重启网络(service network restart)恢复. 虚拟机网络类型 ...
- 3、Android Intent Flag的介绍
介绍:http://blog.csdn.net/luckily01/article/details/7737499
- CDS
very nice artical talk about mergechangelog and cleardataset Delta and Data http://www.cnblogs.com/y ...
- 如何查看前端部署的tracker代码
1.www.gov.cn 2.F12>Source>左侧选择static.gridsumdissector.com/js 3.点击代码下方区域的中括号,展开代码preety print{}
- iOS---XMPP环境搭建过程
什么是即时通信? 即时通信是目前Internet上最为流行的通讯方式, 各种各样的即时通讯软件也层出不穷, 服务提供商也提供了越来越枫木的通讯服务功能. 即时通讯有多重实现方式, XMPP就是其中一种 ...
- Python的平凡之路(19)
一.Django请求生命周期 对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端 ...
- 用css实现条纹背景
我先额外的说一下怎么用CSS绘制三角形: 绘制三角形是把边框加粗,将元素的宽高都设为0,让其余的边框颜色透明,下面我们来看实现的代码: 先把边框的颜色设置成不同颜色: #div{ border-col ...