开发者_开发问答How can I make a PDF non-printable programmatically using .net?
Short answer. You can't. You can try to set a DRM parameter to prevent printing, but that all depends on the client's software for rendering the PDF and if it respects DRM or no.
Also, assuming you are able to view a PDF there is nothing preventing the user from taking screen captures of the contents presented in the PDF.
Sure you can prevent printing! Of course you cannot prevent a screen capture, but you can definitely prevent a casual user from printing a PDF in the Adobe Reader. I know in our ActiveReports product when we export PDF you can specify various security options that determine whether the user viewing the PDF can print or not. The developer can specify an admin password that can be used to enable only some users to print, while preventing those without the password from printing.
You can read more about this in the PdfSecurity enumeration documentation of ActiveReports. A code sample is in the documentation here. Follow some links on that page to see more information.
If you want to load an existing PDF and modify usually people suggest iTextSharp. Although I have not used it, other people recommend it highly and I think it will work for this read+modify scenario. I managed to find an example of how you can use iTextSharp to enable/disable the "AllowPrint" and other permissions here.
Sounds like you're really looking for a group policy object in Active Directory that prevents users from printing, perhaps?
update
What prevents said users from emailing the PDF to themselves and printing at home, other than another GPO that prevents attachments with given extensions or over certain sizes?
You've got a number of options depending on the full requirement details but the easiest would indeed be to set password encryption on the document. As some of the folks above mentioned, you can set an owner password and user password to encrypt the document. The owner password basically allows admin level access and fully opens up the document to all operations. The user password opens up the document but access is limited to what you specified at the time of encryption, e.g., if you turn turn off printing rights, then someone who supplies the user password won't be able to print the PDF.
We offer a solution that supports .NET and can easily encrypt and also decrypt PDFs. Here's a link to the API docs if you're interested in giving it a look:
http://www.pdfonline.com/easypdf/epsdk_manual/index.htm?page=reference%2Fpdfprocessorsdk%2Fpdfprocessor%2Fm_encrypt.htm
The idea would be to call the Encrypt() function above and pass a PrintingPerm argument of PRC_SEC_PRINT_PERM_NONE. You can also set other access permissions like modification, copy/paste, etc., if you like.
Hope this helps.
精彩评论