mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-01-09 14:04:35 +08:00
add extract images example
This commit is contained in:
43
examples/ExtractImages.cs
Normal file
43
examples/ExtractImages.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")))
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user