首页 技术 正文
技术 2022年11月6日
0 收藏 463 点赞 406 浏览 2466 个字

问题:对于上次解决PB调用不起PDF插件的问题,所以将PDF转换为HTML。但是转换完之后发现一个问题,pdf2htmlEX插件转换出的html只支持IE9以上,IE9以下的都会各式错乱,

所以只能换一种思路,将PDF转换为JPG来显示,并且实现了多张PDF在一张图片中显示,而且中文不会乱码

下面贴出代码和jar包下载的地址供大家使用

package org.common.util.pdftohtml;import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;import javax.imageio.ImageIO;import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;public class PdfToJpg
{
public static void main(String[] arguments)
{
pdftoimage("D:\\pdf2htmlEX-v1.0\\PDF\\1.pdf","D:\\pdf2htmlEX-v1.0\\PDF\\a.jpg");
} public static void pdftoimage(String pdfpath,String imagepath){
Document document = new Document(); try
{
document.setFile(pdfpath);
}
catch (Exception e1)
{
e1.printStackTrace();
} int pages = document.getNumberOfPages(); System.out.println("pages " + pages); BufferedImage image = null; RenderedImage rendImage = null;
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
for( int i = 0 ; i < pages ; i++ )
{
image = (BufferedImage)document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_TRIMBOX, 0f, 1.5f);; rendImage = image;
piclist.add(image);
}
yPic(piclist, imagepath);
document.dispose(); } /**
* 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同
*
* @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_RGB);
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);
ImageIO.write(imageResult, "jpg", outFile);// 写图片
new FileOutputStream(outFile).close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

jar包地址:http://pan.baidu.com/s/1hsLpbu8

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289