Use TextField from a Form as URL of href (or PushButton) in hyperref

871 Views Asked by At

So I just can't seem to figure this out. In a Latex "Form" environment, imagine having a textfield like the following:

\TextField[default=URL_Here,bordercolor=,name=link1,width=8cm,charsize=10pt]{}

Now, if you want to access this elsewhere in the same Form block, you can do so with:

this.getField("link1").value

But, how can you then convert that to a hyperlink elsewhere? The end goal is to have it so that when a URL is input in one textfield, a clickable link appears in an adjacent field. So far, the following methods all fail:

\href{this.getField("link1").value}{Link}

and

\PushButton[
    onclick={
        this.submitForm(this.getField("link1").value);
    },
    name=hlink1,
    readonly=false, bordercolor=, width=2cm, charsize=10pt
]{Link}

The first approach simply crashes on pdflatex compilation, and the latter succeeds (only if https:// is specified in the url), but fails as it tries to access some arbitrary url (copied below with my username changed to USERNAME for anonymity):

file:///C:/Users/USERNAME/AppData/Local/Temp/acrord32_sbx/A9Rcopupe_dxvlog_9x4.htm

A minimal test case of the above submit button (that fails) is as follows:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\begin{Form}
\begin{tabular}{c | c }
\TextField[default=https://www.google.com,bordercolor=,name=link1,width=8cm,charsize=10pt]{} & 
\PushButton[
        onclick={
            this.submitForm(this.getField("link1").value);
        },
        name=hlink1,
        readonly=false, bordercolor=, width=2cm, charsize=10pt
    ]{Link}
\end{tabular}
\end{Form}

\end{document}
1

There are 1 best solutions below

0
escalator On

Use app.launchURL() in your MWE (instead of submitForm())

\begin{Form}
\begin{tabular}{c | c }
\TextField[default=https://www.google.com,bordercolor=,name=link1,width=8cm,charsize=10pt]{} & 
\PushButton[
        onclick={
            app.launchURL(this.getField("link1").value);
        },
        name=hlink1,
        readonly=false, bordercolor=, width=2cm, charsize=10pt
    ]{Link}
\end{tabular}
\end{Form}

submitForm() has a different function:

submitForm

Submits the form to a specified URL. To call this method, you must be running inside a web browser or have the Acrobat Web Capture plug-in installed. (If the URL uses the “mailto” scheme, it will be honored even if not running inside a web browser, as long as the SendMail plug-in is present.) Beginning with Adobe Reader 6.0, you need not be inside a web browser to call this method.

-- Page 345