将Tiff转换为PDF:PDF已损坏

时间:2017-06-28 08:49:42

标签: java pdf tiff itext7

我跟随iText 7的this example将多页Tiff转换为多页PDF,但是当我打开PDF时它已损坏。 Adobe Reader显示错误,Chrome显示:

Screenshot

(每个页面都是这样的,但它们并不相同)。

这是我使用的代码:

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

2 个答案:

答案 0 :(得分:1)

好的,感谢MichaëlDemey的建议,我设法使用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();

答案 1 :(得分:0)

我也遇到了同样的问题。我能够转换扩展名为“ TIFF”的文件,而不能转换为“ TIF”扩展名的文件。然后我尝试了一些能够做到。尝试如下更改并从

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

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

这使我的转换工作正常了。希望它也对您有用