Table of Contents

Method RenderPages

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

RenderPages(string, PdfRenderOptions, CancellationToken)

Lazily renders the pages selected by PageRange. Yields one (pageIndex, image) tuple at a time so the caller can dispose the buffer between iterations and keep memory flat.

public static IEnumerable<(int PageIndex, ImageBuffer Image)> RenderPages(string inputPath, PdfRenderOptions options = null, CancellationToken cancellationToken = default)

Parameters

inputPath string

Path to the source PDF.

options PdfRenderOptions

Render options including PageRange.

cancellationToken CancellationToken

Cancels the iteration between pages.

Returns

IEnumerable<(int PageIndex, ImageBuffer Image)>

Examples

using LMKit.Document.Pdf;
using LMKit.Media.Image;

PdfRenderOptions options = new() { Zoom = 2.0, PageRange = "1-3,5" };
foreach ((int pageIndex, ImageBuffer img) in PdfRenderer.RenderPages("report.pdf", options))
{
    using (img)
    {
        img.SaveAsPng($"page-{pageIndex + 1}.png");
    }
}

RenderPages(Stream, PdfRenderOptions, bool, CancellationToken)

Lazily renders the pages selected by PageRange from a PDF stream. Yields one (pageIndex, image) tuple at a time so the caller can dispose the buffer between iterations and keep memory flat.

public static IEnumerable<(int PageIndex, ImageBuffer Image)> RenderPages(Stream input, PdfRenderOptions options = null, bool leaveOpen = true, CancellationToken cancellationToken = default)

Parameters

input Stream

Readable stream containing the PDF.

options PdfRenderOptions

Render options including PageRange.

leaveOpen bool

When true (default), the caller keeps ownership of the stream.

cancellationToken CancellationToken

Cancels the iteration between pages.

Returns

IEnumerable<(int PageIndex, ImageBuffer Image)>

Examples

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

using FileStream pdfStream = File.OpenRead("report.pdf");
foreach ((int pageIndex, ImageBuffer img) in PdfRenderer.RenderPages(pdfStream))
{
    using (img)
    {
        img.SaveAsWebp($"page-{pageIndex + 1}.webp", quality: 80);
    }
}

RenderPages(byte[], PdfRenderOptions, CancellationToken)

Lazily renders the pages selected by PageRange from a PDF byte array. Yields one (pageIndex, image) tuple at a time so the caller can dispose the buffer between iterations and keep memory flat.

public static IEnumerable<(int PageIndex, ImageBuffer Image)> RenderPages(byte[] data, PdfRenderOptions options = null, CancellationToken cancellationToken = default)

Parameters

data byte[]

PDF file content as a byte array.

options PdfRenderOptions

Render options including PageRange.

cancellationToken CancellationToken

Cancels the iteration between pages.

Returns

IEnumerable<(int PageIndex, ImageBuffer Image)>

Examples

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

byte[] bytes = File.ReadAllBytes("report.pdf");
foreach ((int pageIndex, ImageBuffer img) in PdfRenderer.RenderPages(bytes))
{
    using (img)
    {
        img.SaveAsJpeg($"page-{pageIndex + 1}.jpg", quality: 85);
    }
}
Share