功能实现:jpg->TIF->PDF格式转换
一、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,其他报错同理
上一篇: alphapose中出现的问题
下一篇: Java-杨辉三角