-
Notifications
You must be signed in to change notification settings - Fork 673
Migrating from iText 2 and 4
Alberto Fernández edited this page May 5, 2019
·
8 revisions
One goal of LibrePDF is to mantain compatibility with iText, to be a drop-in replacement of iText.
But compatibility is not always possible, so we collect all changes you need to do in your code:
You must depends on twelvemokeys
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.4.1</version>
</dependency>You can use the standard approach
Image tif = Image.getInstance("iText.tif");but if you need to read a multipage TIFF
old way RandomAccessFileOrArray + TiffImage
RandomAccessFileOrArray randomAccess =
new RandomAccessFileOrArray(
file.getAbsolutePath(),
false,
Document.plainRandomAccess);
int pages = TiffImage.getNumberOfPages(randomAccess);
for (int i = 1; i <= pages; i++) {
Image image = TiffImage.getTiffImage(randomAccess, i);
document.add(image);
}new way
try (ImageInputStream iis = ImageIO.createImageInputStream(file)) {
ImageReader reader = getTiffImageReader();
reader.setInput(iis);
int pages = reader.getNumImages(true);
for (int imageIndex = 0; imageIndex < pages; imageIndex++) {
BufferedImage bufferedImage = reader.read(imageIndex);
Image image = Image.getInstance(bufferedImage, null, false);
document.add(image);
}
}
private static ImageReader getTiffImageReader() {
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByFormatName("TIFF");
if (!imageReaders.hasNext()) {
throw new UnsupportedOperationException("No TIFF Reader found!");
}
return imageReaders.next();
}