icepdf和pdfbox转pdf文档为图片
icepdf转pdf文档为图片
首先导入icepdf jar包或maven
pdfPath为pdf文件路径、pdfimgpsth为图片保存的路径
public static void icePdfImg(String pdfPath,String pdfimgpsth,HttpServletRequest request){
Document document=new Document();
try{
document.setFile(pdfPath);
float scale=1f;
float rotation=0f;
for(int i=0;i<document.getNumberOfPages();i++){
BufferedImage image=(BufferedImage)document.getPageImage(i,
GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX,
rotation,scale);
RenderedImage rendImage=image;
File outFile = new File(pdfimgpsth+"/imgpdf-"+i+".png");
ImageIO.write(rendImage,"png",outFile);
image.flush();
}
document.dispose();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
pdfbox 转pdf文档为图片,但是不好用,没有icepdf好用
public static final float DEFAULT_DPI = 105;
public static final String DEFAULT_FORMAT = "jpg";
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
public static void pdfToImage2(String pdfPath,HttpServletRequest request) throws IOException {
try {
int width = 0;
int[] singleImgRGB;
int shiftHeight = 0;
BufferedImage imageResult = null;
PDDocument pdDocument = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pageLength = pdDocument.getNumberOfPages();
int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
for (int m = 0; m < totalCount; m++) {
for (int i = 0; i < pageLength; i++) {
int pageIndex = i + (m * pageLength);
if (pageIndex == pdDocument.getNumberOfPages()) {
break;
}
BufferedImage image = renderer.renderImageWithDPI(pageIndex, 96, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
if (i == 0) {
width = imageWidth;
if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
} else {
imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
}
} else {
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
@SuppressWarnings("deprecation")
String pt = request.getRealPath("/")+"outpdf.jpg";
File outFile = new File(pt);
ImageIO.write(imageResult, "jpg", outFile);
shiftHeight = 0;
}
pdDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
}
最新文章
- 用soapUI测试webservice
- BookBlock - 效果非常真实的书本翻页预览
- BestCoder Round #66 (div.2)
- Team Queue (uva540 队列模拟)
- bootstrap ace treeview树表
- sql cast()和convert()
- WeCenter程序安装
- 学习java之HashMap和TreeMap
- YASKAWA电机控制(1)---接线
- PHP高效的敏感词过滤方法
- tomcat https 未测试成功的版本
- 【Java 小实验】重写(覆写 Override)返回值类型能不能相同
- 基于visual Studio2013解决C语言竞赛题之1019填数
- 蓄水池抽样(原理&;实现)
- 配置nginx服务器 —— Nginx添加多个二级子域名
- JSON(一)——JSON与JavaScript的关系
- 第六次作业-my Backlog
- [UE4]用Blenspace混合空间动画代替AimOffset动画偏移
- 安装Scrapy遇到的坑
- onload属性使用方法
热门文章
- 关于利用python进行验证码识别的一些想法
- Java生鲜电商平台-电商中";再来一单";功能架构与详细设计(APP/小程序)
- 【LeetCode】18.四数之和
- C# 便捷实现可迭代对象间的赋值
- coderforces Gym 100803A/Aizu 1345/CSU 1536/UVALive 6832 Bit String Reordering(贪心证明缺)
- Elasticsearch+spring cloud201912301423
- Javascript/Jquery遇到字符串自动NaN的问题
- E - 不爱学习的lyb HDU - 1789(贪心策略)
- 编写SpringBoot 中的AOP
- vs整合MySQL和QT