# Fillable Form with Calculated Total of Non-Blank Lines in Table

folks. I'm trying to create a form that sums up how many names there are in a user-filled table. Essentially, find all the non-blank cells (with a certain variable name), and add 1 to the sum each time, then output that sum to a read-only form-fillable hyperref field. What's the best way to execute this?

I was able to figure out how to do basic summation calculations for integers, but haven't figured out how to sum up non-blank cells yet.

Code below:

\documentclass[letterpaper]{article}

\usepackage{hyperref}
\usepackage[left=0.25in, right=0.25in, bottom=0.5in]{geometry}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{parskip}
\usepackage{eforms}

\definecolor{light-gray}{gray}{0.85}

\newcommand{\no}{\textnumero}

\newcommand{\dimrule}{\color{light-gray}\hrule}

\newcommand{\blank}[2]{
\textField[\BG{0.95 0.95 0.95}\W{0}\Q{1}]{#1}{#2}{1em}
}

\renewcommand{\arraystretch}{1.5}

\pagestyle{fancy}
\fancyhf{}
\cfoot{}

\begin{document}
\begin{Form}

\begin{center}

\textbf{\large{}List}

\bigskip

\begin{tabular}{ll}
& Name\\
1. & \blank{listName1}{30ex}\\
2. & \blank{listName2}{30ex}\\
3. & \blank{listName3}{30ex}\\
4. & \blank{listName4}{30ex}\\
5. & \blank{listName5}{30ex}\\
6. & \blank{listName6}{30ex}\\
7. & \blank{listName7}{30ex}\\
8. & \blank{listName8}{30ex}\\
9. & \blank{listName9}{30ex}\\
10. & \blank{listName10}{30ex}\\
\end{tabular}

\bigskip

\begin{tabular}{ll}
Total Records Printed: & \TextField[
calculate={
%%%%%%%%%%%%%%%%%%%%%%
},
width=20ex,
backgroundcolor={0.95 0.95 0.95},
bordercolor=
]{}\\
\end{tabular}

\end{center}
\end{Form}
\end{document}


The calculation that simply has "%%%%%%%%%%%%%%%%%" is where I have tried and failed with a number of different attempts.

• If it's related to Latex, please Edit your question and post the code. Please keep relevant parts, drop unimportant ones, still showing your point, ready for copy & compile. Jul 19, 2022 at 15:35
• Updated, thanks. Jul 19, 2022 at 16:07
• That's better. // Can you please name or describe briefly your fruitless attempts? Here a code snippet might be fine, as it's in context now, if you have it. Jul 19, 2022 at 16:43

I would suggest that you stick to either the hyperref package or to the eforms package and don't combine them. Since the hyperref package has also other uses, I would recommend using it. In the following, I will give solutions for either choice.

Apart from that, I removed some packages and defintions from your code that are not essential to these examples.

To use the hyperref package, you need to redefine your \blank macro a bit. Note that in order to refer to a \TextField via JavaScript, you need to give it a name. You also need to name the field that does the calculation.

The script that does the calculation loops over the fields named listName1 to listName10 and checks for each whether the value of this field is not empty. If yes, it adds 1 to the variable result which is finally entered into the field named totalRecords.

\documentclass[letterpaper]{article}

\usepackage[left=0.25in, right=0.25in, bottom=0.5in]{geometry}
\usepackage{fontspec}
\usepackage{fancyhdr}

\usepackage{hyperref}
%\usepackage{eforms}

\newcommand{\blank}[2]{
\TextField[backgroundcolor={0.95 0.95 0.95},bordercolor={},width=#2,name=#1]{}
}

\pagestyle{fancy}
\fancyhf{}
\cfoot{}

\begin{document}
\begin{Form}

\begin{center}

\textbf{\large{}List}

\bigskip

\begin{tabular}{ll}
& Name\\
1. & \blank{listName1}{30ex}\\
2. & \blank{listName2}{30ex}\\
3. & \blank{listName3}{30ex}\\
4. & \blank{listName4}{30ex}\\
5. & \blank{listName5}{30ex}\\
6. & \blank{listName6}{30ex}\\
7. & \blank{listName7}{30ex}\\
8. & \blank{listName8}{30ex}\\
9. & \blank{listName9}{30ex}\\
10. & \blank{listName10}{30ex}\\
\end{tabular}

\bigskip

\begin{tabular}{ll}
Total Records Printed: & \TextField[
name=totalRecords,
calculate={
var result = 0;
for (var i = 1; i <= 10; i++) {
if (this.getField('listName' + i).value != '') {
result++;
}
}
event.value = result;
},
width=20ex,
backgroundcolor={0.95 0.95 0.95},
bordercolor={},
default=0
]{}\\
\end{tabular}

\end{center}
\end{Form}
\end{document}


In case you want to stick to the eforms package, you can do as follows (the script that does the calculation is the same as above):

\documentclass[letterpaper]{article}

\usepackage[left=0.25in, right=0.25in, bottom=0.5in]{geometry}
\usepackage{fontspec}
\usepackage{fancyhdr}

%\usepackage{hyperref}
\usepackage{eforms}

\newcommand{\blank}[2]{
\textField[\BG{0.95 0.95 0.95}\W{0}\Q{0}]{#1}{#2}{1em}
}

\pagestyle{fancy}
\fancyhf{}
\cfoot{}

\begin{document}
\begin{Form}

\begin{center}

\textbf{\large{}List}

\bigskip

\begin{tabular}{ll}
& Name\\
1. & \blank{listName1}{30ex}\\
2. & \blank{listName2}{30ex}\\
3. & \blank{listName3}{30ex}\\
4. & \blank{listName4}{30ex}\\
5. & \blank{listName5}{30ex}\\
6. & \blank{listName6}{30ex}\\
7. & \blank{listName7}{30ex}\\
8. & \blank{listName8}{30ex}\\
9. & \blank{listName9}{30ex}\\
10. & \blank{listName10}{30ex}\\
\end{tabular}

\bigskip

\begin{tabular}{ll}
Total Records Printed: & \textField[
\AA{\AACalculate{
var result = 0;
for (var i = 1; i <= 10; i++) {
if (this.getField('listName' + i).value != '') {
result++;
}
}
event.value = result;
}}
]{totalRecords}{20ex}{1em}\\
\end{tabular}

\end{center}
\end{Form}
\end{document}


And finally, there is also a more user friendly way to use the eforms package using the option useui:

\documentclass[letterpaper]{article}

\usepackage[left=0.25in, right=0.25in, bottom=0.5in]{geometry}
\usepackage{fontspec}
\usepackage{fancyhdr}

%\usepackage{hyperref}
\usepackage[useui]{eforms}

\newcommand{\blank}[2]{
\textField[\ui{bgcolor={0.95 0.95 0.95},border={invisible},align={left}}]{#1}{#2}{1em}
}

\pagestyle{fancy}
\fancyhf{}
\cfoot{}

\begin{document}
\begin{Form}

\begin{center}

\textbf{\large{}List}

\bigskip

\begin{tabular}{ll}
& Name\\
1. & \blank{listName1}{30ex}\\
2. & \blank{listName2}{30ex}\\
3. & \blank{listName3}{30ex}\\
4. & \blank{listName4}{30ex}\\
5. & \blank{listName5}{30ex}\\
6. & \blank{listName6}{30ex}\\
7. & \blank{listName7}{30ex}\\
8. & \blank{listName8}{30ex}\\
9. & \blank{listName9}{30ex}\\
10. & \blank{listName10}{30ex}\\
\end{tabular}

\bigskip

\begin{tabular}{ll}
Total Records Printed: & \textField[\ui{
calculate={
var result = 0;
for (var i = 1; i <= 10; i++) {
if (this.getField('listName' + i).value != '') {
result++;
}
}
event.value = result;
}
}]{totalRecords}{20ex}{1em}\\
\end{tabular}

\end{center}
\end{Form}
\end{document}


Either approach will result in the following output:

• This worked out wonderfully, thank you very kindly! Jul 19, 2022 at 18:48
• Do you know how to execute this with checkboxes? I tried applying this to checkboxes as well, but the only result in the total is 10 (with the code snippet unaltered) or 0 (with value == "Yes"). Jul 19, 2022 at 19:29
• The important thing would be that your checkboxes are also named. But then, this.getField('listName' + i).value == 'Yes' (or whatever you named the boxes) should work. In case that this does not work, consider asking a new question on this site. Jul 19, 2022 at 19:42