Saturday, 25 August 2012
How to check a PDFs page size with iTextSharp

I don't know why I found it so hard to get hold of this information. I've placed it onto my blog for reference purposes. As before, if you can suggest a better method of doing this please leave a comment.

 

   1:   public string GetPageSize(string PathToPDF)
   2:          {
   3:              var reader = new PdfReader(PathToPDF);
   4:   
   5:              // A post script point is 0.352777778mm
   6:              const float postScriptPoints = (float)0.352777778;
   7:   
   8:              // The height and width are returned in post script points from iTextSharp
   9:              float height = reader.GetPageSizeWithRotation(1).Height * postScriptPoints;
  10:              float width = reader.GetPageSizeWithRotation(1).Width * postScriptPoints;
  11:   
  12:              reader.Close();
  13:   
  14:              if ((width >= 210 && width < 211)
  15:                  && (height >= 297 && height < 298))
  16:              {
  17:                  return "A4";
  18:              }
  19:   
  20:              return "unknown page size";
  21:          }