Here are several questions:
1- How to make the usage of \tcboxfit in the first raster row not distort the layout as shown below?
2- How to precisely calculate the dimensions summation passed to headheight option of geometry so that the header starts exactly at the page top border?
3- Why doesn't \iflastpage{\enlargethispage{-1cm}}{} work inside \NewDocumentCommand{\FillAnswerRules} to make a room for the End of the Exam text?
4- Why is not the space (nearly of \baselineskip) between the header and the mainstream text eliminated despite setting headsep = 0pt?
\documentclass{exam} \usepackage[fontsize = 14pt]{scrextend} \usepackage{lipsum} \usepackage[xparse,breakable,raster,skins]{tcolorbox} \tcbuselibrary{fitting} \pagestyle{headandfoot} \newcommand{\myfooter}{% \iflastpage{\raisebox{5pt}[0pt][0pt]{\large End of the Exam}}{}% \hrule\vspace{5pt}% Instructors% } \newcommand\headerBody{% \begin{tcbitemize}[ raster valign = bottom, valign = bottom, raster columns = 2, blankest, raster after skip = 1ex, ] \tcboxfit[height = 3em, add to width = 2cm, halign = flush left,height=3em] University\\Faculty\\Program \tcbitem[add to width = -2cm, halign = flush right,] \includegraphics[height = 3em]{example-image} \end{tcbitemize} % \hrule % \begin{tcbraster}[ raster columns = 3, valign = center, raster before skip = 1ex, raster after skip = 1ex, blankest ] \tcboxfit{Final\\Semester} \tcboxfit[before upper*=\centering]{Course Code} \tcboxfit[halign = flush right]{\textbf{date}\\\textbf{time}} \end{tcbraster} % \hrule % \begin{tcbraster}[ raster columns = 1, valign = center, halign = flush center, raster before skip = 1ex, raster after skip = 1ex, blankest ] \tcboxfit[interior engine = standard,colback = gray!20, top = 1ex, bottom = 1ex,]{\textbf{Course}}% \end{tcbraster} % \hrule % \begin{tcbraster}[ raster columns = 1, valign = center, halign = flush center, raster before skip = 1ex, raster after skip = 1ex, blankest, ] \tcboxfit[top = 1ex, bottom = 1ex,]{This exam contains.}% \end{tcbraster} % \hrule% } \newcommand\headerContents{% https://tex.stackexchange.com/a/547910/2288 \begin{tcbraster}[ fit algorithm=hybrid*, raster equal height = rows, raster halign = center, raster valign = center, raster equal skip = 0mm, raster force size = false, boxsep = 0pt, beforeafter skip = 0pt, leftright skip = 0pt, raster right skip = 0pt, raster left skip = 0pt, raster before skip = 0pt, raster after skip = 0pt, nobeforeafter, height = 2em, raster rows=4, ]% \headerBody% \end{tcbraster}% } \firstpageheader{}{\headerContents}{} \runningheader{}{\headerContents}{} \firstpagefooter{}{\myfooter}{} \runningfooter{}{\myfooter}{} \ExplSyntaxOn \keys_define:nn { diaa / answers } { d .dim_set:N = \l__diaa_answers_distance_dim, t .dim_set:N = \l__diaa_answers_thickness_dim, c .tl_set:N = \l__diaa_answers_color_tl, d .initial:n = 10mm, t .initial:n = 0.2pt, c .initial:n = gray, } \NewTColorBox { fillAnswerRules } { O{} } { height~fill, text~fill, colback=white, boxrule=2pt, sharp~corners, boxsep=0pt, left=10pt, right=10pt, top=8pt, bottom=0pt, #1 } \NewDocumentCommand{\FillAnswerRules}{O{}} { \iflastpage{\enlargethispage{-1cm}}{} \begin{fillAnswerRules} \keys_set:nn { diaa / answers } { #1 } \leaders \hbox:n { \makebox[\textwidth][s] { \color{\l__diaa_answers_color_tl} \vrule width 0pt height 0.5\l__diaa_answers_distance_dim depth 0.5\l__diaa_answers_distance_dim \leaders \hrule height \l__diaa_answers_thickness_dim \hfill } }\vfill \null \end{fillAnswerRules} } \ExplSyntaxOff \usepackage[ a4paper, includehead, headsep=0pt, includefoot, foot=0mm, bottom=20mm, headheight = \dimexpr9em+9ex+0pt, top=0mm, showframe ]{geometry} \begin{document} \begin{questions} \question \begin{parts} \part \lipsum[1-2] \FillAnswerRules \FillAnswerRules \part \lipsum[1] \FillAnswerRules \end{parts} \end{questions} \end{document} 

