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 支持批量转换 使用: 第一步,点击按钮添加文档到列表,或直接将待转换文 ...
随机推荐
- 缓存读写策略 - Cache Aside.md
场景描述 比如一条数据同时存在数据库.缓存,现在你要更新此数据,你会怎么更新? 先更新数据库?还是先更新缓存? 其实这两种方式都有问题. (1)先更新数据库,后更新缓存 这样会造成数据不一致. A 先 ...
- CDH大数据平台搭建终极版
经过无数次的失败,终于将CDH安装到两台普通的笔记本电脑上,主要失败原因有以下几点: 不熟悉安装过程,官方给出的安装方法有三种,所以都尝试了一遍,浪费了大量时间,所以有时候方法多不见得是一件好事. 安 ...
- windows下RocketMQ安装部署
一.预备环境 1.系统 Windows 2. 环境 JDK1.8.Maven.Git 二. RocketMQ部署 1.下载 1.1地址:http://rocketmq.apache.org/relea ...
- MNIST数据集
一.MNIST数据集分类简单版本 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data # ...
- .NET 框架设计 - 模式、配置、工具 Demo 搜索
ps:http://www.ituring.com.cn/article/195770 看完第二章,我也忍了!但是随着第三章“副本”的开启,我的阅读速度开始慢慢的减缓,虽然对其中每一个程序模式的理论也 ...
- GP工作室—Alpha版本发布1
目录 GP工作室-Alpha版本发布1 一.简介 1.1作业要求 1.2团队成员 二.软件下载安装说明 五.项目总结 @(Gold Point团队の项目计划) GP工作室-Alpha版本发布1 一.简 ...
- 使用LD_PRELOAD注入程序
LD_PRELOAD是Linux系统的一个环境变量,它可以影响程序的运行时的链接(Runtime linker),它允许你定义在程序运行前优先加载的动态链接库.这个功能主要就是用来有选择性的载入不同动 ...
- Java类加载器和双亲委派机制
前言 之前详细介绍了Java类的整个加载过程(类加载机制详解).虽然,篇幅较长,但是也不要被内容吓到了,其实每个阶段都可以用一句话来概括. 1)加载:查找并加载类的二进制字节流数据. 2)验证:保证被 ...
- 谷歌翻译API
http://translate.google.cn/translate_a/t?client=t&text=你好&hl=zh-CN&sl=zh-CN&tl=en&am ...
- tricky c++ new(this)
题目如下:问下列代码的打印结果为0吗? #include <stdlib.h> #include <iostream> using namespace std; struct ...