This works for the main part of the question. This is much, much, much slower than the code for filling with plain (unnumbered) rules or dots. Instead of copying a single box containing a rule or line of dots, we construct a coffin incrementally. For the first line, we create a base coffin containing the rule. We then attach a number on the right. This is actually in the margin. For each subsequent line, we first test to see if space remains on the page. If so, we add two further coffins: a copy of the base one with the line and a new one with the next line number.
\documentclass[a4paper]{article} % ateb: https://tex.stackexchange.com/a/713911/ \usepackage{xcolor} \usepackage[]{geometry} \makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % copied verbatim, excepting format from Joseph Wright's' siunitx.sty under LPPL \@ifundefined{ExplLoaderFileDate}{% \RequirePackage{expl3}% }{} % almost verbatim from siunitx.sty \@ifl@t@r\ExplLoaderFileDate{2022-02-24}{% }{% \PackageError{fillwith}{Support package expl3 too old} {% You need to update your installation of the bundles 'l3kernel' and 'l3packages'.\MessageBreak Loading~fillwith~will~abort!% }% \endinput }% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\RequirePackage{svn-prov} \ExplSyntaxOn % BEGIN vars \coffin_new:N \l__fillwith_line_coffin \coffin_new:N \l__fillwith_lines_coffin \coffin_new:N \l__fillwith_tmpa_coffin \dim_new:N \l__fillwith_ht_dim \dim_new:N \l__fillwith_rem_dim \dim_new:N \l__fillwith_line_ht_dim \dim_set:Nn \l__fillwith_line_ht_dim { 0.4pt } \int_new:N \l__fillwith_cnt_int % END vars % BEGIN keys \keys_define:nn { fillwith } { fillwith~ht .tl_set:N = \l__fillwith_ht_tl, fillwith~ht .initial:n = 2, color .meta:n = { colour = #1, }, colour .code:n = { \colorlet{fillwithcolour}{#1} }, colour .initial:n = gray, } % END keys % BEGIN fns \cs_new_protected_nopar:Nn \fillwith_style: {} \cs_new_protected_nopar:Nn \fillwith_coffin_tht:N { \dim_eval:n { \coffin_ht:N #1 + \coffin_dp:N #1 } } % BEGIN fillwith_nos: \cs_new_protected:Nn \fillwith_nos: % rhybudd: ARAF ! SLOW! { \color{fillwithcolour} \vcoffin_set:Nnn \l__fillwith_line_coffin { \linewidth } { \vrule height \l__fillwith_line_ht_dim depth \c_zero_skip width \linewidth \fillwith_style: } \coffin_set_eq:NN \l__fillwith_lines_coffin \l__fillwith_line_coffin \int_incr:N \l__fillwith_cnt_int \vcoffin_set:Nnn \l__fillwith_tmpa_coffin { 1.5em} { \int_to_arabic:n { \l__fillwith_cnt_int } } \coffin_attach:NnnNnnnn \l__fillwith_lines_coffin {b} {r} \l__fillwith_tmpa_coffin {b} {l} {2.5pt} {0pt} \dim_set:Nn \l__fillwith_ht_dim { \fp_to_dim:n { \l__fillwith_ht_tl * \baselineskip } } \dim_set:Nn \l__fillwith_rem_dim { \pagegoal -\footskip - \pagetotal - \l__fillwith_ht_dim } \dim_until_do:nNnn { \fillwith_coffin_tht:N \l__fillwith_lines_coffin } > { \l__fillwith_rem_dim - \l__fillwith_ht_dim } { \coffin_join:NnnNnnnn \l__fillwith_lines_coffin { b } { l } \l__fillwith_line_coffin { t } { l } { 0pt } { -\l__fillwith_ht_dim } \int_incr:N \l__fillwith_cnt_int \vcoffin_set:Nnn \l__fillwith_tmpa_coffin {1.5em} { \int_to_arabic:n { \l__fillwith_cnt_int } } \coffin_attach:NnnNnnnn \l__fillwith_lines_coffin {b} {r} \l__fillwith_tmpa_coffin {b} {l} {2.5pt} {0pt} } \coffin_typeset:Nnnnn \l__fillwith_lines_coffin { t } { l } { 0pt } { -\l__fillwith_ht_dim } \normalcolor } % END \fillwith_nos: % END fns \NewDocumentCommand\fillwithset { +m }{ \keys_set:nn { fillwith } { #1 } } % BEGIN \fillwithnolines \NewDocumentCommand \fillwithnolines { O { } } { \group_begin: \keys_set:nn { fillwith } { #1 } \fillwith_nos: \group_end: } % END \fillwithnolines \ExplSyntaxOff \fillwithset{% colour=gray!80, } \begin{document} \parindent=0pt Find the derivative of $x^2$. \fillwithnolines \end{document}
The only two available keys are fillwith ht and colour (or color). The first determines the factor by which the lines are spread (2 by default). The second determines the colour of the rule and numbers (gray by default).
The rules are always 0.4pt high and \linewidth wide, though you could easily make these dimensions configurable, along with an option to fill some dimension other than the remaining vertical space.
Apologies in advance for the crappy image. KDE/Okular has a new bug which renders bitmap images scrappily. For instance, the PDF is not missing rules randomly! Nor are letters and numbers blotchy and blurry!

For the bonus, there are three possibilities: I'm not sure I understand it, if I do understand it, I'm not sure it is possible and, if it is possible, I don't know how.