Import itext-7 in android gradle

2019-06-26 02:06发布

问题:

I am trying to add itext-7 to android, after adding the following in gradle

compile 'com.itextpdf:root:7.0.0'

I am still not able to find the classes of itext e.g PDFWriter etc.

Please let me know if there's separate version for itext-7 for Android also how to add it.

P.S: I have added itext-5 successfully, but i want to work with itext-7 now.

回答1:

The root artifact is a mere parent pom and does not contain iText 7 classes at all.

If you want to include all iText 7 Core functionality, you should try

compile 'com.itextpdf:itext7-core:7.0.2'

If this does not work out of the box (e.g. due to missing Java classes in Android), or if you simply want a leaner installation, note that in contrast to iText 5 the newer iText 7 is not distributed as one big jar but as a set of modules.

For Maven you would use the following dependencies (or more likely a subset from them); you can easily build gradle compile statements from them:

<dependencies>

    <!-- always needed -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- always needed -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- always needed -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for forms -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for PDF/A -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for digital signatures -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>sign</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for barcodes -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>barcodes</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for Asian fonts -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>font-asian</artifactId>
        <version>7.0.2</version>
    </dependency>

    <!-- only needed for hyphenation -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>hyph</artifactId>
        <version>7.0.2</version>
    </dependency>

</dependencies>

(Getting started with iText 7 on developers.itextpdf.com)

As for Android: currently iText 7 is not compatible with Android and you will get compilation errors iText 7 works out of the box when used on a device with an Android API level 24 or higher (Android Nougat). If you would like to support devices that run on a lower version of Android you could write a Xamarin app, which will run on any version of Android, but Xamarin means writing in .NET.



回答2:

You need to add

compile 'com.itextpdf:io:7.0.2'
compile 'com.itextpdf:kernel:7.0.2'
compile 'com.itextpdf:layout:7.0.2'

and maybe more, depending on which components you might need. See http://developers.itextpdf.com/itext-7 for the full list - it's in Maven XML format but you should be able to adapt for Gradle.

As for Android: currently iText 7 is not compatible with Android and you will get compilation errors iText 7 works out of the box when used on a device with an Android API level 24 or higher (Android Nougat). If you would like to support devices that run on a lower version of Android you could write a Xamarin app, which will run on any version of Android, but Xamarin means writing in .NET.



回答3:

As this is a top Google result I want to add a trick to get it working on Android

I am currently running iText 7.1.5 on Android with

minSdkVersion 22
targetSdkVersion 28 

No problems editing XFA PDFs

The trick is to set your gradle release AND debug

 buildTypes {
        debug {
            minifyEnabled true
        }
        release {
            minifyEnabled true
        }

This will remove the unused classes and in most cases it will also remove the code that depends on API 24 or API 26, allowing your app to compile

The next trick is to add this to your app.gradle

android {
    packagingOptions {
        pickFirst 'com/itextpdf/io/font/*'
        pickFirst 'com/itextpdf/io/font/cmap/*'
    }

to remove the warnings generated.

Obviously use this with extreme caution and copious amounts of testing before using in production