将 Tiff 转换为 PDF:PDF 已损坏

Converting Tiff to PDF: PDF is corrupted

我按照 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 中使用的代码, 有效 但使用了 [=12= 的弃用构造函数]:

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();

很遗憾,我无法提供示例文件,因为它们是 confidential/classified...

可能是什么问题?

P.S.: 我试过 the same tiff used in the example code I followed 并且有效。我的口角怎么了?在文件属性中,除了尺寸和分辨率之外还有:

好的,感谢 Michaël Demey 的建议,我设法使用 iText 7 获得了正确的 pdf。

这是 Maven 导入:

<dependency>
    <groupId>com.sun.media</groupId>
    <artifactId>jai_imageio</artifactId>
    <version>1.1</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>layout</artifactId>
    <version>7.0.3</version>
</dependency>

代码如下:

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

[...]

File newPdfFile = new File("<path...>/converted_file.pdf");

ImageReader reader = ImageIO.getImageReadersByFormatName("TIFF").next();
reader.setInput(ImageIO.createImageInputStream(new File("<path...>/original_file.tif")));
int numberOfPages = reader.getNumImages(true);

PdfDocument pdf = new PdfDocument(new PdfWriter(new FileOutputStream(newPdfFile)));
Document document = new Document(pdf);

for(int i = 0; i < numberOfPages; ++i) {// in javax.imageio.ImageReader they start from 0!
    java.awt.Image img = reader.read(i);
    Image tempImage = new Image(ImageDataFactory.create(img, null));
    pdf.addNewPage(new PageSize(tempImage.getImageWidth(), tempImage.getImageHeight()));
    tempImage.setFixedPosition(i + 1, 0, 0);
    document.add(tempImage);
}
document.close();
pdf.close();

我也遇到了同样的问题。我能够转换扩展名为“TIFF”的文件,但扩展名为“TIF”的文件我无法正确转换。然后我尝试了一些能够进行 it.Try 如下更改并检查

Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, true));

Image image = new Image(ImageDataFactory.createTiff(tiffUrl, true, i, false));

这使我的转换work.Hope对你也有效