Question: A PDF output is obtained by compiling the following code.

```
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset
{
    language={[LaTeX]TeX},
        numbers=left,
        numbersep=1em,
        numberstyle=\tiny,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    morekeywords={maketitle},
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}

```

I attempted to copy the code only inside Acrobat Reader. Unfortunately, the line numbers also got copied as shown on the following screen shot.

![enter image description here](https://i.stack.imgur.com/SyOza.png)

The line numbers are useful, but readers want not to copy them.

Is there a LaTeX trick to prevent a PDF viewer from copying the line number?

Answer: 
This solution is *very* similar to that contained in [How to make text copy in PDF previewers ignore lineno line numbers?](https://tex.stackexchange.com/questions/30783/5764) `\protect`ing the [`accsupp`](http://ctan.org/pkg/accsupp) is the only requirement, perhaps due to the nature in which [`listings`](http://ctan.org/pkg/listings) treats *everything*:

![enter image description here](https://i.stack.imgur.com/lUb2w.png)

```
\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{accsupp}% http://ctan.org/pkg/accsupp
\renewcommand{\thelstnumber}{% Line number printing mechanism
  \protect\BeginAccSupp{ActualText={}}\arabic{lstnumber}\protect\EndAccSupp{}%
}
\lstset
{
    language={[LaTeX]TeX},
        numbers=left,
        numbersep=1em,
        numberstyle=\tiny,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    tabsize=2,
    columns=flexible,
    morekeywords={maketitle},
}

\begin{document}

\begin{lstlisting}
\documentclass{article}
\usepackage{listings}
\title{Sample Document}
\author{John Smith}
\date{\today}
\begin{document}
\maketitle
Hello World!
% This is a comment.
\end{document}
\end{lstlisting}

\end{document}

```
This answer only deals with latex/dvips/ps2pdf.

Package `accsupp` works well with `dvips`. The generated PostScript file contains the right pdfmark operators. However `ghostscript` (version 9.05) has not implemented these tags, from the source file `gdevpdfm.c`:

> 
> 
> ```
> /* [ tag propdict /BDC pdfmark */
> static int
> pdfmark_BDC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
>             const gs_matrix *pctm, const gs_param_string *objname)
> {
>     return 0;                   /****** NOT IMPLEMENTED YET ******/
> }
> 
> /* [ /EMC pdfmark */
> static int
> pdfmark_EMC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
>             const gs_matrix *pctm, const gs_param_string *objname)
> {
>     return 0;                   /****** NOT IMPLEMENTED YET ******/
> }
> 
> ```
> 
> 

Solution sketch:

If the line number is not text, then it cannot be copied. Thus a solution would be to use bitmap images. For each digit and variant (color, size, …) a bitmap needs to be generated. As image format `BMP` can be used, that is supported by `dvips`. Then `\thelstnumber` is redefined to use these images for its digits.