Java itext tiff转换pdf
作者:互联网
需要jar: itext-2.1.7.jar
import java.io.FileOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;
public class TiffToPDF {
private static Logger log = LoggerFactory.getLogger(TiffToPDF.class);
/**
* @param tiffImage:tiff路径 C:\data\08\9e_1.tif
* @param previewFile:pdf路径 C:\data\08\9e_1_pre.pdf
*/
public static void TiffToPDF(String tiffImage, String previewFile) throws Exception {
try {
RandomAccessFileOrArray myTiffFile = new RandomAccessFileOrArray(tiffImage);
// 读取tiff文件页数
int numberOfPages = TiffImage.getNumberOfPages(myTiffFile);
log.info("TiffToPDF tiffImage: {}", tiffImage);
log.info("TiffToPDF numberOfPages: {}", numberOfPages);
Document TifftoPDF = new Document();
PdfWriter.getInstance(TifftoPDF, new FileOutputStream(previewFile));
TifftoPDF.open();
// 读取tiff文件的图像信息,添加到pdf文件
for (int i = 1; i <= numberOfPages; i++) {
Image tempImage = TiffImage.getTiffImage(myTiffFile, i);
Rectangle pageSize = new Rectangle(tempImage.getWidth(), tempImage.getHeight());
TifftoPDF.setPageSize(pageSize);
TifftoPDF.newPage();
TifftoPDF.add(tempImage);
}
TifftoPDF.close();
log.info("TiffToPDF completed: {}", tiffImage);
} catch (Exception e) {
log.error("TiffToPDF error: {}", e);
}
}
}
标签:Java,text,TiffToPDF,lowagie,itext,pdf,import,com 来源: https://blog.csdn.net/dalianlhy/article/details/115655531