【发布时间】:2017-06-28 08:49:42
【问题描述】:
我按照 iText 7 的 this example 将多页 Tiff 转换为多页 PDF,但是当我打开 PDF 时它已损坏。 Adobe Reader 显示错误,Chrome 显示如下:
(每个页面看起来都是这样,但它们并不完全相同)。
这是我使用的代码:
File newPdfFile = new File("<path...>/converted_file.pdf");
URL tiffUrl = UrlUtil.toURL("<path...>/original_file.tif");
IRandomAccessSource ras = new RandomAccessSourceFactory().createSource(tiffUrl);
RandomAccessFileOrArray rafoa = new RandomAccessFileOrArray(ras);
int numberOfPages = TiffImageData.getNumberOfPages(rafoa);
PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(newPdfFile)));
Document document = new Document(pdf);
for(int i = 1; i <= numberOfPages; ++i) {
Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, true));
document.add(image);
}
document.close();
pdf.close();
这是我在 iText 5.5.11 中使用的代码,可以工作,但使用了已弃用的 RandomAccessFileOrArray 构造函数:
File newPdfFile = new File("<path...>/converted_file.pdf");
RandomAccessFileOrArray rafoa = new RandomAccessFileOrArray("<path...>/original_file.tif");
int numberOfPages = TiffImage.getNumberOfPages(rafoa);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(newPdfFile));
document.open();
for (int i = 1; i <= numberOfPages; ++i) {
Image image = TiffImage.getTiffImage(rafoa, i);
Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight());
document.setPageSize(pageSize);
document.newPage();
document.add(image);
}
document.close();
很遗憾,我无法提供示例文件,因为它们是机密/机密文件...
可能是什么问题?
P.S.:我试过 the same tiff used in the example code I followed 并且它有效。我的 tiff 怎么了?在文件属性中,除了尺寸和分辨率之外还有:
- 位深:1
- 压缩:CCITT T.4
- 分辨率单位:2
【问题讨论】:
-
问题出在 tiff 文件中。但是,如果您不能提供,那么我们将无法帮助您。我的建议是尝试通过将 tiff 加载到 JAI(Java 高级成像)中来预处理 tiff,然后将 Java 图像传递给 iText。
-
我如何将它传递给 iText,究竟是什么?我使用的所有构造函数和方法都需要
URL、RandomAccessFileOrArray或byte[]。我需要用 JAI 做什么,所以它给我的 byte[] 与原来的不同?还是有其他方法可以将图像传递给 iText? -
JAI 输出一个 BufferedImage,对吧?将 BufferedImage 输出到 byte[] 就可以了。
-
我是这么认为的,但我不确定……好吧,现在我得让 Maven 找到那个该死的 jai_imageio 库……谢谢。