Skip to Content
NexPDF

Getting started

Activate a license, open a PDF, count its pages, render a page to PNG and extract its text with NexPDF for .NET.

English

This example opens a PDF, prints the page count, renders the first page to a PNG file and prints the text of every page. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.

Create a project

dotnet new console -n QuickStart
cd QuickStart
dotnet add package Nexdevit.NexPdf --version 1.1.0

Make the native library available as described in Installation.

Write the program

Program.cs
using Nexdevit.NexPdf;

// 1. Activate the license once per process (optional for viewing).
var licenseFile = Environment.GetEnvironmentVariable("NEXPDF_LICENSE");
if (licenseFile is not null)
    NexPdfSdk.ActivateLicense(File.ReadAllText(licenseFile), "server", "com.acme.quickstart");

// 2. Open the document.
var path = args.Length > 0 ? args[0] : "sample.pdf";
using var doc = NexPdfDocument.Open(path);

// 3. Page count and size.
Console.WriteLine($"{path}: {doc.PageCount} pages, first page {doc.PageWidth(0):F0} x {doc.PageHeight(0):F0} pt");

// 4. Render the first page at 144 dpi.
File.WriteAllBytes("page1.png", doc.RenderPagePng(0, 144));

// 5. Extract the text of every page.
for (var i = 0; i < doc.PageCount; i++)
{
    Console.WriteLine($"--- page {i + 1} ---");
    Console.WriteLine(doc.ExtractPageText(i));
}

Run it

dotnet run -- contract.pdf

page1.png is written to the working directory.

What the code does

  • NexPdfSdk.ActivateLicense(keyOrXml, platform, appId) accepts the license XML or the bare key. The app id must be one the key was issued for. See License.
  • NexPdfDocument.Open(path) opens a PDF; Open(path, password) opens an encrypted one. Word, Excel and PowerPoint files are converted when the license includes conversion; JPEG, PNG and TIFF pictures become one page each. OpenBytes(bytes) opens from memory.
  • PageWidth(i) and PageHeight(i) are the displayed size in points: crop box, rotation and /UserUnit applied.
  • RenderPagePng(i, dpi) returns PNG bytes. 72 dpi is one pixel per point.
  • ExtractPageText(i) returns the page text in reading order.

Next steps

On this page