资源简介
java 多个多页的tif文件转pdf方法,并附上了目前最新版的itextpdf的jar包(itextpdf-5.5.13.jar)亲测可用

代码片段和文件信息
package com.lj.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
/**
*
* title: TifToPdfUtil
* Description: tif工具类
* @author secret
* @date 2018-3-1 上午11:42:20
*/
public class TifToPdfUtil {
/**
*
* 功能描述: 将多个多页的tif文件转成pdf
* @param tifFileArr
* @param pdfFilePath
* @author secret
* @throws Exception
* @date 2018-3-1 上午09:58:15
*/
public static void tifsToPdf(File[] tifFileArr String pdfFilePath) throws Exception {
// 创建pdf文件的所属文件夹
String pdfFolderPath = pdfFilePath.substring(0 pdfFilePath.lastIndexOf(“\\“));
File pdfFolder = new File(pdfFolderPath);
if (!pdfFolder.exists()) {// 如果pdf目录不存在新建目录
pdfFolder.mkdirs();
}
Document docment = null;// 文档对象
try {
// 定义输出位置并把文档对象装入输出对象中
docment = new Document(PageSize.LETTER 0 0 0 0);// 创建一个文档对象
PdfWriter pdfWriter = PdfWriter.getInstance(docment new FileOutputStream(pdfFilePath));
pdfWriter.setStrictImageSequence(true);
int tifFilePageCount = 0;// tif文件页数
for (int fileNumber = 0; fileNumber < tifFileArr.length; fileNumber++) {
RandomAccessFileOrArray randomAccessFileOrArr = null;
try {
File tifFile = tifFileArr[fileNumber];
InputStream tifFileInputStream = null;
try {
tifFileInputStream = new FileInputStream(tifFile);
randomAccessFileOrArr = new RandomAccessFileOrArray(tifFileInputStream);
tifFilePageCount = TiffImage.getNumberOfPages(randomAccessFileOrArr);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(“读取tiff文件[“
+ (null == tifFile) != null?“文件为空“:tifFile.getName() + “]页数失败“);
} finally {
if(null != tifFileInputStream){
tifFileInputStream.close();
}
}
for (int pageNumber = 1; pageNumber <= tifFilePageCount; ++pageNumber) {
try {
// 将tif文件读取成图片
Image tifImg = TiffImage.getTiffImage(randomAccessFileOrArr pageNumber);
if(null == tifImg){
throw new Exception(“读取tif文件失败“);
}
// 设置首页tif图片的大小
Rectangle firstpageSize = new Rectangle(tifImg.getWidth() tifImg.getHeight());
docment.setPageSize(firstpageSize);
docment.open();// 打开文档对象
// 设置图片大小
Rectangle pageSize = new Rectangle(tifImg.getWidth() tifImg.getHeight());
docment.setPageSize(pageSize);
tifImg.setAbsolutePosition(0 0);// 设置图像位置
tifImg.setAlignment(Image.ALIGN_CENTER);// 设置图像
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2320581 2018-02-28 18:03 tif转pdf\itextpdf-5.5.13.jar
文件 4071 2018-03-01 11:43 tif转pdf\TifToPdfUtil.java
目录 0 2018-03-01 11:49 tif转pdf
----------- --------- ---------- ----- ----
2324652 3
- 上一篇:模仿地铁购票系统+数据库
- 下一篇:在线投稿系统
相关资源
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
- [免费]java实现有障碍物的贪吃蛇游戏
- java Servlet投票实例
- 操作系统作业 (pv,作业管理,等5个
- 基于C/S架构考试系统(Java)
评论
共有 条评论