title: Extracting pdf images in a correct order iTextSharp
body: <p>I'm trying to extract images from a PDF File, but I really need to have it at the correct order to get the correct image. </p>
  
  <pre><code>    static void Main(string[] args)
      {
          string filename = "D:\\910723575_marca_coletiva.pdf";
  
          PdfReader pdfReader = new PdfReader(filename);
  
          var imagemList = ExtraiImagens(pdfReader);
  
          // converter byte[] para um bmp
          List&lt;Bitmap&gt; bmpSrcList = new List&lt;Bitmap&gt;();
          IList&lt;byte[]&gt; imagensProcessadas = new List&lt;byte[]&gt;();
  
          foreach (var imagem in imagemList)
          {
  
              System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
              try
              {
                  Image img = (Image)converter.ConvertFrom(imagem);
                  ConsoleWriteImage(img);
                  imagensProcessadas.Add(imagem);
              }
              catch (Exception)
              {
                  continue;
              }
  
          }
  
          System.Console.ReadLine();
      }
  
      public static void ConsoleWriteImage(Image img)
      {
          int sMax = 39;
          decimal percent = Math.Min(decimal.Divide(sMax, img.Width), decimal.Divide(sMax, img.Height));
          Size resSize = new Size((int)(img.Width * percent), (int)(img.Height * percent));
          Func&lt;System.Drawing.Color, int&gt; ToConsoleColor = c =&gt;
          {
              int index = (c.R &gt; 128 | c.G &gt; 128 | c.B &gt; 128) ? 8 : 0;
              index |= (c.R &gt; 64) ? 4 : 0;
              index |= (c.G &gt; 64) ? 2 : 0;
              index |= (c.B &gt; 64) ? 1 : 0;
              return index;
          };
          Bitmap bmpMin = new Bitmap(img, resSize.Width, resSize.Height);
          Bitmap bmpMax = new Bitmap(img, resSize.Width * 2, resSize.Height * 2);
          for (int i = 0; i &lt; resSize.Height; i++)
          {
              for (int j = 0; j &lt; resSize.Width; j++)
              {
                  Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMin.GetPixel(j, i));
                  Console.Write("██");
              }
  
              Console.BackgroundColor = ConsoleColor.Black;
              Console.Write("    ");
  
              for (int j = 0; j &lt; resSize.Width; j++)
              {
                  Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2, i * 2));
                  Console.BackgroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2, i * 2 + 1));
                  Console.Write("▀");
  
                  Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2 + 1, i * 2));
                  Console.BackgroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2 + 1, i * 2 + 1));
                  Console.Write("▀");
              }
              System.Console.WriteLine();
          }
      }
  
      public static IList&lt;byte[]&gt; ExtraiImagens(PdfReader pdfReader) 
      {
          var data = new byte[] { };
  
          IList&lt;byte[]&gt; imagensList = new List&lt;byte[]&gt;();
  
          for (int numPag = 1; numPag &lt;= 3; numPag++)
          //for (int numPag = 1; numPag &lt;= pdfReader.NumberOfPages; numPag++)
          {
              var pg = pdfReader.GetPageN(numPag);
              var res = PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)) as PdfDictionary;
              var xobj = PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)) as PdfDictionary;
              if (xobj == null) continue;
  
              var keys = xobj.Keys;
              if (keys == null) continue;
  
              PdfObject obj = null;
              PdfDictionary tg = null;
  
              for (int key = 0; key &lt; keys.Count; key++)
              {
                  obj = xobj.Get(keys.ElementAt(key));
  
                  if (!obj.IsIndirect()) continue;
  
                  tg = PdfReader.GetPdfObject(obj) as PdfDictionary;
  
                  obj = xobj.Get(keys.ElementAt(key));
                  if (!obj.IsIndirect()) continue;
                  tg = PdfReader.GetPdfObject(obj) as PdfDictionary;
  
                  var type = PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)) as PdfName;
                  if (!PdfName.IMAGE.Equals(type)) continue;
  
                  int XrefIndex = (obj as PRIndirectReference).Number;
                  var pdfStream = pdfReader.GetPdfObject(XrefIndex) as PRStream;
  
                  data = PdfReader.GetStreamBytesRaw(pdfStream);
  
                  imagensList.Add(PdfReader.GetStreamBytesRaw(pdfStream));
              }
          }
  
          return imagensList;
      }
  }
  </code></pre>
  
  <p>The method ConsoleWriteImage is only to print the image at the console and I used it to study the behavior of the order that iTextSharp was retrieving it for me , based on my code. </p>
  
  <p>Any help ?</p>