Installing iText Licenses with Gradle
This guide explains how to configure Gradle to load your iText license files correctly, ensuring your PDF solutions run smoothly in compliance with the iText license requirements.
At the end of this guide you can find a sample solution folder to load your licenses in a standalone project.
Loading an iText License with Gradle
➡️ Preparing your Project
To get started, place your iText license file (itext-license.json
) inside your project's src/main/resources
folder. This ensures the license file is packaged correctly and can be easily accessed by your application during runtime.
If you have multiple files, e.g., if you have licenses for iText Core and pdfHTML or other add-ons, place them in the same location.
➡️ Configuring Gradle Dependencies
Edit your build.gradle
file and include the necessary iText dependencies:
dependencies {
implementation 'commons-io:commons-io:2.11.0'
implementation 'com.itextpdf:itext-core:9.2.0'
implementation 'com.itextpdf.licensing:licensing-base:4.2.2'
implementation 'com.itextpdf.licensing:licensing-remote:4.2.2'
}
Replace the version numbers (9.2.0
and 4.2.0
) with the appropriate versions compatible with your license.
See the Compatibility Matrix for information on other versions of iText and the License key libraries compatible with those versions.
➡️ Loading the License in Your Java Code
In your main application class, you can load the licenses from your resources like so:
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.licensing.base.LicenseKey;
import java.io.InputStream;
public class SampleCase {
public static void loadLicenses() {
try {
// no full path as gradle resolves resources as resource name
String[] licenseFiles = {
"<Enter License File here>",
"<Enter License File here>"
};
for (String license : licenseFiles) {
InputStream stream = SampleCase.class.getClassLoader()
.getResourceAsStream(license);
if (stream == null) {
throw new RuntimeException("Missing license file: " + license);
}
LicenseKey.loadLicenseFile(stream);
System.out.println("Loaded license: " + license);
// PdfWriter writ = new PdfDocument(new PdfWriter("output.pdf"));
// new PdfDocument(writ).close();
}
System.out.println("All licenses loaded successfully!");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("License loading failed", e);
}
}
public static void main(String[] args) {
loadLicenses();
}
}
Ensure you load the license key before before any iText functionality is invoked.
➡️ Build and Run Your Project
Use Gradle commands to build and run your project:
./gradlew build
./gradlew run
Check the console to confirm the license files have been loaded, ensuring you are using iText correctly.
Following these steps will help you successfully integrate your iText license files using Gradle, enabling your Java applications to utilize iText libraries in compliance with the iText licensing requirements.