Rendering pages
Render PDF pages and thumbnails to PNG with NexPDF for .NET, and map PDF coordinates to pixels with NexPdfPageView.
License module
Rendering is part of the viewer module and works without a key. Unlicensed and trial renders carry
a watermark; commercial keys render without one.
Render a page to PNG
RenderPagePng(pageIndex, dpi) returns the PNG bytes of a page. At 72 dpi one pixel is one PDF
point; 144 dpi is a good default for screens, 300 dpi for print.
using Nexdevit.NexPdf;
using var doc = NexPdfDocument.Open("report.pdf");
for (var i = 0; i < doc.PageCount; i++)
File.WriteAllBytes($"page-{i + 1}.png", doc.RenderPagePng(i, 144));In ASP.NET Core, return the bytes directly: Results.File(doc.RenderPagePng(0, 144), "image/png").
Render a thumbnail
RenderThumbPng(pageIndex, maxDim) fits the page into a square of maxDim pixels, keeping the
aspect ratio:
var thumb = doc.RenderThumbPng(0, 256);Page size and orientation
PageWidth(i) and PageHeight(i) return the displayed size in points, with the crop box, /Rotate
and /UserUnit applied. PageView(i) returns a NexPdfPageView with Width, Height,
Rotation, UserUnit, Crop and ToDisplay, the matrix from PDF user space to the displayed page
(origin at the top-left, y down). PageViewJson(i) returns the same data as JSON.
var view = doc.PageView(0);
Console.WriteLine($"{view.Width} x {view.Height} pt, rotated {view.Rotation}");NexPdfPageView converts between PDF points and pixels. scale is pixels per point, so pass
dpi / 72 for a page rendered at dpi:
double scale = 144 / 72.0;
var (left, top, right, bottom) = view.RectToView(72, 700, 300, 716, scale); // PDF rect to pixels
var (x, y) = view.ToPdf(tapX, tapY, scale); // a click to a PDF point
var (dx, dy) = view.DeltaToPdf(dragX, dragY, scale); // a drag to a PDF offsetToView, ToPdf, DeltaToPdf, RectToView and RectToPdf take rotation and crop into account.
Overprint simulation
Print-oriented PDF/X files can use overprint. SetOverprint(mode) sets how later renders treat it:
0 simulates it for PDF/X documents only (the default), 1 always, 2 never.
doc.SetOverprint(1);
var png = doc.RenderPagePng(0, 300);Memory
Rendering caches decoded fonts and images per document. For long-running services,
doc.TrimMemory() drops those caches and doc.MemoryUsageBytes() reports the current use.