Skip to Content
NexPDF

License

Activate a NexPDF license key in C++, check which modules it unlocks, and handle license errors.

English

Call nexdevit::nexpdf::activate_license 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

#include <nexpdf.hpp>

namespace nexpdf = nexdevit::nexpdf;

int32_t result = nexpdf::activate_license(read_text("nexpdf_license.xml"), "server", "com.acme.invoicing");
std::cout << (result == 1 ? "trial" : "licensed") << "\n";
ParameterValue
key_or_xmlThe license XML or the bare key
platformserver, linux, macos or windows: one of the platforms on the key
app_idThe application id the key was issued for

It returns 2 for a commercial key and 1 for a trial key.

In C, nexpdf_license_activate(key_or_xml, platform, app_id) returns the same values, or the negative code without throwing.

Reading the key from the environment

On servers, keep the key out of the image and read it from a secret:

const char* key = std::getenv("NEXPDF_LICENSE_KEY");
const char* app_id = std::getenv("NEXPDF_APP_ID");
nexpdf::activate_license(key ? key : "", "server", app_id ? app_id : "com.acme.invoicing");

Check the status

#include <nlohmann/json.hpp>   // an example; use your JSON library

auto status = nlohmann::json::parse(nexpdf::license_status_json());
std::cout << status["state"] << " " << status.value("tier", "") << "\n";

if (nexpdf::is_licensed("signatures")) {
    std::cout << "signing is available\n";
}

license_status_json() returns state (unlicensed, trial, licensed), licensee, id, tier, modules, expires_at, days_left and watermark. Without a license only state, modules (["viewer"]) and watermark are present.

Handle errors

A rejected key throws nexdevit::nexpdf::Error with the negative code in code():

try {
    nexpdf::activate_license(key, "server", "com.acme.invoicing");
} catch (const nexpdf::Error& e) {
    if (e.code() == -4) {
        std::cerr << "NexPDF license expired: renew it in the dashboard\n";
    } else if (e.code() == -6 || e.code() == -7) {
        std::cerr << "NexPDF license is for another platform or app id: " << e.what() << "\n";
    } else {
        std::cerr << "NexPDF license rejected (" << e.code() << "): " << e.what() << "\n";
    }
}

A call that needs a module your key does not include throws nexdevit::nexpdf::Error with code() 0:

try {
    doc.encrypt_aes256("user", "owner");
} catch (const nexpdf::Error& e) {
    std::cerr << e.what() << "\n";  // the active license does not include the 'security' module
}
CodeMeaning
-1Malformed key
-2Bad signature
-3Unsupported key version
-4Expired
-5Not yet valid (check the clock)
-6Wrong platform
-7Wrong 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.

On this page