Пример #1
0
        static void Main(string[] args)
        {
            // Create the pdf document
            //PDF4NET v5: PDFDocument doc = new PDFDocument();
            PDFFixedDocument doc = new PDFFixedDocument();

            // Create a blank page
            //PDF4NET v5: PDFPage page = doc.AddPage();
            PDFPage page = doc.Pages.Add();

            // Create the signature field.
            PDFSignatureField sign = new PDFSignatureField("Signature");

            //PDF4NET v5: sign.Widgets[0].DisplayRectangle = new DisplayRectangle(50, 100, 200, 40);
            sign.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(50, 100, 200, 40);
            // Create the digital signature.
            //PDF4NET v5: sign.Signature = new PDFDigitalSignature();
            PDFCmsDigitalSignature ds = new PDFCmsDigitalSignature();

            // The certificate is loaded from disk, but it also can be loaded from a certificate store.
            ds.Certificate = new X509Certificate2("..\\..\\..\\..\\..\\SupportFiles\\O2SolutionsDemoCertificate.pfx", "P@ssw0rd!", X509KeyStorageFlags.Exportable);
            ds.Location    = "Romania";
            ds.Reason      = "For demo";
            ds.ContactInfo = "*****@*****.**";
            sign.Signature = ds;
            page.Fields.Add(sign);

            doc.Save("Sample_DigitalSign.pdf");
        }
Пример #2
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream formStream, X509Certificate2 certificate)
        {
            PDFFixedDocument document = new PDFFixedDocument(formStream);

            PDFSignatureField      signField = document.Form.Fields["signhere"] as PDFSignatureField;
            PDFCmsDigitalSignature signature = new PDFCmsDigitalSignature();

            signature.SignatureDigestAlgorithm = PDFDigitalSignatureDigestAlgorithm.Sha256;
            signature.Certificate = certificate;
            signature.ContactInfo = "*****@*****.**";
            signature.Location    = "Cluj Napoca";
            signature.Name        = "O2 Solutions Support";
            signature.Reason      = "Simple signature";
            signField.Signature   = signature;

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "simplesignature.pdf") };
            return(output);
        }
Пример #3
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream formStream, X509Certificate2 certificate)
        {
            PDFFixedDocument document = new PDFFixedDocument(formStream);

            document.PDFVersion = PDFVersion.Version16;
            PDFSignatureField      signField = document.Form.Fields["signhere"] as PDFSignatureField;
            PDFCmsDigitalSignature signature = new PDFCmsDigitalSignature();

            signature.SignatureDigestAlgorithm = PDFDigitalSignatureDigestAlgorithm.Sha256;
            signature.Certificate = certificate;
            signature.ContactInfo = "*****@*****.**";
            signature.Location    = "Cluj Napoca";
            signature.Name        = "O2 Solutions Support";
            signature.Reason      = "Certifying signature";
            signField.Signature   = signature;
            // Certify the document with the signature included in the field.
            document.CertifyDocument(signField, PDFDigitalSignatureAllowedChanges.AllowFormFilling);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "certifyingsignature.pdf") };
            return(output);
        }
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\SupportFiles\\";

            X509Certificate2 certificate = new X509Certificate2(supportPath + "O2SolutionsDemoCertificate.pfx", "P@ssw0rd!", X509KeyStorageFlags.Exportable);
            FileStream       input       = File.OpenRead(supportPath + "formfill.pdf");
            PDFFixedDocument document    = new PDFFixedDocument(input);

            input.Close();

            // Sign the document
            PDFSignatureField      signField = document.Form.Fields["signhere"] as PDFSignatureField;
            PDFCmsDigitalSignature signature = new PDFCmsDigitalSignature();

            signature.SignatureDigestAlgorithm = PDFDigitalSignatureDigestAlgorithm.Sha256;
            signature.Certificate = certificate;
            signature.ContactInfo = "*****@*****.**";
            signature.Location    = "Cluj Napoca";
            signature.Name        = "O2 Solutions Support";
            signature.Reason      = "Simple signature";
            signField.Signature   = signature;

            // Load the signature image
            FileStream  signatureImageStream = File.OpenRead(supportPath + "signature.png");
            PDFPngImage signatureImage       = new PDFPngImage(signatureImageStream);

            signatureImageStream.Close();
            // Create the signature custom appearance
            PDFAnnotationAppearance aa = new PDFAnnotationAppearance(signField.Widgets[0].VisualRectangle.Width, signField.Widgets[0].VisualRectangle.Height);

            aa.Canvas.DrawImage(signatureImage, 0, 0, aa.Width, aa.Height);
            // Set the custom appearance on the signature field
            signField.Widgets[0].NormalAppearance = aa;

            using (FileStream output = File.Create("DigitalSignatureWithCustomAppearance.pdf"))
            {
                document.Save(output);
            }
        }
