Registrant name of itextsharp for PDF

Source: Internet
Author: User
Tags pk12 pfx file

 

 

Open your browser, and click "Tools" ----> Internet Options ......
Switch to the "content" property page and click "certificate"
Select a certificate from the certificate list, and click "Export"
Follow the wizard prompts and select the certificate extraction option that contains the private key. When prompted, enter the password.
Now you can useCodeFollow these steps:
1 Compilation and execution example
2. Select the PDF document to be signed
3. Select the location of the target document
4. Add or modify document attributes if necessary
5. Select the certificate (. pfx file) You just extracted)
6. Enter the password used to extract the certificate.
7. Add the signature information (such as the reason, contact information, and address) if necessary)
8. Click "sign ".

In the tracking window, you can see the operation process. If everything goes well, open the target file you specified and the document has been signed.

How is all this achieved?

In the Code provided in this articleA library called signer. It is a tool that uses itextsharp.Package to implement everything you need for digital signature.
It includes three classes:
Cert Class: this class is used to encapsulate the certificate and extract the information required for the signature. The most important method in this class is processcert ()
Metadata class: Metadata encapsulation class
Registrant signer class: this class is constructed and requires a certificate object and a metadata object. The most important method is the sign method.

Processcert () method:

Private void processcert ()
{
String alias = NULL;
Pkcs12store PK12;

// First we'll read the Certificate file
PK12 = new pkcs12store (New filestream (this. Path, filemode. Open, fileaccess. Read), this. Password. tochararray ());

// then iterate throught certificate entries to find the private key entry
ienumerator I = pk12.aliases ();
while (I. movenext ()
{< br> alias = (string) I. current);
If (pk12.iskeyentry (alias)
break;
}

This. AKP = pk12.getkey (alias). getkey ();
X509certificateentry [] Ce = pk12.getcertificatechain (alias );
This. Chain = new org. bouncycastle. x509.x509certificate [Ce. Length];
For (int K = 0; k <Ce. length; ++ K)
Chain [k] = Ce [K]. getcertificate ();

}

This method retrieves the certificate, traverses all its elements, and extracts the private key. If possible, it also creates a certificate chain.

Sign () method:

Public void sign (string sigreason, string sigcontact, string siglocation, bool visible)
{
Pdfreader reader = new pdfreader (this. inputpdf );
// Activate multisignatures
Optional Stamper ST = pdfstamper. createsignature (reader, new filestream (this. outputpdf, filemode. Create, fileaccess. Write), '\ 0', null, true );
// To disable multi signatures uncomment this line: every new signature will invalidate older ones!
// Export Stamper ST = pdfstamper. createsignature (reader, new filestream (this. outputpdf, filemode. Create, fileaccess. Write), '\ 0 ');

St. moreinfo = This. Metadata. getmetadata ();
St. xmpmetadata = This. Metadata. getstreamedmetadata ();
Repeated signatureappearance sap = ST. signatureappearance;

Sap. setcrypto (this. mycert. AKP, this. mycert. Chain, null, pdfsignatureappearance. wincer_signed );
Sap. Reason = sigreason;
Sap. Contact = sigcontact;
Sap. Location = siglocation;
If (visible)
Sap. setvisiblesignature (New itextsharp. Text. rectangle (100,100,250,150), 1, null );

St. Close ();
}

This function retrieves the content of the source PDF document, and then uses the extracted data to create a new pdf file through pdfstamper.
Pdfstamper is a PDF reader that can sign PDF documents. The appearance of the signature can be customized, so you can add attributes such as the reason, contact information, and address for the signature.
The setcrypto method allows us to use the private key and chain Certificate Signature document extracted from the certificate file.
Finally, you can use the setvisiblesignature method to add a visual appearance.
Pdfreader, pdfstamper, and pdfsignatureappearance are provided by the itextsharp library.

 

 

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.