How can I recognize only the first and the last page without knowing total page count in advance and recognizing all of them?
To do this, you could use MoveById and DeleteById methods of Pages Object. For example:
var pages = Document.Pages;
int p_count = pages.Count;
if (p_count > 2)
for (int i = p_count; i>2; i--)