[Tex/LaTex] \CheckBox with hyperref: not able to check it

formshyperrefxetex

I'm using this simple example:

\documentclass[hidelinks, english, a4paper, 11pt]{scrartcl}

\usepackage{hyperref}   % typeset hyperlinks
\usepackage[ngerman]{babel}

\begin{document}
\begin{Form}
    \CheckBox[name=asd]{label}
\end{Form}
\end{document}

I understand it relies heavily on the PDF viewer, how those check boxes work, and how they appear.

I can't seem to get this checkbox working. When I view this in Evince, the box just doesn't do anything when clicked. In Chrome, it creates a red square, which I think means it's checked, but I'm unsure. PDF.js (the PDF viewer inside Firefox) does absolutely nothing.

I have yet to check with Adobe Acrobat Reader, but this is getting frustrating already.

What I'm eventually trying to do, is create a form as a PDF, that can be filled out, and then printed, it is not meant to be sent like an online form.

What I need, is the Box to be visible even when unchecked. The checked box should look like this: ⊠ (Unicode symbol U+22A0 "SQUARED TIMES") while the unchecked box should look like an empty box of the same kind.

Since it doesn't work in any of the viewers that are readily available to me, I'm a bit at a loss here. This form should work in as many viewers as possible. Things like \TextField{} works fine.

Please suggest me the best practice, if possible.

I use xelatex on Linux (TeX Live 2014/dev)

Best Answer

I stumbled onto this problem today. For checkboxes, XeTeX 3.14159265-2.6-0.99992 (TeX Live 2015/Debian) produces PDF objects which do not include the so-called "appearance dictionary" (/AP). Clicking the checkbox may actually change its state, but it will not have visible effect. This might actually be a bug in xelatex and/or the hyperref package.

I tried fixing the PDF manually, but no mentally sane person should ever write PDF code by hand. I managed to find a workaround using python3-pdfrw 0.3. It copies the appearance dictionary from a working checkbox (generated by LibreOffice Writer) into the checkbox objects in the PDF produced by xetex:

#!/usr/bin/env python3

import sys
import os

from pdfrw import PdfReader, PdfWriter
from pdfrw.findobjs import find_objects
from pdfrw.objects import PdfName

for annot in find_objects(
  PdfReader("libreform.pdf").pages, 
  valid_types=(PdfName.XObject,PdfName.Annot,), 
  valid_subtypes=(PdfName.Widget,)
):
  if (annot.FT == '/Btn'):
    AP = annot.AP

outfn = sys.argv[2]
inpfn = sys.argv[1]
pdf = PdfReader(inpfn)
pages = pdf.pages
for annot in find_objects(
  pages, 
  valid_types=(PdfName.XObject, None), 
  valid_subtypes=(PdfName.Widget,)
):
  if (annot.FT == '/Btn'):
    annot.AP = AP
    annot.Type = "/Annot"

writer = PdfWriter(outfn, trailer=pdf)
writer.write()

The code and example files can be found at https://ssl.hehoe.de/zeug/pdflatexCheckboxFix .

Related Question