示例#1
0
        public void GetSigner()
        {
            String[] testFiles =
            {
                "hyperlink-example-signed.docx",
                "hello-world-signed.docx",
                "hello-world-signed.pptx",
                "hello-world-signed.xlsx",
                "hello-world-office-2010-technical-preview.docx",
                "ms-office-2010-signed.docx",
                "ms-office-2010-signed.pptx",
                "ms-office-2010-signed.xlsx",
                "Office2010-SP1-XAdES-X-L.docx",
                "signed.docx",
            };

            foreach (String testFile in testFiles)
            {
                OPCPackage pkg = OPCPackage.Open(testdata.GetFileInfo(testFile), PackageAccess.READ);
                try {
                    SignatureConfig sic = new SignatureConfig();
                    sic.SetOpcPackage(pkg);
                    SignatureInfo si = new SignatureInfo();
                    si.SetSignatureConfig(sic);
                    List <X509Certificate> result = new List <X509Certificate>();
                    foreach (SignatureInfo.SignaturePart sp in si.GetSignatureParts())
                    {
                        if (sp.Validate())
                        {
                            result.Add(sp.GetSigner());
                        }
                    }

                    Assert.IsNotNull(result);
                    Assert.AreEqual(1, result.Count, "test-file: " + testFile);
                    X509Certificate signer = result[0];
                    //LOG.Log(POILogger.DEBUG, "signer: " + signer.SubjectX500Principal);

                    bool b = si.VerifySignature();
                    Assert.IsTrue(b, "test-file: " + testFile);
                    pkg.Revert();
                } finally {
                    pkg.Close();
                }
            }
        }
