Prevent PDF printing

I need to prevent a PDF file from printing (see this tutorial for instance)
How do I achieve this (programmatically) using TPdfWriter?

I am also interested in "locking" PDFs with a password.

Sorry, it isn't currently possible to protect or lock pdfs generated by pdfwriter. It is on our todo list, but with low priority because honestly, it is kind of useless. A 10 sec  google search will show you how to print them anyway, and how to "unlock" them too.

We do provide a way to sign pdf documents to show it was you who create them, because that has a sound mathematical foundation and you can't really modify the document without invalidating the signature. But it is actually impossible to create a file which Acrobat reader can read, but it can't print or modify. If the file can be read, then it can be copied/printed, etc. Yes, the file can have something inside that says "don't print me" but it is trivial to remove that something, and there are many already made apps which will do just that in seconds. In an era before internet it might make sense to try to keep that hidden, only those with a lot of technical knowledge would be able to find and remove the bit that says "do not print me". Today, basic google skills will get you there very fast even if you don't know anything about the pdf format.

Now this all being said, as mentioned at the start, it is indeed in our todo list. We understand people want this "protection" anyway. (and that is why we support protection in Excel documents, even if it is as useless). But just not with hight priority, since we have a lot of other stuff also in our plate and some of that stuff it more useful.

I suspected that PDF password were not secure at all, in fact I didn't need them for security purposes.

In my application I have an "Export to PDF" feature and a "print" button. I just need to "force" the user to click the print button if he wants to print, instead of export the page to PDF and then print, which (depending on what PDF viewer you use and what print driver you have) can result in all kind of formatting mess.

I know that my request may seem silly but, you know, users are strange creatures.

I can understand your use case, and I understand there are other legitimate reasons for wanting a file "locked" even if it isn't really anything secure. That is why adding protection to pdf is in the todo list.

It is just that the pdf spec makes it quite complex to "lock" a file, probably as a way to "obfuscate" better the stuff, and you need to calculate a checksum of all the document so it isn't as trivial to just remove the record which says "I am protected. When you open me in acrobat don't print me". You now need to remove the record and resign the file, which is still just obfuscation, but takes a little more time to the guy doing the protection removal program.

In our case, as pdfwriter is "write as you go", that is it writes the stuff directly to the file as the file is being created, without holding it in memory, it gets more difficult to calculate the bytes for the signature. It is possible of course, but it also has to be done in a way that doesn't make it slower when you are exporting normal pdfs, which is the most common case.

I will investigate during this weekend on the complexity and let you know if it can be done.