How to make listings code correct copyable from PDF and with hyperlink

If I generate listings code with line numbers and copy the result from the PDF I can not use it again as tex code. First the line numbers are included which they should not, second spaces are included which should not appear.

For example with this code:

\documentclass{scrbook}

\usepackage{listings}

\lstdefinestyle{demostyle}{
basicstyle=\small\ttfamily,
numbers=left,
stepnumber=1,
frame=single,
}

\begin{document}
\begin{lstlisting}[style=demostyle]
\textbf{\textit{foobar}}
\textit{\sffamily foobar}}
\end{lstlisting}
\end{document}


you get this result:

1    \ textbf {\ textit { foobar }}
2    \ textit {\ sffamily   foobar }}


Here I found a solution in How to make listings code indentation remain unchanged when copied from pdf?.

\lstset{columns=flexible}
\lstset{keepspaces=true}
\makeatletter
\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
\makeatother


However it is not clear to me how reader-safe the solution is. It works for me in PDF-XChange. However, the line numbers are still copyable. Can this be solved as well?

Besides this problem - is it possible to add a link to the PDF which copies the code to the clipboard? The code should not be in a file, it shall be part of the PDF but nevertheless linked with a hyperlink as a file to download.

EDIT (embedfile does not what I expect)

\documentclass{scrbook}
\usepackage{embedfile}
\usepackage{filecontents}

\begin{filecontents*}{democode}
\so{letterspacing}, \\
\ul{underlining},   \\
\st{overstriking}   \\
and \hl{highlighting}.
\end{filecontents*}

\begin{document}
Any Text.

\embedfile[]{democode.tex}
\embedfilesetup{view}
\end{document}


Basically the code compiles but nothing further is visible. From the doc I do not understand how this package shall be used anyway. Especially I do not know how to link or view the embedded files.

EDIT2 (attachfile)

I also tested the package attachfile. It links to a file, but the link is not text, but an annotation which is almost invisible. Tested with:

\textattachfile{democode.tex}{source code}

-
Did you know you can have an attachment to a PDF embedded using LaTeX. The embedfile package (or attachfile/attachfile2) allows for this. – Werner Oct 2 '11 at 23:05
I expanded my question with non solving example code for embedfile – Matthias Pospiech Oct 3 '11 at 7:02
When I last had the line number problem, I changed the format to // 127 and put them to the end of the line. (If you can't figure out how to win, simply change the game. ;) ) – Ulrich Schwarz Oct 3 '11 at 7:21
Related question: Attach listing to PDF – diabonas Feb 10 '12 at 11:53

You can mark the line numbers so that they are not copied, i.e. an empty text is copied instead, using the technique described in Is it possible to provide alternative text to use when copying text from the PDF?. The required code can be added using the numberstyle option if listings, as shown below. If you want to add other formatting macros to the numbers either add it before the \noncopynumber macro or inside of it.

The solution for the spaces you found in How to make listings code indentation remain unchanged when copied from pdf? should be PDF viewer independent. I don't see any reason why different PDF viewers should display it differently. Once the spaces are frozen to the PDF the presentation should be identical with all viewers. That's the basic idea behind the PDF standard.

Copying the text to the clipboard might be possible using some JavaScript but would not be that trivial to add. I would attach the listings also as files to the PDF and provide a link to that instead (as already suggested by others in the comments).

\documentclass{scrbook}
\usepackage{xcolor}
\usepackage{accsupp}

\usepackage{listings}

\lstdefinestyle{demostyle}{
basicstyle=\small\ttfamily,
numbers=left,
stepnumber=1,
frame=single,
columns=flexible,
keepspaces=true,
numberstyle=\noncopynumber
}

\newcommand{\noncopynumber}[1]{%
\BeginAccSupp{method=escape,ActualText={}}%
#1%
\EndAccSupp{}%
}

\makeatletter
\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
\makeatother

\begin{document}
\begin{lstlisting}[style=demostyle]
\textbf{\textit{foobar}}
\textit{\sffamily foobar}}
\end{lstlisting}
\end{document}


Using this I get the following when copying the text:

\textbf{\textit{foobar}}
\textit{\sffamily foobar}}


For longer text, e.g. multiple pages long, you will have issues with the footer and header. These could be made un-copyable using the same technique. Attaching the listings as files would be a good alternative here.

-
If I use this code, the line numbers are marked as well and copied as well. Therefore I do not see any changes. However with the space changes now the spaces are copied as questionmarks: \textit{\sffamily?foobar}}. This does not happen in my minimal example, only in my huge template code. It will take some time to find the reason. If this is already known I would like to get to know it. – Matthias Pospiech Oct 3 '11 at 8:53
@Martin: When I copy the output using the PDF viewer that comes with TeXShop, I get \textbf{\textit{foobar}} \textit{\sffamily␣foobar}} (missing the <CR>), but with Acrobat, I do get the newline character. – Peter Grill Oct 3 '11 at 17:40
A similar solution was given at ctt (groups.google.com/group/comp.text.tex/browse_frm/thread/…) – Martin Heller Oct 4 '11 at 16:12
I have added your solution to my repository. Thanks for it! I just noticed, that intendation seems not to work. Is it possible to fix it? – Martin Thoma Nov 18 '12 at 13:11