Table of Contents

Method SavePageAsPng

Namespace
LMKit.Document.Pdf
Assembly
LM-Kit.NET.dll

SavePageAsPng(string, int, string, PdfRenderOptions, int, CancellationToken)

Renders one page and writes it as PNG to a file.

public static void SavePageAsPng(string inputPath, int pageIndex, string outputPath, PdfRenderOptions options = null, int compressionLevel = 6, CancellationToken cancellationToken = default)

Parameters

inputPath string

Path to the source PDF.

pageIndex int

Zero-based index of the page to render.

outputPath string

Destination file path.

options PdfRenderOptions

Render options. PageRange is ignored.

compressionLevel int

PNG compression level 0 (fastest) to 9 (smallest). Default 6.

cancellationToken CancellationToken

Cancels the operation.

Examples

using LMKit.Document.Pdf;

PdfRenderer.SavePageAsPng("report.pdf", pageIndex: 0, "thumb.png");

// Smallest PNG (slowest), grayscale, at 3x zoom
PdfRenderOptions options = new()
{
    Zoom = 3.0,
    PixelFormat = LMKit.Media.Image.ImagePixelFormat.GRAY8,
};
PdfRenderer.SavePageAsPng("scan.pdf", 0, "thumb.png", options, compressionLevel: 9);

SavePageAsPng(string, int, Stream, PdfRenderOptions, int, CancellationToken)

Renders one page and writes it as PNG to a writable stream.

public static void SavePageAsPng(string inputPath, int pageIndex, Stream outputStream, PdfRenderOptions options = null, int compressionLevel = 6, CancellationToken cancellationToken = default)

Parameters

inputPath string

Path to the source PDF.

pageIndex int

Zero-based index of the page to render.

outputStream Stream

Writable destination stream.

options PdfRenderOptions

Render options.

compressionLevel int

PNG compression level 0-9. Default 6.

cancellationToken CancellationToken

Cancels the operation.

Examples

using System.IO;
using LMKit.Document.Pdf;

// Stream a page preview straight into an HTTP response body
// (e.g. an ASP.NET endpoint serving a PDF thumbnail).
using MemoryStream output = new();
PdfRenderer.SavePageAsPng("report.pdf", pageIndex: 0, output);
byte[] payload = output.ToArray();
Share