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)的更多相关文章

  1. Java 图片生成PDF

    public static void main(String[] args) { String imageFolderPath = "E:\\Tencet\\图片\\test\\" ...

  2. Java给图片和PDF文件添加水印(图片水印和文字水印)

    有时候我们看到的图片或者PDF文件会自动加上水印.分为文字水印和图片水印. ----------------------------图片水印---------------------------- 1 ...

  3. Java:PPT(X)转图片、PDF和SVG

    (一) 简介: 工作中,PowerPoint文档有时需要被转换为PDF/图像文件来存档.因为PDF或图片的页面布局是固定的,很难被修改且能被大多数设备打开,所以PDF或者图片比起PowerPoint格 ...

  4. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  5. wkhtmltox实现网页转换成图片或pdf

    1.先下载http://download.gna.org/wkhtmltopdf/obsolete/windows/wkhtmltox-0.11.0_rc1-installer.exe,安装 2.在命 ...

  6. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  7. java 多页pdf转化为多张图片

    相关jar包: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian ...

  8. Java实现Word/Pdf/TXT转html

    引言: 最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及在线学习和统计的功能,因为课件涉及到多种类型,像视频,音频,图文,外部链接及文档类型. ...

  9. Atitit.java图片图像处理attilax总结

    Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...

随机推荐

  1. python列表的11种方法

    python列表的11种方法2017年11月24日 03:26:43 Milton-Long 阅读数:254版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...

  2. js- caller、 callee

    caller 返回一个对函数的引用,该函数调用了当前函数.    functionName.caller     functionName对象 是所执行函数的名称.  说明        对于函数来说 ...

  3. datatables 多一列报错Cannot read property 'sWidth' of undefined(…)/少一列报错Cannot read property 'style' of undefined(…)

    datatables 多一列报错Cannot read property 'sWidth' of undefined(…)/少一列报错Cannot read property 'style' of u ...

  4. win7英文版

    下载个系统,不是一堆木马,就是一堆病毒:相对来说,我还是倾向于相信国外的镜像. https://pcriver.com/operating-systems/windows-7-ultimate-iso ...

  5. PRCT-1302 the OCR has an invalid ip address

    PRCT-1302 the OCR has an invalid ip address 1. 报错信息 an internal error occurred within cluster verifi ...

  6. python2.7.9安装mysql-python模块

    我使用的系统版本是: SLES12-sp2 使用python连接Mysql数据库,需要安装mysql-python模块: 1. 首先安装pip: 从python官方网站下载get-pipe.py,执行 ...

  7. next_permutation(start,end)

    一道水题,简单的next_permutation用法,相同的还有prev_permutation 包含在头文件<algorithm>中 字符串 acab 含有两个a ,一个b ,一个c , ...

  8. 20155219 2016-2017-2 《Java程序设计》第9周学习总结

    20155219 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与 ...

  9. JavaScript 如何断平台

    isMobile = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgen ...

  10. CodeForces 682A

    D - Alyona and Numbers Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...