62

I have a digital certificate that identifies a user. I need to use it to Digitally sign pdf files.

Does anyone have an example that does not uses a third party component? I need to get this done but it would be nice to fully understand how things are done.

C# Examples please :)

starball
  • 20,030
  • 7
  • 43
  • 238
Sergio
  • 8,125
  • 10
  • 46
  • 77
  • Please read the white paper on iText and digital signatures: http://itextpdf.com/book/digitalsignatures – Bruno Lowagie Sep 18 '12 at 07:57
  • 9
    @Will: how is this not constructive, with 29 upvotes and 25 votes for the #1 answer? – Dan Dascalescu Jun 14 '13 at 13:03
  • @DanDascalescu: Because it's a shopping question. We discourage these questions as they attract link rot and spam. Nobody denies that shopping questions are popular, although 30 votes in four years isn't exactly stellar. For more info, please visit [meta]. –  Jun 14 '13 at 13:26
  • 4
    @Will: the vast majority of "howto" questions are "shopping questions". Check the Linked and Related questions to the right - half of them ask for recommendation - [1](http://stackoverflow.com/questions/3563053/how-to-digital-sign-a-pdf-with-php?rq=1), [2](http://stackoverflow.com/questions/5107138/adding-a-digital-signature-to-a-pdf-file?lq=1), [3](http://stackoverflow.com/questions/7469605/signing-pdfs-programmaticaly-in-linux?lq=1) etc.. Closing questions is way too blunt of an instrument to solve the alleged link rot and spam problems. There are better tools: editing answers and downvotes. – Dan Dascalescu Jun 14 '13 at 21:09
  • 4
    @dan honestly, this has been discussed and debated for a long time. If you believe you have a persuasive argument, you can try your luck on [meta]. But the policy has been in effect (contrary examples not yet closed not withstanding), and has worked well for us. Note the lack of spammers filling up SO with their bs. But, if you insist, how about offshore.SO? All the shopping you can shake a question at? –  Jun 14 '13 at 21:42
  • Take a look at [this article on CodeProject](http://www.codeproject.com/Articles/14488/E-signing-PDF-documents-with-iTextSharp) – Corne Aug 19 '09 at 12:08

4 Answers4

44

The open source iTextSharp library will allow you to do this. Here's a post explaining how to digitally sign a pdf file. If you don't want to use a third party library then you can implement it yourself but it could be a tough task -> you can start by reading the pdf specification (8.6MB)

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
11

Proper PDF signing is a very sophisticated task. There exist a number of files that don't conform to the PDF specification (broken xrefs etc) and your code must handle all of them. Then various Acrobat versions treat certain things in signed fields differently. So if you need to do the task (rather than study how it works) you should rely on third-party solution, such as our PDFBlackbox components.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
6

Digitally signing a PDF document without using a third-party component entails a great deal of work and is generally best avoided.

Components do all the hard work for you, so you don't have to. You should find there are some excellent free PDF components available that will suit your needs.

The following example written in C# shows how simple it is to digitally sign a PDF document using ABCpdf:

Doc theDoc = new Doc();
theDoc.Read(Server.MapPath("../Rez/Authorization.pdf"));
Signature theSig = (Signature)theDoc.Form["Signature"];
theSig.Location = "Washington";
theSig.Reason = "Schedule Agreed";
theSig.Sign(Server.MapPath("../Rez/JohnSmith.pfx"), "111111");
theDoc.Save(Server.MapPath("Signed.pdf"));

Source: ABCpdf documentation - Sign method

AffineMesh
  • 1,025
  • 8
  • 14
  • 1
    This is ok if the document already has a signature field but AbcPdf doesnt seem to have anyway to add a signature field. The Fields Collection is readonly. – chrisp_68 Mar 27 '14 at 11:13
  • This is very true; I haven't found anything related to add a form from within ABCPdf – Olivier ROMAND Nov 04 '16 at 11:20
  • Actually, the support just pointed out, that there's an example named Annotations in the installation Folder of ABCPdf demonstrating how to add a signature. – Olivier ROMAND Nov 04 '16 at 15:16
1

Lost my first answer. May want to give DocQ a try to link text They have their own cert and can do this for you for free/cheap to seal and encrypt PDFs. They also have an API you can use.

user337658
  • 11
  • 1
  • I need the users to be able to use their own certificates. Got this working now using iTextSharp – Sergio May 11 '10 at 08:51