示例#2
0
        internal static SignatureConfig GetPaths()
        {
            string          rootPath         = Path.GetFullPath(@"..\..\");
            string          storagePath      = Path.Combine(rootPath, @"Storage");
            string          outputPath       = Path.Combine(rootPath, @"Output");
            string          imagesPath       = Path.Combine(rootPath, @"Images");
            string          certificatesPath = Path.Combine(rootPath, @"Certificates");
            SignatureConfig signatureConfig  = new SignatureConfig
            {
                StoragePath      = storagePath,
                OutputPath       = outputPath,
                ImagesPath       = imagesPath,
                CertificatesPath = certificatesPath
            };

            return(signatureConfig);
        }
示例#3
0
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // setup a configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            // setup Cells image signature options
            var cellsImageSignatureOptions = new CellsSignImageOptions(@"Autograph_of_Benjamin_Franklin.png");

            // set coordinates of image
            cellsImageSignatureOptions.Left   = 10;
            cellsImageSignatureOptions.Top    = 10;
            cellsImageSignatureOptions.Width  = 100;
            cellsImageSignatureOptions.Height = 100;

            // set document page number
            cellsImageSignatureOptions.SheetNumber  = 1;
            cellsImageSignatureOptions.ColumnNumber = 0;
            cellsImageSignatureOptions.RowNumber    = 5;
            cellsImageSignatureOptions.SignAllPages = false;

            SaveOptions saveOptions = new SaveOptions(OutputType.String);

            // sign the document
            string fileName = handler.Sign <string>(@"test.xlsx", cellsImageSignatureOptions, saveOptions);

            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
        }
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            PdfSignDigitalOptions pdfDigitalOptions = new PdfSignDigitalOptions(Path.Combine(storagePath, @"a.pfx"));

            // set coordinates of image
            pdfDigitalOptions.Left   = 10;
            pdfDigitalOptions.Top    = 10;
            pdfDigitalOptions.Width  = 100;
            pdfDigitalOptions.Height = 100;

            pdfDigitalOptions.ImageFileName = "Autograph_of_Benjamin_Franklin.png";

            // set document page number
            pdfDigitalOptions.DocumentPageNumber = 1;

            // set document password if required
            pdfDigitalOptions.Password = "******";

            // set up configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };
            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            SaveOptions saveOptions = new SaveOptions(OutputType.String);

            // sign document
            string fileName = handler.Sign <string>(@"candy.pdf", pdfDigitalOptions, saveOptions);

            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
        }
        /// <summary>
        /// Get source document from URI
        /// </summary>
        public static void GetSrcDocFromUri()
        {
            // setup Signature configuration
            var signConfig = new SignatureConfig
            {
                OutputPath = @"c:\Test\Output"
            };
            // instantiating the signature handler without Signature Config object
            var handler = new SignatureHandler(signConfig);
            // setup image signature options
            var signOptions = new PdfSignImageOptions(@"http://groupdocs.com/images/banner/carousel2/conversion.png");
            // save options
            var saveOptions = new SaveOptions(OutputType.String);
            // sign document with image
            var signedPath = handler.Sign <string>("https://www.adobe.com/content/dam/Adobe/en/feature-details/acrobatpro/pdfs/combine-multiple-documents-into-one-pdf-file.pdf", signOptions, saveOptions);

            Console.WriteLine("Signed file path is: " + signedPath);
        }
        public static void CellsDocument()
        {
            // set up Signature configuration
            SignatureConfig signConfig = Helper.GetPaths();

            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // setup digital signature options
            var signOptions = new CellsSignDigitalOptions("test.pfx");

            signOptions.Password = "******";

            // sign document
            var signedPath = handler.Sign <string>("test.xls", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
示例#7
0
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // setup a configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // setup PDF image signature options
            PdfSignImageOptions pdfImageSignatureOptions = new PdfSignImageOptions(@"Autograph_of_Benjamin_Franklin.png");

            pdfImageSignatureOptions.DocumentPageNumber = 1;
            pdfImageSignatureOptions.Top    = 500;
            pdfImageSignatureOptions.Width  = 50;
            pdfImageSignatureOptions.Height = 20;

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            SaveOptions saveOptions = new SaveOptions(OutputType.String);
            // sign the document
            LoadOptions loadOptions = new LoadOptions();
            string      fileName    = handler.Sign <string>(@"candy.pdf", pdfImageSignatureOptions, saveOptions);

            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            loadOptions.Password = "******";
            fileName             = handler.Sign <string>(@"candyPassword123.pdf", pdfImageSignatureOptions, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
        }
示例#8
0
        public static void PDFDocument()
        {
            // setup Signature configuration
            SignatureConfig signConfig = Helper.GetPaths();

            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // setup text signature options
            var signOptions = new PdfSignTextOptions("John Smith");

            // text position
            signOptions.Left = 100;
            signOptions.Top  = 100;
            signOptions.DocumentPageNumber = 1;
            // sign document
            var signedPath = handler.Sign <string>("test.pdf", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
示例#9
0
        public static void CellsDocument()
        {
            // set up Signature configuration
            SignatureConfig signConfig = Helper.GetPaths();
            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // set text signature options
            var signOptions = new CellsSignTextOptions("John Smith");

            // text position
            signOptions.RowNumber          = 10;
            signOptions.ColumnNumber       = 10;
            signOptions.SignAllPages       = true;
            signOptions.DocumentPageNumber = 1;
            // sign document
            var signedPath = handler.Sign <string>("test.xls", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
        /// <summary>
        /// Get source document from Stream
        /// </summary>
        public static void GetSrcDocFromStream()
        {
            // setup Signature configuration
            var signConfig = new SignatureConfig
            {
                OutputPath = @"c:\Test\Output"
            };
            // instantiating the signature handler without Signature Config object
            var handler = new SignatureHandler(signConfig);
            // setup image signature options
            var signOptions = new PdfSignImageOptions(@"http://groupdocs.com/images/banner/carousel2/conversion.png");
            // save options
            var saveOptions = new SaveOptions(OutputType.String);

            using (var fileStream = new FileStream(@"C:\test.pdf", FileMode.Open, FileAccess.Read))
            {
                // sign document with image
                var signedPath = handler.Sign <string>(fileStream, signOptions, saveOptions);
                Console.WriteLine("Signed file path is: " + signedPath);
            }
        }
示例#11
0
        public static void WordsDocument()
        {
            // setup Signature configuration
            SignatureConfig signConfig = Helper.GetPaths();

            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // setup image signature options with relative path - image file stores in config.ImagesPath folder
            var signOptions = new WordsSignImageOptions("signature.jpg");

            signOptions.Left               = 10;
            signOptions.Top                = 10;
            signOptions.Width              = 100;
            signOptions.Height             = 100;
            signOptions.DocumentPageNumber = 1;
            // sign document
            var signedPath = handler.Sign <string>("test.docx", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
        public static void SlidesDocument()
        {
            // set up Signature configuration
            SignatureConfig signConfig = Helper.GetPaths();

            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // setup digital signature options
            var signOptions = new SlidesSignDigitalOptions("test.pfx", "signature.jpg");

            signOptions.Left               = 10;
            signOptions.Top                = 10;
            signOptions.Width              = 100;
            signOptions.Height             = 100;
            signOptions.DocumentPageNumber = 1;
            signOptions.Password           = "******";
            // sign document
            var signedPath = handler.Sign <string>("test.ppt", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
        /// <summary>
        /// Get source document from relative path
        /// </summary>
        public static void GetSrcDocFromRelaticePath()
        {
            var storagePath = @"c:\Test\Storage";
            var outputPath  = @"c:\Test\Output";
            var imagesPath  = @"c:\Test\Images";
            // setup Signature configuration
            var signConfig = new SignatureConfig
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };
            // instantiating the conversion handler
            var handler = new SignatureHandler(signConfig);
            // setup image signature options with relative path - image file stores in config.ImagesPath folder
            var signOptions = new PdfSignImageOptions("signature.jpg");
            // sign document
            var signedPath = handler.Sign <string>("test.pdf", signOptions, new SaveOptions {
                OutputType = OutputType.String
            });

            Console.WriteLine("Signed file path is: " + signedPath);
        }
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // setup a configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            // set up PDF image signature options
            PdfSignTextOptions signOptions = new PdfSignTextOptions(@"Test signature");

            signOptions.DocumentPageNumber = 1;
            signOptions.Left         = 100;
            signOptions.Top          = 0;
            signOptions.SignAllPages = true;

            SaveOptions saveOptions = new SaveOptions(OutputType.String);
            // sign the document
            string fileName = handler.Sign <string>(@"candy.pdf", signOptions, saveOptions);

            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
        }
示例#15
0
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // setup a storage configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // setup Words text signature options
            var options = new WordsSignTextOptions();

            // set the signature text, font family name and other properties
            options.Text   = "Test signature";
            options.Left   = 50;
            options.Top    = 300;
            options.Width  = 100;
            options.Height = 50;

            // put the signature on all the pages
            options.SignAllPages = true;

            SaveOptions saveOptions = new SaveOptions();

            saveOptions.OutputType = OutputType.String;

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            // sign the documents
            string fileName;
            Stream resultStream;

            using (Stream fileStream = File.OpenRead(Path.Combine(storagePath, "test.docx")))
            {
                fileName = handler.Sign <string>(fileStream, options, saveOptions);
            }
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            using (Stream fileStream = File.OpenRead(Path.Combine(storagePath, "test.docx")))
            {
                saveOptions.OutputType = OutputType.Stream;
                resultStream           = handler.Sign <Stream>(fileStream, options, saveOptions);
                resultStream.Seek(0, SeekOrigin.Begin);
            }

            LoadOptions loadOptions = new LoadOptions();

            loadOptions.Password   = "******";
            saveOptions.OutputType = OutputType.String;
            fileName = handler.Sign <string>(Path.Combine(storagePath, @"test.docx"), options, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            fileName = handler.Sign <string>(@"test.docx", options, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            fileName = handler.Sign <string>(@"test1PagePassword123.docx", options, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            saveOptions            = new SaveOptions(OutputType.Stream);
            saveOptions.OutputType = OutputType.Stream;
            using (Stream stream = handler.Sign <Stream>(@"test.docx", options, loadOptions, saveOptions))
            {
                stream.Seek(0, SeekOrigin.Begin);
            }
        }
        /// <summary>
        /// Agregar o inserta la imagen de la firma en el documento especificado.
        /// </summary>
        /// <param name="documentConfig">Es la configuración del documento.</param>
        /// <param name="signatureConfig">Es la configuración de la firma.</param>
        /// <returns>Devuelve un entero positivo o negativo que indica si el proceso se completo o no.</returns>
        public int AddSignatureInDocument(DocumentConfig documentConfig, SignatureConfig signatureConfig)
        {
            int output = 0;

            try
            {
                if (!documentConfig.FullPathTemplate.FileExist())
                {
                    output = (int)TypesEvent.FileNotFound;
                    return(output);
                }

                Document document = new Document(documentConfig.FullPathTemplate);

                var fieldSignatures = DocumentProcessor.FindFieldByKeyWord(document, signatureConfig.FieldSignatureName);

                if (fieldSignatures == null)
                {
                    output = (int)TypesEvent.FieldNotFound;
                    return(output);
                }

                string fieldSignature = DocumentProcessor.GetFieldNameInMergedField(fieldSignatures.DisplayResult);
                document.InsertImageInDocument(signatureConfig.FullPathImage, fieldSignature, signatureConfig.Width, signatureConfig.Height);

                if (!string.IsNullOrEmpty(signatureConfig.FieldFooterName))
                {
                    var footerField = DocumentProcessor.FindFormFieldByKeyWord(document, signatureConfig.FieldFooterName);

                    if (footerField == null)
                    {
                        output = (int)TypesEvent.FieldNotFound;
                        return(output);
                    }

                    footerField.Result = signatureConfig.FieldFooterValue;
                }

                return(document.SaveDocument(documentConfig.FullPathDirectory, documentConfig.FileName, ".docx"));
            }
            catch (UnsupportedFileFormatException)
            {
                output = (int)TypesEvent.UnsupportedFileFormat;
            }
            catch (FileCorruptedException)
            {
                output = (int)TypesEvent.FileCorrupted;
            }
            catch (IOException ioex)
            {
                var message = ioex.Message.ToLower();

                if (message.Contains("porque está siendo utilizado en otro proceso"))
                {
                    output = (int)TypesEvent.DocumentInOtherProccess;
                }
                else
                {
                    output = (int)TypesEvent.ErrorGeneric;
                }
            }
            catch (Exception ex)
            {
                output = (int)TypesEvent.ErrorGeneric;
            }

            return(output);
        }
示例#17
0
 public void SetSignatureConfig(SignatureConfig signatureConfig)
 {
     this.signatureConfig = signatureConfig;
 }
        protected internal string SignDocumentWithImage(string rootPath,
                                                        string fileName,
                                                        Stream imageStream,
                                                        int pageNumber,
                                                        int left,
                                                        int top,
                                                        int width,
                                                        int height,
                                                        int signatureColumnNum, int signatureRowNum)
        {
            string storagePath = rootPath;
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // set up a configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // Set a license if you have one
            _licensing.ApplyLicense();

            // setup PDF image signature options
            SignImageOptions signOptions       = null;
            string           fileNameExtension = Path.GetExtension(fileName).TrimStart('.');

            fileNameExtension = fileNameExtension.ToLower();
            int pageWidth = 0, pageHeight = 0;
            DocumentViewType fileType = GetDocumentType(fileNameExtension);

            switch (fileType)
            {
            case DocumentViewType.Pdf:
                signOptions = new PdfSignImageOptions(imageStream);
                break;

            case DocumentViewType.Words:
                signOptions = new WordsSignImageOptions(imageStream);
                break;

            case DocumentViewType.Cells:
                signOptions = new CellsSignImageOptions(imageStream)
                {
                    ColumnNumber = signatureColumnNum,
                    RowNumber    = signatureRowNum
                };
                break;

            case DocumentViewType.Slides:
                signOptions = new SlidesSignImageOptions(imageStream);
                break;
            }
            signOptions.DocumentPageNumber = pageNumber;
            signOptions.Left         = left;
            signOptions.Top          = top;
            signOptions.Width        = width;
            signOptions.Height       = height;
            signOptions.SignAllPages = false;

            GroupDocs.Signature.Options.SaveOptions saveOptions = new GroupDocs.Signature.Options.SaveOptions(OutputType.String);
            // sign the document
            string outputFilePath = handler.Sign <string>(fileName, signOptions, saveOptions);

            return(outputFilePath);
        }
        static void Main(string[] args)
        {
            string rootPath    = Path.GetFullPath(@"..\..\");
            string storagePath = Path.Combine(rootPath, @"Storage");
            string outputPath  = Path.Combine(rootPath, @"Output");
            string imagesPath  = Path.Combine(rootPath, @"Images");

            // setup a configuration
            SignatureConfig config = new SignatureConfig()
            {
                StoragePath = storagePath,
                OutputPath  = outputPath,
                ImagesPath  = imagesPath
            };

            // instantiating the handler
            SignatureHandler handler = new SignatureHandler(config);

            // setup Words image signature options
            SlidesSignTextOptions options = new SlidesSignTextOptions(@"Test signature");

            // setup coordinates of image
            options.Left   = 500;
            options.Top    = 100;
            options.Width  = 100;
            options.Height = 50;

            // setup document page number
            options.DocumentPageNumber = 0;
            options.SignAllPages       = true;

            // Set a license if you have one
            License license = new License();

            license.SetLicense(@"GroupDocs.Signature3.lic");

            SaveOptions saveOptions = new SaveOptions(OutputType.String);
            string      fileName;

            using (Stream fileStream = File.OpenRead(Path.Combine(storagePath, "GroupDocs_Demo_2_pages.pptx")))
            {
                fileName = handler.Sign <string>(fileStream, options, saveOptions);
            }
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            // sign the document
            fileName = handler.Sign <string>(@"GroupDocs_Demo_2_pages.pptx", options, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            LoadOptions loadOptions = new LoadOptions();

            fileName = handler.Sign <string>(@"GroupDocs_Demo_2_pages.pptx", options, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            loadOptions.Password = "******";
            fileName             = handler.Sign <string>(@"GroupDocs_Demo_2_pagesPassword123.pptx", options, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
            saveOptions = new SaveOptions(OutputType.Stream);
            using (Stream stream = handler.Sign <Stream>(@"GroupDocs_Demo_2_pages.pptx", options, loadOptions, saveOptions))
            {
                stream.Seek(0, SeekOrigin.Begin);
            }
        }