Mr. Khatami served as president of Iran from 1997 to 2005 .