PdfPig/examples/ExtractImages.cs

41 lines
1.2 KiB
C#
Raw Normal View History

2020-04-19 01:55:44 +08:00
namespace UglyToad.Examples
{
using System;
using PdfPig;
using PdfPig.Content;
using PdfPig.XObjects;
internal static class ExtractImages
{
public static void Run(string filePath)
{
using (var document = PdfDocument.Open(filePath))
{
foreach (var page in document.GetPages())
{
foreach (var image in page.GetImages())
{
if (!image.TryGetBytes(out var b))
{
b = image.RawBytes;
}
var type = string.Empty;
2020-04-19 01:55:44 +08:00
switch (image)
{
case XObjectImage ximg:
type = "XObject";
2020-04-19 01:55:44 +08:00
break;
case InlineImage inline:
type = "Inline";
2020-04-19 01:55:44 +08:00
break;
}
Console.WriteLine($"Image with {b.Count} bytes of type '{type}' on page {page.Number}. Location: {image.Bounds}.");
2020-04-19 01:55:44 +08:00
}
}
}
}
}
}