C # How to sign a PDF file,

Source: Internet
Author: User

C # How to sign a PDF file,

If you can sign a pdf file and search for it online, iText is free of charge, but AGPL is free of charge. Of course, it also has a commercial agreement, iText is now available in version 5 and 7. Here I use version 5.

Then, in order to make more comparisons, I also found Spire. PDF for testing. Its Free version is Free. spire. the PDF version is still version 3.2, and the paid version is currently version 4.2. Here we will talk about the test result directly. The free version can be used for digital signature, but the image signature will not work, as long as the image is assigned to the corresponding parameter, the conversion exception will be reported during saving, and the code version 4.2 needs to be slightly adjusted compared with the code version 3.2 (only recommended in nature, can also run without adjustment ), it can be saved successfully. In addition, I also found the PDF-Writer for the paid version. Because the example itself contains the Image Signature section, we will not describe it here. Here are the iText and Spire codes:

Using iTextSharp.text.pdf. security; using Org. bouncyCastle. pkcs; // This is built-in with itext. You do not need to add the BouncyCastle package using System separately. drawing; public class implements digstsign {static string pfxPath = "001180530510644.pfx"; static string pfxPwd =" 111111 "; static string sourcePdf =" template.pdf "; static string imgPath =" password_1.png "; public static void initialize signwithitext5demo () {string outPutPdf = "ImageStampWithIText5.pdf"; var reader = new iTextSharp.text.pdf. pdfReader (sourcePdf); using (FileStream fs = new FileStream (outPutPdf, FileMode. create) {var stamper = iTextSharp.text.pdf. optional Stamper. createSignature (reader, fs, '\ 0', null, true); var appearance = stamper. signatureAppearance; appearance. reason = "signature test Reason"; appearance. location = "signature Location"; appearance. setVisibleSignature (new iTextSharp. text. rectangle (200,200,300,300), 1, "itext"); // itext automatically scales the image proportionally to appearance. signatureGraphic = iTextSharp. text. image. getInstance (imgPath); appearance. certificationLevel = itextsharp.textdetail. pdfSignatureAppearance. CERTIFIED_NO_CHANGES_ALLOWED; appearance. signatureRenderingMode = itextsharp.text=. pdfSignatureAppearance. renderingMode. GRAPHIC; using (var stream = File. openRead (pfxPath) {Pkcs12Store store = new Pkcs12Store (stream, pfxPwd. toCharArray (); string keyAlias = null; foreach (string alias in store. aliases) {keyAlias = alias; break;} var chains = store. getCertificateChain (keyAlias ). select (c => c. certificate ). toList (); var signature = new PrivateKeySignature (store. getKey (keyAlias ). key, DigestAlgorithms. SHA1); MakeSignature. signDetached (appearance, signature, chains, null, 0, CryptoStandard. CADES) ;}} public static void require signwithspirepdf4_2demo () {// The commercial version is required. var doc = new Spire. pdf. invalid document (); doc. loadFromFile (sourcePdf); var page = doc. pages [0]; var cert = new Spire. pdf. security. pdfCertificate (pfxPath, pfxPwd); var signature = new Spire. pdf. security. revoke signature (doc, page, cert, "spire"); // var loApprearance = signature. appearence; var image = Spire. pdf. graphics. invalid image. fromFile (imgPath); var template = new Spire. pdf. graphics. template (image. width, image. height); template. graphics. drawImage (image, 0, 0,100,100); // spire is specified by the code to scale loApprearance. normal = template; signature. bounds = new RectangleF (new PointF (100,100), new SizeF (100,100); signature. reason = "signature Reason"; signature. locationInfo = "signature location"; signature. graphicsMode = Spire. pdf. security. graphicMode. signNameOnly; signature. documentPermissions = Spire. pdf. security. pdfcertifflflags. forbidChanges; string output = "ImageStampWithSpirePDF4_2.pdf"; doc. saveToFile (output );}}

After the above Code is run, the signature panel of the two is slightly different, as shown below:

Although the generated pdf cannot be modified, it still feels more reliable to generate iText ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.