pdf转图片(qq:2798641729)
前面已经把html转成pdf,但是用户可以下载图片格式的文件,所以我们必须把pdf转成图片格式,代码如下
package com.jit.platform.basics.util.pdf; import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List; import javax.imageio.ImageIO; import org.jpedal.PdfDecoder; class ImgPp{
BufferedImage img;
int width ;
int height;
public BufferedImage getImg() {
return img;
}
public void setImg(BufferedImage img) {
this.img = img;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
} } public class PdfToImg { public static List<String> toImgList(String pdfPath,String imgPath,String imgName) throws Exception{ PdfDecoder decode_pdf = new PdfDecoder(true);
decode_pdf.openPdfFile(pdfPath); //file
int start = 1, end = decode_pdf.getPageCount(); List<String> list = new ArrayList<String>();
for(int i=start;i<end+1;i++){
BufferedImage img=decode_pdf.getPageAsImage(i);
String fileName = imgPath + imgName+"_"+i +".png";
ImageIO.write(img, "png", new File(fileName));
list.add(fileName);
}
decode_pdf.closePdfFile(); return list; } public static String toImgOne(String pdfPath,String imgPath,String imgName) throws Exception { PdfDecoder decode_pdf = new PdfDecoder(true);
decode_pdf.openPdfFile(pdfPath); //file
int start = 1, end = decode_pdf.getPageCount(); List<ImgPp> list = new ArrayList<ImgPp>();
int width = 0;
int totalHeight = 0;
for(int i=start;i<end+1;i++){
BufferedImage img=decode_pdf.getPageAsImage(i);
ImgPp imgpp = new ImgPp();
imgpp.setImg(img);
imgpp.setWidth(img.getWidth());
imgpp.setHeight(img.getHeight());
totalHeight+=img.getHeight();
width=img.getWidth();
list.add(imgpp);
} String mergeImage = mergeImage(list ,totalHeight,width ,imgPath,imgName); decode_pdf.closePdfFile(); return mergeImage; } public static String mergeImage(List<ImgPp> list,int totalHeight,int width,String imgPath,String imgName) throws Exception { //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度,高度为各个图片高度之和
BufferedImage tag = new BufferedImage(width, totalHeight, BufferedImage.TYPE_INT_RGB);
//绘制合成图像
Graphics g = tag.createGraphics(); int tempHeight = 0;
for (int i = 0; i < list.size(); i++) {
ImgPp imgPp = list.get(i);
g.drawImage(imgPp.getImg(), 0, tempHeight, width, imgPp.getHeight(), null);
tempHeight+=imgPp.getHeight();
}
// 释放此图形的上下文以及它使用的所有系统资源。
g.dispose(); // Save as new image
ImageIO.write(tag, "png", new File(imgPath + imgName));
return imgName; }
public static void main(String[] args) { String pdfPath = "D:\\many page.pdf";
String imgPath = "D:\\";
String imgName = "pdfbox_image";
try {
String imgOne = toImgOne(pdfPath,imgPath,imgName);
System.out.println("imgOne"+imgOne); /*List<String> img2 = toImgList(pdfPath,imgPath,imgName); File[] fileArray = new File[img2.size()];
for (int i = 0; i < img2.size(); i++) {
//System.out.println(img2.get(i));
File file = new File(img2.get(i));
fileArray[i] = file;
}
BatchDownloadAction.makeZip(imgPath,imgName , fileArray);*/ } catch (Exception e1) {
e1.printStackTrace();
} } }
项目中遇到了用户还可以下载图片格式的文件,所以我们就需要把pdf转成图片,下面是我们完成的代码。
pdf转图片(qq:2798641729)的更多相关文章
- C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器.使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下 ...
- 13种PDF转图片的案列
Acrobat.dllc#PDFPDFRender4NET.dllpdf转图片 GitHub Clone Adress : https://github.com/stone0090/OfficeToo ...
- C# 给PDF添加图片背景
C# 给PDF添加图片背景 今天要实现的是给PDF文件添加图片背景这个功能.PDF是近年来最流行的文件之一,无论是办公还是日常生活中都经常会用到,很多时候,PDF文件的背景色都是白色,看多了难免觉得累 ...
- PDF转图片 C# with Adobe API
PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.
- 在线提取PDF中图片和文字
无需下载软件,你就可以在线提取PDF中图片和文字,http://www.extractpdf.com/不仅可以获取本地PDF文档的图片和文字,还能获取远程PDF文档的图片和文字.如下图所示:结果本人测 ...
- .Net的PDF转图片
用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可 public static class PdfToImage { , , ) { try { / ...
- 使用pdfbox分页保存pdf为图片
一.背景 pdfbox作为Apache开源的PDF操作工具,允许创建新的PDF文档,操作现有文档,以及从文档中提取内容的能力.Apache PDFBox还包括一些命令行实用工具.本文楼主主要介绍其中的 ...
- JAVA中pdf转图片的方法
JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...
- 【JAVA】pdf转图片
最近java项目中使用到了pdf转图片的需求,在此记录一下. 1.基于GhostScript p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px ...
- PDF转图片工具
点击下载( 提取码:1ll1 ) 软件功能基于mupdf,UI使用wxpython开发 功能: 支持pdf转图片,图片格式png 支持批量转换 使用: 第一步,点击按钮添加文档到列表,或直接将待转换文 ...
随机推荐
- MySQL日志及索引
MySQL物理结构: MySQL它是通过文件系统对数据进行储存和管理,从物理结构上分为日志文件和数据文件 日志文件: 日志文件记录了数据库操作的信息和一些错误信息,我们常用的日志文件有:错误日志.二进 ...
- 动画 | 什么是平衡二分搜索树(AVL)?
二分搜索树又名有序二叉查找树,它有一个特点是左子树的节点值要小于父节点值,右子树的节点值要大于父节点值.基于这样的特点,我们在查找某个节点的时候,可以采取二分查找的思想快速找到这个节点,时间复杂度期望 ...
- xsd 和 wsdl
xsd : 可用方便 不同的语言之间的 用命令行来 转换对应语言的. wsdl: 可用方便不同语言的类描述 用命令行 来相互转换. 类似 thift me ?
- axios请求方法封装.
axios的使用上一般封装好对应的方法,ES6导出,直接调用,消息通知使用了ElementUI的Message组件. 这是一个封装了axios的Rest风格的工具类,包扩常用的POST,GET,PUT ...
- 初学者学Java常遇到的问题,我都给你回答了!
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 春节在家刷知乎,看到了一个知乎的问题:<学 ...
- sqli_labs学习笔记(一)Less-21~Less-37
续上,开门见山 Less-21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入) 登录后页面 圈出来的地方显 ...
- 文件上传三:base64编码上传
介绍三种上传方式: 文件上传一:伪刷新上传 文件上传二:FormData上传 文件上传三:base64编码上传 Flash的方式也玩过,现在不推荐用了. 优点: 1.浏览器可以马上展示图像,不需要先上 ...
- 架构模式中的Active Record和Data Mapper
架构模式中的Active Record和Data Mapper 概念 在简单应用中,领域模型是一种和数据库结构一致的简单结构,对应每个数据库表都有一个领域类,在这种情况下,有必要让每个对象负责数据库的 ...
- shiro中ecache-core版本引起的异常
ecache-core包版本不对引起的错误,将2.5.3换成2.4.5就好了 来源 WARN [RMI TCP Connection(3)-127.0.0.1] - Exception encount ...
- java刷题(1-5)
第一题:在一个数组中找出三个数相加之和为0,且不重复的集合 import java.lang.reflect.Array; import java.security.PublicKey; import ...