package util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
/*
* 图片转换成pdf
* 注意:这里需要导入 com.lowagie.text-2.1.7jar包
* imgPath:图片路径,如 E:\\victoria\\001.jpg"
* pdfPath:转换之后的pdf路径,如 E:\\victoria\\001.pdf
*/
public class UtilFiles {
public boolean jpgToPdf(String imgPath,String pdfPath) throws IOException {
//判断文件是否存在
if (new File(imgPath).exists()) {
Document doc = new Document(PageSize.A4);
try {
//生成pdf
PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));
doc.open();
float newHeight = 0;
//判断文件的宽度
float pageWidth = doc.getPageSize().getWidth();
float pageHeight = doc.getPageSize().getHeight();
//获取图片的大小
Image image = Image.getInstance(imgPath);
float imageWidth = image.getPlainHeight();
float imageHeight = image.getPlainWidth();
//使图片自动适应大小
if(imageWidth > pageWidth - 72) {
image.scaleAbsoluteWidth(pageWidth-72);
newHeight += imageHeight*(pageWidth - 72)/imageWidth +36;
image.scaleAbsoluteHeight(imageHeight*(pageWidth -72)/imageWidth);
}else{
newHeight += imageHeight + 36;
}
//判断是否需要分页
if (pageHeight < newHeight) {
doc.newPage();
newHeight = imageHeight*(pageWidth -72)/imageWidth +36;
}
image.setAbsolutePosition(36, pageHeight - newHeight);
doc.add(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (DocumentException e) {
e.printStackTrace();
return false;
}finally{
doc.close();
File file = new File(imgPath);
if (new File(imgPath).exists()) {
//file.delete();
}
}
}else{
return false;
}
return true;
}
public static void main(String[] args) throws IOException {
String imgth = "E:\\victoria\\001.jpg";
String pdfth = "E:\\victoria\\001.pdf";
UtilFiles u = new UtilFiles();
System.out.println("开始进入方法================》");
u.jpgToPdf(imgth, pdfth);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ssm框架-spring+springmvc+mybatis+eclipse+oracle+tomcat小项目
共68个文件
jar:30个
xml:8个
class:6个
5星 · 超过95%的资源 需积分: 11 104 下载量 77 浏览量
2017-09-26
16:42:45
上传
评论 5
收藏 13.03MB RAR 举报
温馨提示
ssm框架-spring+springmvc+mybatis+eclipse+oracle+tomcat小项目,项目比较简单,如果有想法,可以自己往里面添加新的功能。
资源推荐
资源详情
资源评论
收起资源包目录
victoria_02.rar (68个子文件)
.project 1KB
WebContent
WEB-INF
views
show.jsp 727B
success.jsp 413B
thirdRun.jsp 683B
list.jsp 822B
lib
spring-context-4.0.0.RELEASE.jar 928KB
slf4j-log4j12-1.7.7.jar 9KB
spring-expression-4.0.0.RELEASE.jar 202KB
spring-tx-4.0.0.RELEASE.jar 242KB
jstl-1.2.jar 405KB
spring-webmvc-4.0.0.RELEASE.jar 645KB
ojdbc6.jar 1.9MB
spring-web-4.0.0.RELEASE.jar 646KB
standard-1.1.2.jar 384KB
commons-pool2-2.4.2.jar 109KB
spring-aop-4.0.0.RELEASE.jar 343KB
spring-beans-4.0.0.RELEASE.jar 654KB
commons-logging-1.1.1.jar 59KB
junit-4.0.jar 103KB
log4j-1.2.17.jar 478KB
spring-core-4.0.0.RELEASE.jar 933KB
com.lowagie.text-2.1.7.jar 1.7MB
mybatis-spring-1.2.0.jar 48KB
spring-jdbc-4.0.0.RELEASE.jar 410KB
spring-aspects-4.0.0.RELEASE.jar 50KB
c3p0-0.9.1.2.jar 596KB
slf4j-api-1.6.1.jar 25KB
commons-logging-1.1.3.jar 61KB
spring-orm-4.0.0.RELEASE.jar 309KB
com.springsource.org.aopalliance-1.0.0.jar 5KB
com.springsource.net.sf.cglib-2.2.0.jar 319KB
commons-dbcp2-2.1.1.jar 170KB
mysql-connector-java-5.1.7-bin.jar 693KB
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 1.57MB
mybatis-3.1.1.jar 636KB
web.xml 2KB
index.jsp 1KB
META-INF
MANIFEST.MF 39B
src
applicationContext.xml 2KB
com
controller
UserController.java 2KB
mapper
User.java 709B
UserMapper.xml 948B
dao
UserDao.java 312B
MyBatisRepository.java 129B
spring-mvc.xml 811B
jdbc.properties 145B
util
UtilFiles.java 2KB
LogAppender.java 983B
log4j.properties 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 134B
org.eclipse.wst.common.component 485B
.jsdtscope 567B
build
classes
applicationContext.xml 2KB
com
controller
UserController.class 3KB
mapper
User.class 1KB
UserMapper.xml 948B
dao
MyBatisRepository.class 162B
UserDao.class 440B
spring-mvc.xml 811B
jdbc.properties 145B
util
LogAppender.class 597B
UtilFiles.class 3KB
log4j.properties 2KB
.classpath 834B
共 68 条
- 1
资源评论
- Miraitowa_Ming2019-04-12可以可以。
- changyuming2018-08-25东西好用,非常感谢分享。
viktoria
- 粉丝: 16
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功