'Calculates the number of pages in this PDF file.
:return: number of pages
:rtype: int
:raises PdfReadError: if file is encrypted and restrictions prevent
this action.'

def getNumPages(self):

    if self.isEncrypted:
        try:
            self._override_encryption = True
            self.decrypt('')
            return self.trailer['/Root']['/Pages']['/Count']
        except:
            raise utils.PdfReadError('File    has    not    been    decrypted')
        finally:
            self._override_encryption = False
    else:
        if (self.flattenedPages == None):
            self._flatten()
        return len(self.flattenedPages)
