import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter; public class PdfUtilImg {
public static File Pdf(ArrayList<String> imageUrllist,String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 20, 20, 20, 20); //new一个pdf文档
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf写入
doc.open();//打开文档
for (int i = 0; i < imageUrllist.size(); i++) { //循环图片List,将图片加入到pdf中
doc.newPage(); //在pdf创建一页
Image png1 = Image.getInstance(imageUrllist.get(i)); //通过文件路径获取image
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; //反回文件输出流
} 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;
}
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;
}
public void imgOfPdf(String filepath,HttpServletRequest request) {
boolean result = false;
try {
ArrayList<String> imageUrllist = new ArrayList<String>(); //图片list集合
imageUrllist.add(request.getSession()
.getServletContext().getRealPath("\\" + filepath)); //添加图片文件路径
String fles = filepath.substring(0, filepath.lastIndexOf("."));
String pdfUrl = request.getSession().getServletContext()
.getRealPath("\\" +fles+".pdf"); //输出pdf文件路径
result = true;
if (result == true) {
File file = PdfUtilImg.Pdf(imageUrllist, pdfUrl);//生成pdf
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用方法:

PdfUtilImg img = new PdfUtilImg();
img.imgOfPdf(filePaths, request);//filePaths为存储位置

java img图片转pdf 工具类的更多相关文章

  1. Java 借助poi操作PDF工具类

    ​ 一直以来说写一个关于Java操作PDF的工具类,也没有时间去写,今天抽空写一个简单的工具类,拥有PDF中 换行,字体大小,字体设置,字体颜色,首行缩进,居中,居左,居右,增加新一页等功能,如果需要 ...

  2. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  3. Java汉字转成汉语拼音工具类

    Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包. import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...

  4. java中excel导入\导出工具类

    1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...

  5. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  6. java代码行数统计工具类

    package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...

  7. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  8. Java 操作Redis封装RedisTemplate工具类

    package com.example.redisdistlock.util; import org.springframework.beans.factory.annotation.Autowire ...

  9. java后台表单验证工具类

    /** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...

随机推荐

  1. es与hive整合

    在hive classpath中添加elasticsearch-hadoop.jar,以下方法任一种均可: 1.启动hiveserver2 前,在hive-site.xml文件中更改hive.aux. ...

  2. 高版本sonar安装遇到的坑-sonar 6.7.5

    最近安装了6.7.5版本的sonar,发现里面的坑还是很多,下面列举下遇到的坑 sonar插件地址:https://docs.sonarqube.org/display/PLUG/Plugin+Lib ...

  3. Java_方法的调用①及案例

    方法调用的语法格式: 类名.方法名称([参数列表]); 调用过程: 案例: class Method01{ /*练习1:使用方法完成,输出5遍HelloWorld 方法语法格式: [修饰符] 返回值类 ...

  4. (转)MySQL open_files_limit相关设置

    http://www.cnblogs.com/zhoujinyi/archive/2013/01/31/2883433.html---------------------------MySQL ope ...

  5. java.lang.RuntimeException: HRegionServer Aborted的问题

    进程情况 [hadoop@hadoop1 hbase]$ jps QuorumPeerMain ResourceManager HMaster NameNode JournalNode HRegion ...

  6. CentOS6.4 安装sftp

    1.打开命令终端窗口,按以下步骤操作.使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. [root@ecs-3c46 ~]# ssh -v OpenS ...

  7. ORA-XXXX错误集合

    第一.ORA-12514:listener does not currently know of service requested in connect descriptor 监听器巴拉巴拉一堆,然 ...

  8. step6: item与pipeline

    目的:提取内容进行格式化输出,类似于字典 编写item文件 class JobBoleArticleItem(scrapy.Item): title = scrapy.Field() #支持传进任何数 ...

  9. C#基础笔记 转自wojiushigelg

    笔记如下: 概念:.net与c# .net/dontnet:一般指.net framework框架,一种平台,一种技术 c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一 ...

  10. SQL执行一次INSERT INTO查询,插入多行记录

    那如果我们想插入多行记录呢?可能你会想到多次运行INSERT语句即可,就像下面的php代码示例: INSERT INTO tbl_name (col1,col2) VALUES (,); 那如果我们想 ...