在android gradle中导入itext-7

问题描述 投票:4回答:3

我想在gradle中添加以下内容后将itext-7添加到android

编译'com.itextpdf:root:7.0.0'

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

如果有针对Android的itext-7的单独版本,请告诉我如何添加它。

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

android pdf gradle itext itext7
3个回答
7
投票

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

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

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>

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

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


5
投票

你需要添加

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中编写。


1
投票

由于这是谷歌的一个重要结果,我想添加一个技巧,让它在Android上运行

我目前在Android上运行iText 7.1.5

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/*'
    }

删除生成的警告。

在生产中使用之前,显然要谨慎使用并进行大量测试

© www.soinside.com 2019 - 2024. All rights reserved.