java (图片转PDF)
1.导入jar包
itextpdf-5.5.12.jar
2.写代码
package com.util; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* @author Administrator
* (图片转PDF)
*/
public class PDFManage { /**
*
* @param imageUrllist 图片路径集合
* @param mOutputPdfFileName 输出的pdf文件名
* @return
*/
public static File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {
// String TAG = "PdfManager";
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
for (int i = 0; i < imageUrllist.size(); i++) {
doc.newPage();
// doc.add(new Paragraph("简单使用iText"));
Image png1 = Image.getInstance(imageUrllist.get(i));
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent+3);// 表示是原来图像的比例;
doc.add(png1);
}
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
} /**
* 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
*
* @param h
* @param w
* @return
*/ public static int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
} /**
* 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的,自我认为给客户的效果是最好的
*
* @param args
*/
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
} }
测试代码:
package org.test; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import com.util.PDFManage; public class pdfTest { public static void main(String[] args) {
ArrayList<String> imageUrllist = new ArrayList<String>();
imageUrllist.add("D:\\" + "1" + ".jpg");
String pdfUrl = "D:\\2.pdf";
File file = PDFManage.Pdf(imageUrllist, pdfUrl);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
亲测可用
java (图片转PDF)的更多相关文章
- Java 图片生成PDF
public static void main(String[] args) { String imageFolderPath = "E:\\Tencet\\图片\\test\\" ...
- Java给图片和PDF文件添加水印(图片水印和文字水印)
有时候我们看到的图片或者PDF文件会自动加上水印.分为文字水印和图片水印. ----------------------------图片水印---------------------------- 1 ...
- Java:PPT(X)转图片、PDF和SVG
(一) 简介: 工作中,PowerPoint文档有时需要被转换为PDF/图像文件来存档.因为PDF或图片的页面布局是固定的,很难被修改且能被大多数设备打开,所以PDF或者图片比起PowerPoint格 ...
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- wkhtmltox实现网页转换成图片或pdf
1.先下载http://download.gna.org/wkhtmltopdf/obsolete/windows/wkhtmltox-0.11.0_rc1-installer.exe,安装 2.在命 ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- java 多页pdf转化为多张图片
相关jar包: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian ...
- Java实现Word/Pdf/TXT转html
引言: 最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及在线学习和统计的功能,因为课件涉及到多种类型,像视频,音频,图文,外部链接及文档类型. ...
- Atitit.java图片图像处理attilax总结
Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...
随机推荐
- ubantu 安装git
1.安装git并配置 sudo add-apt-repository ppa:git-core/ppa sudo apt-get update sudo apt-get install git 可以使 ...
- 2019-04-01-day023-对象实例的反射实例化
学习方法 学练改管测 听别人说 读 input 自己说 自己写 output 解决语法错误 解决逻辑错误 ##内容回顾 ##继承 多态 封装 property classmethod staticme ...
- 排序jq
var arr = [1,2,3,4,5,6,7]; arr.sort(function (a, b) { 从大到小 if (a > b) { return 1; } else if (a &l ...
- phpcms 操作数据库 增删改查
数据库的其他类继承的都是libs/class/model.class.php 这里面有写好的操作数据库的常用方法 1.增 insert($data, $return_insert_id = false ...
- 如何在Ubuntu/Linux内使用 zip 压缩隐藏文件?
假设你的当前目录结构为: A文件--B文件夹--C文件等 | | D文件夹-E文件 | | 等等文件(含隐藏) 那么在使用zip命令时加上 -r 即可包含整个目录结构和隐藏文件: zip -r B.z ...
- if-else练习
练习1 import java.util.Scanner; public class V{ public static void main(String[] args){ Scanner s=new ...
- 《DSP using MATLAB》Problem 6.16
从别的地方找来的: 截图有些乱. 结构流程图如下
- selected标签判断默认选中
<select name="suggestedType" style="width:280px" > <option value=" ...
- Mybatis(二,三)
参考孤傲苍狼的博客,地址如下: http://www.cnblogs.com/xdp-gacl/p/4264301.html 在此声明,自己写博客,是为了学习总结过程中的记录.没有侵权和偷懒的意思. ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...