Skip to Content
NexPDF

Opening and saving

Open PDFs from files and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for .NET.

English

License module

Opening PDFs and pictures, saving and copying are part of the viewer module and work without a key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and above).

Open a file or bytes

NexPdfDocument.Open(path, password = null) reads a file; NexPdfDocument.OpenBytes(data, password = null) takes the bytes you already have, for example from an upload or blob storage.

using Nexdevit.NexPdf;

using (var doc = NexPdfDocument.Open("contract.pdf"))
    Console.WriteLine($"{doc.PageCount} pages");

var data = File.ReadAllBytes("contract.pdf");
using (var doc = NexPdfDocument.OpenBytes(data))
    Console.WriteLine(doc.PageCount);

Both accept JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module, Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are detected and converted on open.

A document holds native memory. Dispose it with Dispose() or using; after that any call throws ObjectDisposedException, and IsDisposed tells you whether it is disposed.

Encrypted documents

Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.

try
{
    using var doc = NexPdfDocument.Open("locked.pdf", "s3cret");
    Console.WriteLine(doc.SecurityJson());
}
catch (NexPdfException e) when (e.Message is "password required" or "incorrect password")
{
    AskTheUserAgain();
}

Office files, e-books and comics carry no PDF password: since 1.1 they are converted as usual and the password is ignored, so you can pass the password a user typed for any file. Files that are only restricted by permissions (no open password) open without a password. doc.SecurityJson() reports the encryption and permissions; see Security.

Create a PDF from other formats

SourceMethodModule
JPEG, PNG, TIFFNexPdfDocument.OpenImage(data)none
HTMLNexPdfDocument.FromHtml(html)conversion
Word, Excel, PowerPointNexPdfDocument.OpenOffice(data)conversion
EPUB, FictionBook, XPS/OpenXPS, CBZNexPdfDocument.OpenEbook(data)conversion

The same methods exist as static members of NexPdfSdk.

using (var doc = NexPdfDocument.OpenImage(File.ReadAllBytes("scan.tiff")))   // one page per TIFF frame
    doc.Save("scan.pdf");

using (var doc = NexPdfDocument.FromHtml("<h1>Invoice 42</h1><p>Total: 120 EUR</p>"))
    doc.Save("invoice.pdf");

To add pictures to an existing document, use InsertImagePages(index, data); see Editing.

Save

Save(path) writes the document with every change as a complete new file. SaveBytes() returns the same bytes instead of writing a file.

doc.Save("out.pdf");
byte[] pdf = doc.SaveBytes();            // e.g. to return from a web handler

Fast Web View

A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:

doc.Save("web.pdf", linearized: true);
byte[] web = doc.SaveLinearized();

Signed files

Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.

Copy a document

Copy() returns an independent document with the same content and changes. Use it as an undo snapshot, or to hand work to another thread:

var snapshot = doc.Copy();
doc.DeletePage(0);
// ... undo:
doc.Dispose();
doc = snapshot;

On this page