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转成图片的更多相关文章

  1. C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)

    原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...

  2. C#技术分享【PDF转换成图片——11种方案】

    1.[iTextSharp.dll],C# 开源PDF处理工具,可以任意操作PDF,并可以提取PDF中的文字和图片,但不能直接将PDF转换成图片. DLL和源码 下载地址:http://downloa ...

  3. 在linux环境下使用icepdf或pdfbox将pdf转化成图片是乱码解决

    在linux环境下使用icepdf或pdfbox将pdf转化成图片是出现乱码,网上查发下是itextpdf生成pdf引用"STSong-Light"字体而linux环境下没有这个字 ...

  4. 【PyMuPDF和pdf2image】Python将PDF转成图片

    前言: 在最近的测试中遇到一个与PDF相关的测试需求,其中有一个过程是将PDF转换成图片,然后对图片进行测试. 粗略的试了好几种方式,其中语言尝试了Python和Java,总体而言所找到的Python ...

  5. C# Asp.Net 实现PPT/PDF转成图片(不依赖office)

    最近公司有个需求,将PPT课件转成图片列表,然后在前端展示成轮播图,于是一开始通过Microsoft.Office.Interop.PowerPoint包实现了这个需求具体代码如下: /// < ...

  6. php 将pdf转成图片且将图片拼接

    说明: 1.pdf转图片通过安装php扩展imagick实现. 2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装. 3.为Centos 安装Imag ...

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

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

  8. C#pdf 切割成图片

    引用 using Ghostscript.NET;using Ghostscript.NET.Rasterizer; 需要安装 exe文件 public static GhostscriptVersi ...

  9. ImageMagick之PDF转换成图片(image)

    安装完ImageMagick之后,直接执行“magick convert f:\parseWord\tmp\testpdf.pdf f:\parseWord\tmp\testpdf.jpg”,会报错: ...

随机推荐

  1. [转载] linux查找目录下的所有文件中是否含有某个字符串

    链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...

  2. socket.io与redis构建实时推送

    准备工作 1:准备nodejs环境 2:使用npm工具安装 socket.io  npm install socket.io 3:下载客户端socket.io文件 解压附件中的文件 node app. ...

  3. jQuery设置聚焦并使光标位置在文字最后

    var editor = document.getElementById('btn'); editor.onfocus = function () { window.setTimeout(functi ...

  4. Android网络连接判断与处理

    博客分类: Android 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android ...

  5. 【 2013 Multi-University Training Contest 5 】

    HDU 4647 Another Graph Game 如果没有边的作用,显然轮流拿当前的最大值即可. 加上边的作用,将边权平均分给两个点,如果一个人选走一条边的两个点,就获得了边的权值:如果分别被两 ...

  6. Fibonacci(斐波那契)递归实现。容易看懂

    #include<iostream>using namespace std;int fibonacci(int n){if(n<=0) return 0; else if(n==1) ...

  7. ICEM(1)—边界结构网格绘制

    以两个圆为例 1. geometry→ create curve→ 选择圆,随便画两个圆 2. block下选择create block,选择第一项,initial block,设置改为2D Plan ...

  8. linux内核学习之一 简单c语言反汇编

    (我是第一次发技术博客的菜鸟,恳请大家指导!!) 一  由简单c程序生成汇编代码 首先给出本次我们要反汇编的简单c语言程序:(够简单吧~) 在linux环境中使用下面的命令条件编译: 生成汇编文件sh ...

  9. 关于HttpURLConnection.setFollowRedirects

    public static void HttpURLConnection.setFollowRedirects(boolean followRedirects)public void HttpURLC ...

  10. jqGrid属性中文详细说明 (转)

    jqGrid的属性很多,其实很大部分的属性,使用其默认值就可以了.但是详细了解一下属性的含义以及作用,对我们定制自己的grid是有帮助的. 以下内容描述格式是:属性名称 参数值类型    描述内容(可 ...