package com.icepdf;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import org.icepdf.core.util.GraphicsRenderingHints;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;
public class PdfToTiff {
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
public String changePdfToTiff(String srcPath,String outPath) throws Exception {
File fileIn = new File(srcPath);
if(!fileIn.exists()){
throw new RuntimeException("源文件不存在!");
}
File fileOut = new File(outPath);
OutputStream os = new FileOutputStream(fileOut);
if (!srcPath.endsWith(".pdf")) {
throw new RuntimeException("请检查参数pdfName文件名是否以.pdf结尾!");
}
org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
document.setFile(srcPath);
float scale = 2f;//比例
float rotation = 0f;//旋转
List<BufferedImage> bufferImgList = new ArrayList<BufferedImage>(document.getNumberOfPages() - 1);
for (int i = 0 + 1; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
bufferImgList.add(image);
}
TIFFEncodeParam param = new TIFFEncodeParam();// 创建tiff编码参数类
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);// 压缩参数
param.setExtraImages(bufferImgList.iterator());// 设置图片的迭代器
BufferedImage fimg = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN,
org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
PlanarImage fpi = JAI.create("mosaic", fimg); // 通过JAI的create()方法实例化jai的图片对象
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os, param);
enc.encode(fpi);// 指定第一个进行编码的jai图片对象,并将输出写入到与此
document.dispose();
return "";
}
public static void main(String[] args) {
try {
PdfToTiff pdfToTiff = new PdfToTiff();
System.out.println("start");
System.out.println(System.currentTimeMillis());
String srcPath="E:\\正式文件.pdf";
String outPath="E:\\aaa\\正式文件.tiff";
pdfToTiff.changePdfToTiff(srcPath,outPath);
System.out.println(System.currentTimeMillis());
System.out.println("end");
} catch (Exception e) {
e.printStackTrace();
}
}
}
xuanfeng6060
- 粉丝: 2
- 资源: 14
最新资源
- (源码)基于C语言的系统服务框架.zip
- (源码)基于Spring MVC和MyBatis的选课管理系统.zip
- (源码)基于ArcEngine的GIS数据处理系统.zip
- (源码)基于JavaFX和MySQL的医院挂号管理系统.zip
- (源码)基于IdentityServer4和Finbuckle.MultiTenant的多租户身份认证系统.zip
- (源码)基于Spring Boot和Vue3+ElementPlus的后台管理系统.zip
- (源码)基于C++和Qt框架的dearoot配置管理系统.zip
- (源码)基于 .NET 和 EasyHook 的虚拟文件系统.zip
- (源码)基于Python的金融文档智能分析系统.zip
- (源码)基于Java的医药管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