(Issue #3809) Fix margin calculation; only process pages as required