title: How can I get the resolution of an embeded image in a PDF using ITextSharp
body: <p>I've built a method that tries to see if the resolution of all the embedded images in a given pdf is at least 300 PPI (print-worthy).  What it does is cycle through each image on a page, and compare its width and height to the artbox width and height.  It works successfully if there is only one image per page, but when there are multiple, the artbox size includes all of the images, throwing the numbers off.  </p>
  
  <p>I was hoping that somebody might have some idea of how to get the rectangle size the image is drawn in so I can compare correctly, or if there is an easier way to get the PPI of an image object (as it would be rendered in its rectangle, not in raw form).</p>
  
  <p>this is the code for the aforementioned method</p>
  
  <pre><code>    private static bool AreImages300PPI(PdfDictionary pg)
      {
          var res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
          var xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
          if (xobj == null) return true;
          foreach (PdfName name in xobj.Keys)
          {
              PdfObject obj = xobj.Get(name);
              if (!obj.IsIndirect()) continue;
              var tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
              var type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
              var width = float.Parse(tg.Get(PdfName.WIDTH).ToString());
              var height = float.Parse(tg.Get(PdfName.HEIGHT).ToString());
              var artbox = (PdfArray) pg.Get(PdfName.ARTBOX);
              var pdfRect = new PdfRectangle(float.Parse(artbox[0].ToString()), float.Parse(artbox[1].ToString()),
                  float.Parse(artbox[2].ToString()), float.Parse(artbox[3].ToString()));
  
              if (PdfName.IMAGE.Equals(type) &amp;&amp; (width &lt; pdfRect.Width*300/72 || height &lt; pdfRect.Height*300/72)
                  || ((PdfName.FORM.Equals(type) || PdfName.GROUP.Equals(type)) &amp;&amp; !AreImages300PPI(tg)))
              {
                  return false;
              }
          }
          return true;
      }
  </code></pre>
  
  <p>for reference, here is the method that calls it: </p>
  
  <pre><code>    internal static List&lt;string&gt; GetLowResWarnings(string MergedPDFPath)
      {
          var returnlist = new List&lt;string&gt;();
          using (PdfReader pdf = new PdfReader(MergedPDFPath))
          {
                  for (int pageNumber = 1; pageNumber &lt;= pdf.NumberOfPages; pageNumber++)
                  {
                      var pg = pdf.GetPageN(pageNumber);
                      if (!AreImages300PPI(pg))
                          returnlist.Add(pageNumber.ToString());
                  }
          }
          return returnlist;
      }
  </code></pre>
  
  <p>Thanks for any help you can provide.</p>