Skip to Content
NexPDF

Platform notes

Threading, ASP.NET Core, memory, packaging and deployment notes for NexPDF for .NET on servers and desktops.

English

Threading

  • A NexPdfDocument is not thread-safe. Use each document from one thread at a time.
  • Different documents can be used on different threads at the same time, so threads scale across cores for independent files. The state shared between documents (the license and the renderer's caches) is thread-safe.
  • doc.Copy() gives an independent document you can hand to another thread.
  • The license is process-wide: one ActivateLicense call covers every thread.
  • Errors are tracked per thread, so an error on one thread never leaks into another.

Every call is synchronous; there is no async API. P/Invoke calls hold no managed lock, so Parallel.ForEach or tasks render and convert in parallel:

using System.Collections.Concurrent;
using Nexdevit.NexPdf;

var pngs = new ConcurrentDictionary<string, byte[]>();
Parallel.ForEach(paths, new ParallelOptions { MaxDegreeOfParallelism = 8 }, path =>
{
    using var doc = NexPdfDocument.Open(path);   // one document per task
    pngs[path] = doc.RenderPagePng(0, 144);
});

ASP.NET Core

Activate the license once in Program.cs, then open a document per request. Do not register a NexPdfDocument as a singleton or share one between requests, because two requests could use it at the same time.

app.MapPost("/thumbnail", async (HttpRequest request) =>
{
    using var ms = new MemoryStream();
    await request.Body.CopyToAsync(ms);
    using var doc = NexPdfDocument.OpenBytes(ms.ToArray());
    return Results.File(doc.RenderThumbPng(0, 256), "image/png");
});

Desktop apps

Engine calls block the calling thread until they finish. In WPF, WinForms or Avalonia, run rendering, conversion and OCR with await Task.Run(...) and update the UI with the result after the await. For a mobile or desktop viewer control, see .NET MAUI.

Memory

  • A document is read fully into memory when it is opened. Budget roughly the file size plus the decoded pages you touch.
  • The engine allocates native memory, which the garbage collector does not count. GC settings and heap limits do not bound it, and container memory limits must leave room for it.
  • A rendered page at 144 dpi of an A4 page is about 1190 x 1684 pixels before PNG compression. Render large documents page by page instead of keeping every PNG.
  • The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a logo or font used in many files is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB on a process that renders many large, image-heavy files. Memory that stays high after a rendering batch is this cache being reused, not a leak.
  • Dispose documents with using. A finalizer frees one that was not disposed, but only after a garbage collection, at no predictable time.
  • MemoryUsageBytes() and TrimMemory() let long-lived documents give memory back; see Compare and optimise.

Packaging and size

  • The assembly itself is managed code with no dependency outside .NET. The engine is one shared library per platform: libnexpdf_ffi.so, libnexpdf_ffi.dylib or nexpdf_ffi.dll, about 20 MB for one architecture (the macOS library is universal and holds two).
  • The default engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard fonts and PDF/A. NexPdfSdk.HasBundledFonts() tells you which build you have.
  • A package that carries the libraries under runtimes/<rid>/native/ (see Installation) copies every runtime into a portable build. Publish for one runtime (dotnet publish -r linux-x64) to ship only the library that runtime needs.
  • Single-file publishing (-p:PublishSingleFile=true) leaves native libraries next to the executable unless you set IncludeNativeLibrariesForSelfExtract. Ship the library with the executable either way.
  • In containers, keep the library in the image and set LD_LIBRARY_PATH, as in the Docker example.

Serverless

Container and function runtimes work like any Linux x64 or arm64 host, as long as the image has glibc 2.35 or later (see Troubleshooting). Ship the library in your deployment package or image and activate the license once at start-up so warm invocations reuse it. Use the temporary folder for intermediate files, or work with OpenBytes and SaveBytes to stay in memory.

Network

The engine makes no network calls of its own: license keys are verified offline and documents never leave the process. Only signing options you set (tsa_url for a timestamp, ltv for OCSP and CRL lookups) and NexPdfSdk.AddLtv contact the servers named in the certificates.

On this page