Xceed PDF Library for .NET Documentation v1.0
In This Topic
    Get the data from specific images
    In This Topic

    Introduction

    The following example demonstrates how to load an existing PDF document, get the images it contains, analyze their data (format, location, bit depth, etc.) & output this information into a newly created PDF document.


    Get information about the images in a PDF document (C#)
    Copy Code
    public static void GetImagesData()
    {
      Console.WriteLine( "=== GET IMAGES DATAS ===" );
      var filename = "Animal_Welfare_Magazine_January_to_June_2023.pdf";
    
      // Load a pdf document.
      using( var pdfDoc = PdfDocument.Load( ImagesSample.ImagesSampleResourcesDirectory + filename ) )
      {
        var outputFileName = "GetImagesData.pdf";
        var outputPath = ImagesSample.ImagesSampleOutputDirectory + outputFileName;
    
        // Create a pdf document.
        using( var pdfoutput = PdfDocument.Create( outputPath ) )
        {
          // Get the first page.
          var page1 = pdfoutput.Pages.First();
    
          // Add Title.
          var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
          page1.AddParagraph( "Getting images datas", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
          var exportData = new ExportInfoToPdf();
    
          // Add text.
          exportData.AddLine( pdfoutput, $"Printing Images Data from: {filename}\n\n", Color.Red );
    
          foreach( var page in pdfDoc.Pages )
          {
            // Get images from loaded document pages.
            var pageImages = page.Images;
            if( pageImages != null )
            {
              foreach( var image in pageImages )
              {
                // Add image data.
                exportData.AddLine( pdfoutput, $"Format: {image.Format}; BitDepth: {image.BitDepth}; Location: {image.Bounds.TopLeft}; Page: {page.Id}\n\n" );
              }
            }
          }
    
          // Save the output document.
          pdfoutput.Save();
          Console.WriteLine( $"Info exported to path: {outputFileName}" );
        }
      }
    }

    See Also