Method RenderPagesAsync
RenderPagesAsync(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 IAsyncEnumerable<(int PageIndex, ImageBuffer Image)> RenderPagesAsync(string inputPath, PdfRenderOptions options = null, CancellationToken cancellationToken = default)
Parameters
inputPathstringPath to the source PDF.
optionsPdfRenderOptionsRender options including PageRange.
cancellationTokenCancellationTokenCancels the iteration between pages.
Returns
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");
}
}
RenderPagesAsync(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 IAsyncEnumerable<(int PageIndex, ImageBuffer Image)> RenderPagesAsync(Stream input, PdfRenderOptions options = null, bool leaveOpen = true, CancellationToken cancellationToken = default)
Parameters
inputStreamReadable stream containing the PDF.
optionsPdfRenderOptionsRender options including PageRange.
leaveOpenboolWhen true (default), the caller keeps ownership of the stream.
cancellationTokenCancellationTokenCancels the iteration between pages.
Returns
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);
}
}
RenderPagesAsync(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 IAsyncEnumerable<(int PageIndex, ImageBuffer Image)> RenderPagesAsync(byte[] data, PdfRenderOptions options = null, CancellationToken cancellationToken = default)
Parameters
databyte[]PDF file content as a byte array.
optionsPdfRenderOptionsRender options including PageRange.
cancellationTokenCancellationTokenCancels the iteration between pages.
Returns
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);
}
}