Import itext-7 in android gradle
Asked Answered
H

5

13

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.

Harrington answered 25/4, 2017 at 10:1 Comment(0)
R
14

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.

Ricker answered 25/4, 2017 at 10:34 Comment(1)
@hannes why did you change "mere" into "more"? That doesn't make any sense, does it?Ricker
A
9

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.

Avraham answered 25/4, 2017 at 10:33 Comment(8)
Oops, you were faster... ;)Ricker
And you were more verbose. :)Avraham
@Ricker If you add the part about not being compatible with Android then I will delete my answer. Your answer is better.Avraham
Thank you very much. Yes the part "not compatible with android" is vital here.Harrington
@AmedeeVanGasse You were faster once again, I was just starting the edit when your edit flew in...Ricker
All during my lunch break, I'm not even at my desk but doing this on my phone.Avraham
Don't forget to eat!Ricker
Please have a look at my other question regarding itext, before the lunch is over :) #43609006Harrington
N
7

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

implementation 'com.itextpdf:itext7-core:7.1.8'
Nissy answered 7/10, 2019 at 12:6 Comment(4)
Is it required license?Farrison
for some features it do require licenseNissy
Do I need license for watermark? When I try to add watermark it is not working and in log message it is showing message like license file not found.Farrison
@RealTechHacks did you know how to add a license in iText7?Coolish
S
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

Syllable answered 14/2, 2019 at 1:26 Comment(0)
I
1

From version 7.2.3 there is official support for Android. You can include iText core dependency like this example below. Make sure you include the android maven repository, otherwise the artefact won't be found.

maven {
  url "https://repo.itextsupport.com/android"
}

dependencies {
  implementation 'com.itextpdf.android:itext7-core-android:7.2.3'
}

Check the official documentation for more info

Idalla answered 15/8, 2022 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.