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 支持批量转换 使用: 第一步,点击按钮添加文档到列表,或直接将待转换文 ...
随机推荐
- 头条一面竟然问我Maven?
maven package和maven install 有什么区别? 你常用的maven命令有哪些? <dependencyManagement> 是干什么的? 还有用过其它构建工具吗? ...
- [LOJ#2743][DP]「JOI Open 2016」摩天大楼
题目传送门 DP 经典题 考虑从小到大把数加入排列内 如下图(\(A\) 已经经过排序): 我们考虑如上,在 \(i\) ( \(A_i\) )不断增大的过程中,维护上面直线 \(y=A_i\) 之下 ...
- Promise.finally
const Gen = (time) => { return new Promise((resolve, reject) => { setTimeout(function () { if( ...
- 工作笔记-- 源码安装nginx
源码安装nginx 1.安装nginx的依赖包 [root@localhost ~]# yum -y install gcc gcc-c++ openssl openssl-devel pcre pc ...
- Git The requested URL returned error:403
一.问题描述 hexo部署的仓库需要换到另一个账号上,于是按照之前创建的步骤,修改了hexo安装目录下的deploy的repo地址,然而hexo s和hexo g没出错,但是hexo d时,会出错. ...
- mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。
昨天 去面试来着, 问了一下mong . 我记得mong支持 地理位置索引的,说了一下. 然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了.... 回去 查询了一下. 发现有 ...
- 最新Pyecharts-基本图表
Pyecharts是由Echarts而来,Echarts是百度开源的数据可视化的库,适合用来做图表设计开发,当使用Python与Echarts结合时就产生了Pyecharts.可使用pip安装,默认是 ...
- linux开机无法进入桌面直接进入initramfs模式的问题修复
可能是因为关机异常导致磁盘错误. kali linux升级到2019.4版本之后出现过好几次异常关机导致直接进入initramfs的模式,无法进入系统桌面,网上的办法基本上也都是无效的,前几次翻了很多 ...
- oc---instancetype和id的异同
[instancetype和id的异同] 相同点:都可以作为方法的返回类型. 不同点: (1)instancetype可以返回方法所在类相同类型的对象,id只能返回未知类型的对象: (2)instan ...
- Python中__init__的用法和理解
在Python中定义类经常会用到__init__函数(方法),首先需要理解的是,两个下划线开头的函数是声明该属性为私有,不能在类的外部被使用或访问.而__init__函数(方法)支持带参数类的初始化, ...