Skip to content

Migrating from iText 2 and 4

Andreas R edited this page Feb 29, 2024 · 7 revisions

Migrating from iText 2.1.7 and iText 4.2.0

Compatibility

One goal of LibrePDF is to maintain compatibility with iText, to be a drop-in replacement of iText.

OpenPDF 2.0.0 removed some deprecated methods, please review the JavaDoc for methods available in various versions: OpenPDF JavaDoc

But compatibility is not always possible, so we collect all changes you need to do in your code:

Changes needed in your code

TiffImage

You must depends on twelvemonkeys

        <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

iText 2 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);
        }

OpenPDF

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

Metadata manipulation

iText 2

    PdfReader reader = new PdfReader(origin);			
    PdfStamper stamper = new PdfStamper(reader, out);
    reader.getCatalog().remove(PdfName.METADATA);
    HashMap<String, String> info = reader.getInfo();
    info.put("Title", "new title");
    // ....
    stamper.setMoreInfo(info);

OpenPDF & iText compatible

    PdfReader reader = new PdfReader(origin);			
    PdfStamper stamper = new PdfStamper(reader, out);
    reader.getCatalog().remove(PdfName.METADATA);

    // This function now return Map
    Map<String, String> info = reader.getInfo();
    info.put("Title", "new title");
    // ....

    // And now we need a cast 
    // (setMoreInfo is deprecated in OpenPdf but the alternative doesn't exists in itext)
    
    stamper.setMoreInfo((HashMap)info);

Base64 removed

OpenPDF has no self implementation of Base64 anymore, and com.lowagie.text.pdf.codec.Base64 was removed. Please use java.util.Base64 instead.