在 android gradle 中导入 itext-7

Import itext-7 in android gradle

在 gradle

中添加以下内容后,我正在尝试将 itext-7 添加到 android

compile 'com.itextpdf:root:7.0.0'

我仍然无法找到 itext 的 类,例如 PDFWriter 等

请让我知道 Android 的 itext-7 是否有单独的版本以及如何添加它。

P.S:我已成功添加 itext-5,但我想使用 itext-7 现在。

您需要添加

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

也许更多,具体取决于您可能需要哪些组件。有关完整列表,请参阅 http://developers.itextpdf.com/itext-7 - 它采用 Maven XML 格式,但您应该能够适应 Gradle.

至于 Android:目前 iText 7 与 Android 兼容,您 出现编译错误 iText 7 在具有 Android API 24 级或更高级别(Android Nougat)的设备上使用时开箱即用。如果您想在 Android 的较低版本上支持 运行 的设备,您可以编写一个 Xamarin 应用程序,它将 运行 在 Android 的任何版本上,但 Xamarin 意味着用 .NET 编写。

根工件只是一个父 pom,根本不包含 iText 7 类。

如果你想包含所有 iText 7 核心功能,你应该尝试

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

如果开箱即用(例如,由于 Android 中缺少 Java 类),或者如果您只是想要更精简的安装,请注意相比之下对于 iText 5,较新的 iText 7 不是作为一个大罐子分发的,而是作为一组模块分发的。

对于 Maven,您将使用以下依赖项(或者更可能是它们的子集);您可以轻松地从它们构建 gradle compile 语句:

<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>

(developers.itextpdf.com 上的Getting started with iText 7

至于 Android:目前 iText 7 与 Android 兼容,您 出现编译错误 iText 7 在具有 Android API 24 级或更高级别(Android Nougat)的设备上使用时开箱即用。如果您想在 Android 的较低版本上支持 运行 的设备,您可以编写一个 Xamarin 应用程序,它将 运行 在 Android 的任何版本上,但 Xamarin 意味着用 .NET 编写。

因为这是最高 Google 结果,我想添加一个技巧让它在 Android

上运行

我目前 运行 iText 7.1.5 Android

minSdkVersion 22
targetSdkVersion 28 

编辑 XFA PDF 没有问题

诀窍是设置您的 gradle 发布和调试

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

这将删除未使用的 类,在 大多数 情况下,它还会删除依赖于 API 24 或 API 的代码26、允许您的应用程序编译

下一个技巧是将其添加到您的 app.gradle

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

删除生成的警告。

显然在生产中使用之前要格外小心并进行大量测试

如果您想包含所有 iText 7 核心功能,您应该尝试

implementation 'com.itextpdf:itext7-core:7.1.8'