欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

功能实现:jpg->TIF->PDF格式转换

程序员文章站 2022-07-10 20:27:26
...

一、JPG转换成TIF格式
两个jar包
com.sun.media jai_imageio 1.1
com.sun.media jai_codec 1.1.3
public static void ScanTifToPdf (String filepath){
String tImages = “”;
String file = “”;
String tdate ="";//获取当前系统时间
int pathlength = filePath.get(i).length;
String picPath = filePath.get(i).subString(0,Pathlength-4);
String extName = filePath.get(i).subString(Pathlength-4,Pathlength);
if(new File(picPath+".tif").exists()){
tImages.add(picPath+".tif");
}else{
File jpgFile= new File(picPath+extName)
if(jpgFile.exists()){
try{
OutputStream os = new FileOutputStream(picPath+".tif");
RenderedOp src = JAI.create(“fileload”,picPath+extName);
TIFFEncodeParam param = new TIFFEncodeParam();
ImageEncoder encoder = ImageCodec.createImageEncoder(“TIFF”,os,param);
encoder.encode(src);
tImages = picPath+".tif";
}catch(Exception e ){

			    }finally{
					os.close();	
				}
	}
}

}

2,TIF转换成pdf 格式
public static void Topdf(String filename ,String tImages){
File file = new File (tImages);
if(file.exists()){
Document document = new Document();
FileOutputStream fos = null;
try{
fos = new FileOutputStream(filename);
pdfWriter.getInstance(document,fos);
document.setPageSize(PageSize.A4);
document.open();
Image image = Image.getInstance(tImages);
image.scaleToFit(550,800);
image.setAlignment(Image.ALIGN_CENTER);
document.add(image);
}catch{
} finally{
document.close();
fos.flush();
}
}
}

如果document.add报错,请查看引入的包是否是在itextpdf.pdf.Document,其他报错同理

相关标签: 功能实现 java