Cross-platform library to render pdf documents as images with PdfPig using SkiaSharp, or to extract images contained in a pdf page as SkiaSharp images.
Available as a Nuget package https://www.nuget.org/packages/PdfPig.Rendering.Skia/
Uses parts of PDFBox code.
using UglyToad.PdfPig.Graphics.Colors;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Rendering.Skia;
using SkiaSharp;
[...]
using (var document = PdfDocument.Open(_path, SkiaRenderingParsingOptions.Instance))
{
string fileName = Path.GetFileName(_path);
document.AddSkiaPageFactory(); // Same as document.AddPageFactory<SKPicture, SkiaPageFactory>() and document.AddPageFactory<PdfPageSize, PageSizeFactory>()
for (int p = 1; p <= document.NumberOfPages; p++)
{
using (var fs = new FileStream($"{fileName}_{p}.png", FileMode.Create))
using (var ms = document.GetPageAsPng(p, _scale, RGBColor.White))
{
ms.WriteTo(fs);
}
}
}using UglyToad.PdfPig.Graphics.Colors;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Rendering.Skia;
using SkiaSharp;
[...]
using (var document = PdfDocument.Open(_path, SkiaRenderingParsingOptions.Instance))
{
document.AddSkiaPageFactory(); // Same as document.AddPageFactory<SKPicture, SkiaPageFactory>() and document.AddPageFactory<PdfPageSize, PageSizeFactory>()
for (int p = 1; p <= document.NumberOfPages; p++)
{
using var skBitmap = document.GetPageAsSKBitmap(p, _scale, RGBColor.White);
// Use the SKBitmap
}
}Starting from version 0.1.14.1, the SKPicture.CullRect cannot be used anymore to get the page size.
using UglyToad.PdfPig.Graphics.Colors;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Rendering.Skia;
using SkiaSharp;
[...]
using (var document = PdfDocument.Open(_path, SkiaRenderingParsingOptions.Instance))
{
document.AddSkiaPageFactory(); // Same as document.AddPageFactory<SKPicture, SkiaPageFactory>() and document.AddPageFactory<PdfPageSize, PageSizeFactory>()
for (int p = 1; p <= document.NumberOfPages; p++)
{
using var skPicture = document.GetPage<SKPicture>(p);
// Use the SKPicture
}
}using UglyToad.PdfPig.Rendering.Skia.Helpers;
[...]
using (var document = PdfDocument.Open(_path, SkiaRenderingParsingOptions.Instance))
{
for (int p = 1; p <= document.NumberOfPages; p++)
{
var page = document.GetPage(p);
foreach (var pdfImage in page.GetImages())
{
using var skBitmap = pdfImage.GetSKBitmap();
// Use SKBitmap
// In order to get the SKImage, use the following:
using var skImage = SKImage.FromBitmap(skBitmap);
// Use SKImage
}
}
}The visual regression tests (TestRendering.PdfPigSkiaTest) compare the rendered output against expected
images and must be run in Release mode.
Important
Expected images are platform specific: Windows, Linux and macOS do not produce identical output. The differences are font related. When a glyph has no usable embedded outline, rendering falls back to a system font, and the available installed fonts (and their substitutions) differ from one OS / machine to another. As a result, an expected image baseline generated on one OS will not match pixel-for-pixel on another.