mirror of
				https://github.com/UglyToad/PdfPig.git
				synced 2025-10-31 16:46:55 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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;
 | |
|                         switch (image)
 | |
|                         {
 | |
|                             case XObjectImage ximg:
 | |
|                                 type = "XObject";
 | |
|                                 break;
 | |
|                             case InlineImage inline:
 | |
|                                 type = "Inline";
 | |
|                                 break;
 | |
|                         }
 | |
| 
 | |
|                         Console.WriteLine($"Image with {b.Count} bytes of type '{type}' on page {page.Number}. Location: {image.Bounds}.");
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | 