Пример #5
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\SupportFiles\\";

            X509Certificate2 certificate = new X509Certificate2(supportPath + "O2SolutionsDemoCertificate.pfx", "P@ssw0rd!", X509KeyStorageFlags.Exportable);
            FileStream       input       = File.OpenRead(supportPath + "formfill.pdf");
            PDFFixedDocument document    = new PDFFixedDocument(input);

            input.Close();

            // Sign the document
            PDFSignatureField      signField = document.Form.Fields["signhere"] as PDFSignatureField;
            PDFCmsDigitalSignature signature = new PDFCmsDigitalSignature();

            signature.SignatureDigestAlgorithm = PDFDigitalSignatureDigestAlgorithm.Sha256;
            signature.Certificate = certificate;
            signature.ContactInfo = "*****@*****.**";
            signature.Location    = "Cluj Napoca";
            signature.Name        = "O2 Solutions Support";
            signature.Reason      = "Simple signature";
            signField.Signature   = signature;

            using (FileStream output = File.Create("MultipleDigitalSignatures.pdf"))
            {
                document.Save(output);
            }

            // For the second signature the document must be opened in incremental update mode
            // by passing the PDFDocumentFeatures object to PDFFixedDocument constructor
            PDFDocumentFeatures df = new PDFDocumentFeatures();

            // Do not load file attachments, new file attachments cannot be added.
            df.EnableDocumentFileAttachments = false;
            // Load form fields to support signing.
            df.EnableDocumentFormFields = true;
            // Do not load embedded JavaScript code, new JavaScript code at document level cannot be added.
            df.EnableDocumentJavaScriptFragments = false;
            // Do not load the named destinations, new named destinations cannot be created.
            df.EnableDocumentNamedDestinations = false;
            // Do not load the document outlines, new outlines cannot be created.
            df.EnableDocumentOutline = false;
            // Do not load annotations, new annotations cannot be added to existing pages.
            df.EnablePageAnnotations = true;
            // Do not load the page graphics, new graphics cannot be added to existing pages.
            df.EnablePageGraphics = false;
            // Incremental update mode requires the changes to be added to the input file
            input    = File.OpenRead("MultipleDigitalSignatures.pdf");
            document = new PDFFixedDocument(input, df);
            input.Close();

            signField = new PDFSignatureField("sign2");
            document.Pages[0].Fields.Add(signField);
            signField.Widgets[0].VisualRectangle = new PDFDisplayRectangle(150, 350, 200, 60);
            signature = new PDFCmsDigitalSignature();
            signature.SignatureDigestAlgorithm = PDFDigitalSignatureDigestAlgorithm.Sha256;
            signature.Certificate = certificate;
            signature.ContactInfo = "*****@*****.**";
            signature.Location    = "Cluj Napoca";
            signature.Name        = "O2 Solutions Support";
            signature.Reason      = "Simple signature";
            signField.Signature   = signature;

            // The same file used as input must also be used as output as the Save method saves only the changes and not the entire file.
            using (FileStream output = File.Open("MultipleDigitalSignatures.pdf", FileMode.Open))
            {
                document.Save(output);
            }
        }