License
Activate a NexPDF license key in Java, check which modules it unlocks, and handle license errors.
Call NexPdf.activateLicense once per process, before other NexPDF calls. It accepts the license
XML file's text or the bare NEX2. key. See Licensing for how keys are
issued and bound.
Activate
import com.nexdevit.nexpdf.NexPdf;
import java.nio.file.Files;
import java.nio.file.Path;
int result = NexPdf.activateLicense(Files.readString(Path.of("nexpdf_license.xml")), "server", "com.acme.invoicing");
System.out.println(result == 1 ? "trial" : "licensed");| Parameter | Value |
|---|---|
keyOrXml | The license XML or the bare key |
platform | server, linux, macos or windows: one of the platforms on the key |
appId | The application id the key was issued for |
It returns 2 for a commercial key and 1 for a trial key.
Reading the key from the environment
NexPdf.activateLicense(System.getenv("NEXPDF_LICENSE_KEY"), "server",
System.getenv().getOrDefault("NEXPDF_APP_ID", "com.acme.invoicing"));Check the status
String status = NexPdf.licenseStatusJson(); // parse with any JSON library
System.out.println(status);
if (NexPdf.isLicensed("signatures")) {
System.out.println("signing is available");
}licenseStatusJson() returns state (unlicensed, trial, licensed), licensee, id,
tier, modules, expires_at, days_left and watermark.
Handle errors
A rejected key throws NexPdfException (unchecked) with the negative code in code():
try {
NexPdf.activateLicense(key, "server", "com.acme.invoicing");
} catch (NexPdfException e) {
switch (e.code()) {
case -4 -> log.error("NexPDF license expired: renew it in the dashboard");
case -6, -7 -> log.error("NexPDF license is for another platform or app id: {}", e.getMessage());
default -> log.error("NexPDF license rejected ({}): {}", e.code(), e.getMessage());
}
}A call that needs a module your key does not include throws NexPdfException too:
try {
doc.encryptAes256("user", "owner");
} catch (NexPdfException e) {
System.out.println(e.getMessage()); // the active license does not include the 'security' module
}| Code | Meaning |
|---|---|
| -1 | Malformed key |
| -2 | Bad signature |
| -3 | Unsupported key version |
| -4 | Expired |
| -5 | Not yet valid (check the clock) |
| -6 | Wrong platform |
| -7 | Wrong application id |
Without a license, or after a rejected key, the SDK stays in viewer mode: it opens, renders, extracts text and searches, and rendered pages carry the "NexPDF • Unlicensed" watermark.