Skip to Content
NexPDF

Error handling

The exceptions NexPDF for .NET throws, the engine's error messages, license error codes, and patterns for processing untrusted files.

English

Every NexPDF call either returns its result or throws. There are no status codes to check by hand: the SDK reads the engine's per-thread error after each call and turns it into an exception.

Exception types

ExceptionThrown when
NexPdfExceptionAn engine call failed. Message is the engine's message.
NexPdfException with Code < 0NexPdfSdk.ActivateLicense rejected the key. For every other failure Code is 0.
IOException, FileNotFoundException, UnauthorizedAccessExceptionNexPdfDocument.Open(path) could not read the file, or Save(path) could not write it.
ObjectDisposedExceptionA method was called on a disposed NexPdfDocument.
OverflowExceptionA negative page index was passed where the engine takes an unsigned index.
DllNotFoundException.NET could not find or load the native library, on the first NexPDF call.
EntryPointNotFoundExceptionThe native library is older than the package and lacks a function.
try
{
    using var doc = NexPdfDocument.Open("upload.pdf");
    doc.AddWatermark("CONFIDENTIAL");
    doc.Save("out.pdf");
}
catch (NexPdfException e)
{
    logger.LogWarning("NexPDF could not process upload.pdf: {Message}", e.Message);
}
catch (IOException e)
{
    logger.LogWarning(e, "cannot read or write the file");
}

Common engine messages

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to Open(path, password)
incorrect passwordThe password matches neither the user nor the owner passwordAsk again
no license activated: '<module>' requires a NexPDF license keyThe call needs a module and no key is activeCall ActivateLicense at start-up; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck NexPdfSdk.IsLicensed(module), or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe process keeps running; report the file to support

Other messages describe the input: a malformed or truncated PDF, a page index out of range, invalid JSON options. They are meant to be logged as they are.

License error codes

NexPdfException.Code after ActivateLicense is one of:

CodeMeaning
-1Malformed key
-2Bad signature (the key was changed, or it is not a NexPDF key)
-3Unsupported key version (update the SDK)
-4Expired
-5Not yet valid (check the system clock)
-6Wrong platform
-7Wrong application id

A rejected key leaves the previous license state as it was, so a process that had no key stays in view-only mode. Exception filters (catch (NexPdfException e) when (e.Code == -4)) let you handle each code; see License.

The last error

NexPdfSdk.LastError returns the engine's last error message for the current thread, or null. You rarely need it, because the SDK throws with that message already, but it is useful after NexPdfSdk.Invoke, which reports failures inside its JSON result instead of throwing.

Untrusted input

The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfException, not a crash of the process. When you process uploads:

  • catch NexPdfException per file, so one bad file does not stop a batch;
  • limit the file size you accept before opening it, since documents are read into memory (Open(path) reads the whole file into a byte[]);
  • dispose documents with using so native memory is released on failure too.
static int? PageCountOrNull(byte[] data)
{
    try
    {
        using var doc = NexPdfDocument.OpenBytes(data);
        return doc.PageCount;
    }
    catch (NexPdfException)
    {
        return null;
    }
}

On this page