14

I have the following:

  • routine X that creates a PDF file on a daily basis.
  • routine Y that attaches this file to an Outlook e-mail and sends it to recipients.

Both the above are in VBA. They are called from a C# console application.

Once the PDF has been created I need to password protect it. To do this via VBA without purchasing third party software is quite involved.

What is the simplest solution using C#?

(I'm suspecting there will be an inverse relationship between amount we spend and complexity of answer!)

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • If you only need to password protect your pdf file then use http://itextpdf.com/ library. For more info goto this: http://stackoverflow.com/questions/370571/password-protected-pdf-using-c-sharp –  Sep 12 '12 at 07:56
  • 4
    @DarshanJoshi: iText is not free for commercial use. – Daniel Hilgarth Sep 12 '12 at 07:58
  • @DanielHilgarth two people have voted to close this question - I've tried to be very specific; are there any possible edits I can make to make it more acceptable? – whytheq Sep 12 '12 at 08:17
  • 1
    @whytheq: I was one of the persons voting to close the question. My reason was that there wasn't a concrete problem. You asked for general guidance. I never did PDF editing - my answer is the result of five minutes of googling. You could have done this yourself. – Daniel Hilgarth Sep 12 '12 at 08:25
  • @DanielHilgarth I think a lot of people use SO for a first port of call so to keep this question open isn't a bad thing as your answer is good and concise i.e Google can be avoided. Although I suspect I'll have more code specific questions when implementing your answer so the code will still end up on here. Thanks for the help. – whytheq Sep 12 '12 at 08:40
  • @whytheq: No need to avoid google - it can be used to find answers faster than using SO. – Daniel Hilgarth Sep 12 '12 at 08:43
  • @DanielHilgarth I use Google all the time; I also get the feeling SO is turning into a massive stand alone resource. – whytheq Sep 12 '12 at 08:45
  • @whytheq: Indeed, it is. A lot of my google searches end up here :-) – Daniel Hilgarth Sep 12 '12 at 08:51
  • @DanielHilgarth Q.E.D ...pass go on Google and come straight to SO – whytheq Sep 12 '12 at 16:59

2 Answers2

29

PDFSharp should be able to protect a PDF file with a password:

// Open an existing document. Providing an unrequired password is ignored.
PdfDocument document = PdfReader.Open(filename, "some text");

PdfSecuritySettings securitySettings = document.SecuritySettings;

// Setting one of the passwords automatically sets the security level to 
// PdfDocumentSecurityLevel.Encrypted128Bit.
securitySettings.UserPassword  = "user";
securitySettings.OwnerPassword = "owner";

// Don't use 40 bit encryption unless needed for compatibility reasons
//securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;

// Restrict some rights.
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = false;

// Save the document...
document.Save(filename);

Reference:
http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 2
    this looks good & relatively simple - is it totally open source? i.e even for commercial use it is free? – whytheq Sep 12 '12 at 08:15
  • 5
    @whytheq: [Yes](http://www.pdfsharp.net/Licensing.ashx), it is free to use even in commercial products. – Daniel Hilgarth Sep 12 '12 at 08:16
  • had a play this afternoon - excellent; I've imported all the source code folders...even had a little nose through the source code! – whytheq Sep 12 '12 at 16:59
  • @whytheq: Cool :-) I want to point out that it would have been enough to reference the compiled assembly from your code. – Daniel Hilgarth Sep 12 '12 at 17:03
  • ok added this subsequent [SO question](http://stackoverflow.com/questions/12391244/pdfsharp-getting-started) and the PdfSharp team answered; they seem to favour importing all the folders. I think they mentioned that it adds intellisense. For me, as a c# newbie, i enjoyed adding the projects as it gave me a sense of what is actually being used when I put `using x` at the top of a code module; a reference seems a little more black-box. – whytheq Sep 20 '12 at 11:16
  • @whytheq: You can have intellisense without adding the project. Simply place the pdfsharp.dll.xml file beside the pdfsharp.dll. – Daniel Hilgarth Sep 20 '12 at 11:25
  • thanks - not sure of the reason then why the pdfsharp team suggest importing the projects. When I add a reference using VS what does it do? - just physically move a copy of the `.dll` file into the project/solution? ...I could effectively just use import existsing item and go and find the dll and bring it into the project; would be equivalent to using the references? Or is adding a reference just a pointer to somewhere else on my machine? – whytheq Sep 20 '12 at 13:57
  • @whytheq: Maybe you should ask that as a new question. It seems like you are missing some basics here :-) Others are better at explaining basics than I am... – Daniel Hilgarth Sep 20 '12 at 14:30
  • hmmm - I am still learning the basics. basic questions can sometimes be hounded out of SO - I might be better Googling. thanks for all the help on this question. – whytheq Sep 20 '12 at 14:43
  • @DanielHilgarth can this document be opened in PdfSharp after password protection is applied..? if not, how we are gonna load this document again..? – shashwat May 13 '13 at 13:07
  • @harsh: It should be supported. The sample I posted already hints at it: *// Open an existing document. **Providing an unrequired password is ignored**.* – Daniel Hilgarth May 13 '13 at 13:08
  • @DanielHilgarth- So is there a way we can make password a required thing to open the document..? – shashwat May 13 '13 at 13:12
  • 1
    @harsh: Yes, sure. Not clear what you are asking here or what your problem is. My answer shows how to password protect a PDF file. The second parameter to `PdfReader.Open` is the passwort needed to open a password protected PDF file. If you have any further questions, please post them as such if they haven't been answered already: http://stackoverflow.com/questions/ask – Daniel Hilgarth May 13 '13 at 13:14
2

For .NET Core/.NET 5 and up only


PdfSharp works only on old .NET Framework.
PdfSharpCore is the same library just ported to the new .NET (.NET Core).

The above answer by @Daniel Hilgarth still works without any change to the code with the ported library.

NOTE This library is under MIT LICENSE, so nothing to worry about.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131