Example #1
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 Cells text signature options
            CellsSignTextOptions cellsOptions = new CellsSignTextOptions(@"Test signature");

            cellsOptions.SheetNumber  = 1;
            cellsOptions.ColumnNumber = 0;
            cellsOptions.RowNumber    = 0;
            cellsOptions.Width        = 150;
            cellsOptions.Height       = 50;
            cellsOptions.SignAllPages = true;

            SaveOptions saveOptions = new SaveOptions(OutputType.String);

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

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

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

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

            LoadOptions loadOptions = new LoadOptions();

            loadOptions.Password = "******";
            fileName             = handler.Sign <string>(@"testPassword123.xlsx", cellsOptions, loadOptions, saveOptions);
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);

            saveOptions = new SaveOptions(OutputType.Stream);
            using (Stream stream = handler.Sign <Stream>(@"test.xlsx", cellsOptions, saveOptions))
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            saveOptions = new SaveOptions(OutputType.String);
            using (Stream fileStream = File.OpenRead(Path.Combine(storagePath, "test.xlsx")))
            {
                fileName = handler.Sign <string>(fileStream, cellsOptions, saveOptions);
            }
            Console.WriteLine("Document signed successfully. The output filename: {0}", fileName);
        }
Example #2
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);
        }
        protected internal string SignDocumentWithText(string rootPath,
                                                       string fileName,
                                                       string signatureText,
                                                       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
            SignTextOptions 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 PdfSignTextOptions(signatureText);
                break;

            case DocumentViewType.Words:
                signOptions = new WordsSignTextOptions(signatureText);
                break;

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

            case DocumentViewType.Slides:
                signOptions = new SlidesSignTextOptions(signatureText);
                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);
        }
        /// <summary>
        /// Saves the output/signed file with document save options
        /// </summary>
        /// <param name="fileExtension">Extension of the file</param>
        /// <param name="fileName">Name of the file</param>
        /// <param name="handler">Signature's handler</param>
        /// <param name="textSignOptions">Text sign true or false</param>
        /// <param name="imageSignOptions">Image sign true or false</param>
        /// <param name="digitalSignOptions">Digital sign true or false</param>
        public static void SaveFileWithFormat(string fileExtension, string fileName, SignatureHandler handler, object textSignOptions, object imageSignOptions, object digitalSignOptions)
        {
            //ExStart:SaveFileWithFormat
            try
            {
                switch (fileExtension)
                {
                case ".docx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingworddocwithtextandsaveformatoption
                        WordsSignTextOptions wordTextSignOptions = (WordsSignTextOptions)textSignOptions;
                        var wordTextSignedPath = handler.Sign <string>(fileName, wordTextSignOptions, new WordsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.WordsSaveFileFormat.Pdf
                        });
                        //ExEnd:signingworddocwithtextandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingworddocwithimageandsaveformatoption
                        WordsSignImageOptions wordImageSignOptions = (WordsSignImageOptions)imageSignOptions;
                        var wordImageSignedPath = handler.Sign <string>(fileName, wordImageSignOptions, new WordsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.WordsSaveFileFormat.Dot
                        });
                        //ExEnd:signingworddocwithimageandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingworddocwithdigitalcertificatesandsaveformatoption
                        WordsSignDigitalOptions wordDigitalSignOptions = (WordsSignDigitalOptions)digitalSignOptions;
                        var wordDigitalSignedPath = handler.Sign <string>(fileName, wordDigitalSignOptions, new WordsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.WordsSaveFileFormat.Dotm
                        });
                        //ExEnd:signingworddocwithdigitalcertificatesandsaveformatoption
                    }

                    break;

                case ".pdf":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithtextandsaveformatoption
                        PdfSignTextOptions pdfTextSignOptions = (PdfSignTextOptions)textSignOptions;
                        var pdfTextSignedPath = handler.Sign <string>(fileName, pdfTextSignOptions, new PdfSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.PdfSaveFileFormat.Doc
                        });
                        //ExEnd:signingpdfdocwithtextandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithimageandsaveformatoption
                        PdfSignImageOptions pdfImageSignOptions = (PdfSignImageOptions)imageSignOptions;
                        var pdfImageSignedPath = handler.Sign <string>(fileName, pdfImageSignOptions, new PdfSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.PdfSaveFileFormat.Doc
                        });
                        //ExEnd:signingpdfdocwithimageandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithdigitalcertificatesandsaveformatoption
                        PdfSignDigitalOptions pdfDigitalSignOptions = (PdfSignDigitalOptions)digitalSignOptions;
                        var pdfDigitalSignedPath = handler.Sign <string>(fileName, pdfDigitalSignOptions, new PdfSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.PdfSaveFileFormat.Pdf
                        });
                        //ExEnd:signingpdfdocwithdigitalcertificatesandsaveformatoption
                    }

                    break;

                case ".xlsx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingexceldocwithtextandsaveformatoption
                        CellsSignTextOptions cellTextSignOptions = (CellsSignTextOptions)textSignOptions;
                        var cellTextSignedPath = handler.Sign <string>(fileName, cellTextSignOptions, new CellsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.CellsSaveFileFormat.Xlsm
                        });
                        //ExEnd:signingexceldocwithtextandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingexceldocwithimageandsaveformatoption
                        CellsSignImageOptions cellImageSignOptions = (CellsSignImageOptions)imageSignOptions;
                        var cellImageSignedPath = handler.Sign <string>(fileName, cellImageSignOptions, new CellsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.CellsSaveFileFormat.Xlsm
                        });
                        //ExEnd:signingexceldocwithimageandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingexceldocwithdigitalcertificatesandsaveformatoption
                        CellsSignDigitalOptions cellDigitalSignOptions = (CellsSignDigitalOptions)digitalSignOptions;
                        var cellDigitalSignedPath = handler.Sign <string>(fileName, cellDigitalSignOptions, new CellsSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.CellsSaveFileFormat.Xlsm
                        });
                        //ExEnd:signingexceldocwithdigitalcertificatesandsaveformatoption
                    }

                    break;

                case ".pptx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithtextandsaveformatoption
                        SlidesSignTextOptions slildeTextSignOptions = (SlidesSignTextOptions)textSignOptions;
                        var slideTextSignedPath = handler.Sign <string>(fileName, slildeTextSignOptions, new SlidesSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.SlidesSaveFileFormat.Odp
                        });
                        //ExEnd:signingslidesdocwithtextandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithimageandsaveformatoption
                        SlidesSignImageOptions slideImageSignOptions = (SlidesSignImageOptions)imageSignOptions;
                        var slideImageSignedPath = handler.Sign <string>(fileName, slideImageSignOptions, new SlidesSaveOptions {
                            OutputType = OutputType.String, FileFormat = Domain.SlidesSaveFileFormat.Odp
                        });
                        //ExEnd:signingslidesdocwithimageandsaveformatoption
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithdigitalcertificatesandsaveformatoption
                        SlidesSignDigitalOptions slideDigitalSignOptions = (SlidesSignDigitalOptions)digitalSignOptions;
                        var slideDigitalSignedPath = handler.Sign <string>(fileName, slideDigitalSignOptions, new SlidesSaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingslidesdocwithdigitalcertificatesandsaveformatoption
                    }

                    break;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //ExEnd:SaveFileWithFormat
        }
        /// <summary>
        /// Saves the output/signed file
        /// </summary>
        /// <param name="fileExtension">Extension of the file</param>
        /// <param name="fileName">Name of the file</param>
        /// <param name="handler">Signature's handler</param>
        /// <param name="textSignOptions">Text sign true or false</param>
        /// <param name="imageSignOptions">Image sign true or false</param>
        /// <param name="digitalSignOptions">Digital sign true or false</param>
        public static void SaveFile(string fileExtension, string fileName, SignatureHandler handler, object textSignOptions, object imageSignOptions, object digitalSignOptions)
        {
            //ExStart:saveoutputfile
            try
            {
                switch (fileExtension)
                {
                case ".docx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingworddocwithtext
                        WordsSignTextOptions wordTextSignOptions = (WordsSignTextOptions)textSignOptions;
                        var wordTextSignedPath = handler.Sign <string>(fileName, wordTextSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingworddocwithtext
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingworddocwithimage
                        WordsSignImageOptions wordImageSignOptions = (WordsSignImageOptions)imageSignOptions;
                        var wordImageSignedPath = handler.Sign <string>(fileName, wordImageSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingworddocwithimage
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingworddocwithdigitalcertificates
                        WordsSignDigitalOptions wordDigitalSignOptions = (WordsSignDigitalOptions)digitalSignOptions;
                        var wordDigitalSignedPath = handler.Sign <string>(fileName, wordDigitalSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingworddocwithdigitalcertificates
                    }

                    break;

                case ".pdf":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithtext
                        PdfSignTextOptions pdfTextSignOptions = (PdfSignTextOptions)textSignOptions;
                        var pdfTextSignedPath = handler.Sign <string>(fileName, pdfTextSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingpdfdocwithtext
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithimage
                        PdfSignImageOptions pdfImageSignOptions = (PdfSignImageOptions)imageSignOptions;
                        var pdfImageSignedPath = handler.Sign <string>(fileName, pdfImageSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingpdfdocwithimage
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingpdfdocwithdigitalcertificates
                        PdfSignDigitalOptions pdfDigitalSignOptions = (PdfSignDigitalOptions)digitalSignOptions;
                        var pdfDigitalSignedPath = handler.Sign <string>(fileName, pdfDigitalSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingpdfdocwithdigitalcertificates
                    }

                    break;

                case ".xlsx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingexceldocwithtext
                        CellsSignTextOptions cellTextSignOptions = (CellsSignTextOptions)textSignOptions;
                        var cellTextSignedPath = handler.Sign <string>(fileName, cellTextSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingexceldocwithtext
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingexceldocwithimage
                        CellsSignImageOptions cellImageSignOptions = (CellsSignImageOptions)imageSignOptions;
                        var cellImageSignedPath = handler.Sign <string>(fileName, cellImageSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingexceldocwithimage
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingexceldocwithdigitalcertificates
                        CellsSignDigitalOptions cellDigitalSignOptions = (CellsSignDigitalOptions)digitalSignOptions;
                        var cellDigitalSignedPath = handler.Sign <string>(fileName, cellDigitalSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingexceldocwithdigitalcertificates
                    }

                    break;

                case ".pptx":
                    if (textSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithtext
                        SlidesSignTextOptions slildeTextSignOptions = (SlidesSignTextOptions)textSignOptions;
                        var slideTextSignedPath = handler.Sign <string>(fileName, slildeTextSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingslidesdocwithtext
                    }
                    else if (textSignOptions == null && imageSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithimage
                        SlidesSignImageOptions slideImageSignOptions = (SlidesSignImageOptions)imageSignOptions;
                        var slideImageSignedPath = handler.Sign <string>(fileName, slideImageSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingslidesdocwithimage
                    }
                    else if (textSignOptions == null && imageSignOptions == null && digitalSignOptions != null)
                    {
                        //ExStart:signingslidesdocwithdigitalcertificates
                        SlidesSignDigitalOptions slideDigitalSignOptions = (SlidesSignDigitalOptions)digitalSignOptions;
                        var slideDigitalSignedPath = handler.Sign <string>(fileName, slideDigitalSignOptions, new SaveOptions {
                            OutputType = OutputType.String
                        });
                        //ExEnd:signingslidesdocwithdigitalcertificates
                    }

                    break;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //ExEnd:saveoutputfile
        }