The following example demonstrates how to get the images in a PDF document & then display information about them, like on what page they can be found & how many images there are on that page.
| Get the images from a PDF document (C#) |
Copy Code |
|---|---|
public static void GetImages() { Console.WriteLine( "=== Get IMAGES ===" ); var filename = "Engineering_Civil Seven.pdf"; // Loads a document. using( var pdfDoc = PdfDocument.Load( ImagesSample.ImagesSampleResourcesDirectory + filename ) ) { var outputFileName = "GetImages.pdf"; var outputPath = ImagesSample.ImagesSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page. var page1 = pdfoutput.Pages.First(); // Adds a title. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page1.AddParagraph( "Get images", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Adds red Courier text. var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d, Brushes.Red ); page1.AddText( $"Get Images per pages from: {filename}", new Point( 25, 60 ), redTextStyle ); var imageTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d ); var counter = 0; foreach( var page in pdfDoc.Pages ) { // Gets the Images from the loaded document's Pages. var pageImages = page.Images; if( pageImages.Count > 0 ) { // Adds text that displays the PageId and the Page's Image count. page1.AddText( $"Page {page.Id}: {pageImages.Count} images found", new Point( 25, 90 + ( counter * 15 )), imageTextStyle ); foreach( var image in pageImages ) { page1.AddText( $" Location: {image.Bounds.TopLeft}, Size: {image.Bounds.Width:F0} X {image.Bounds.Height:F0}", new Point( 25, 105 + ( counter * 15 )), imageTextStyle ); counter++; } } counter++; } // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } } | |