add extract images example

This commit is contained in:
Eliot Jones
2020-04-18 18:55:44 +01:00
parent 563985e2b4
commit 23087fe38c
2 changed files with 47 additions and 0 deletions

43
examples/ExtractImages.cs Normal file
View File

@@ -0,0 +1,43 @@
namespace UglyToad.Examples
{
using System;
using System.Linq;
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())
{
switch (image)
{
case XObjectImage ximg:
byte[] b;
try
{
b = ximg.Bytes.ToArray();
}
catch
{
b = ximg.RawBytes.ToArray();
}
Console.WriteLine($"Image with {b.Length} bytes and dictionary {ximg.ImageDictionary}.");
break;
case InlineImage inline:
Console.WriteLine($"Inline image: {inline.RawBytes.Count} bytes.");
break;
}
}
}
}
}
}
}

View File

@@ -19,6 +19,10 @@
{1,
("Extract Words with newline detection",
() => OpenDocumentAndExtractWords.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf")))
},
{2,
("Extract images",
() => ExtractImages.Run(Path.Combine(filesDirectory, "2006_Swedish_Touring_Car_Championship.pdf")))
}
};