When I include code snippets in my documents, my lazier readers, i.e. students
:), are often tempted to just copy and paste those snippets instead of typing them themselves. However, there are risks associated with that practice.
Copying and pasting of listings is the subject of numerous questions on the site; at the time of writing this question, the search
copy paste [listings] is:question returns at least ten questions relevant to that problem. The answers to those questions show that the problem remains, by and large, unsolved.
There are workaround to ensure that white space doesn't disappear in the process, but none work across all PDF viewers.
Even worse: you run the risk of picking up nasty spurrious invisible characters that you really don't want; see Why does compiling this .dtx produce a blank page at the beginning? for an example.
Therefore, instead of letting my readers run the risk of saving an imperfect copy of one of my listings to their "clipboard", I'd like to preclude them from copying anything from my listings. Niet. Nada. Nichts. Rien du tout! Because I still want to make the source code available to them, though, I plan to embed it in the PDF instead.
How can I preclude my readers from copying my listings?
My question is related to Mangling listings source code. However, I cannot afford to export the listings as raster images only to import them back as graphics is not an option, because I want to keep the listings in vectorised format.
Also, I do not want to encrypt the whole PDF as in Is it possible to produce a PDF with un-copyable text?. I simply want to forbid copying of my listings.
I don't care that OCR can be used to recover my listings. I suspect most of my users wouldn't know how to use OCR or wouldn't do it just to avoid having to type a listing themselves.