//Static Constructor
        static SAPIWrapper()
        {
            try
            {
                SAPI = new SAPICryptClass();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("SAPI is not installed");
                System.Windows.Forms.Application.Exit();
            }

            int rc = SAPI.Init();
            if (rc != 0)
            {
                System.Windows.Forms.MessageBox.Show("Failed to initialize SAPI (" + rc.ToString("X") + ")");
                System.Windows.Forms.Application.Exit();
            }

            rc = SAPI.HandleAcquire(out hSession);
            if (rc != 0)
            {
                System.Windows.Forms.MessageBox.Show("Failed in SAPIHandleAcquire (" + rc.ToString("X") + ")");
                System.Windows.Forms.Application.Exit();
            }

            isLoggedIn = false;
        }
        //Static Constructor
        static SAPIWrapper()
        {
            try
            {
                SAPI = new SAPICryptClass();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("SAPI is not installed");
                System.Windows.Forms.Application.Exit();
            }

            int rc = SAPI.Init();

            if (rc != 0)
            {
                System.Windows.Forms.MessageBox.Show("Failed to initialize SAPI (" + rc.ToString("X") + ")");
                System.Windows.Forms.Application.Exit();
            }

            rc = SAPI.HandleAcquire(out hSession);
            if (rc != 0)
            {
                System.Windows.Forms.MessageBox.Show("Failed in SAPIHandleAcquire (" + rc.ToString("X") + ")");
                System.Windows.Forms.Application.Exit();
            }

            isLoggedIn = false;
        }
Beispiel #3
0
        //SAPIInit() should be called in the static constructor in order to make sure
        //that it's called only once, when the class is being initialized.
        static FileSign()
        {
            SAPICrypt SAPI = new SAPICrypt();
            int       rc   = SAPI.Init(); //SAPIInit() should be called once per process

            if (rc != 0)
            {
                throw new Exception("Failed to initialize SAPI! (" + rc.ToString("X") + ")");
            }
        }
Beispiel #4
0
        //Thread-Safe SAPIInit()
        private static void SAPIInit()
        {
            SAPICrypt SAPI = new SAPICrypt();

            lock (_SyncRoot)
            {
                //Do nothing if SAPI is initialized already
                if (isSAPIInitialized(SAPI))
                {
                    return;
                }

                //Initialize SAPI
                int rc = SAPI.Init();
                if (rc != 0)
                {
                    throw new Exception(string.Format(
                                            "Failed to load SAPI library (#{0})", rc.ToString("X")));
                }
            }
        }
        static void Main(string[] args)
        {
            const int SAPI_OK = 0;

            int       rc;
            SAPICrypt SAPI      = new SAPICrypt();
            SESHandle sesHandle = null;

            // Custom Values
            string filePath       = @"c:\temp\demo.pdf";                                                       // PDF file to sign
            string username       = "******";                                                       // DSA account username
            string password       = "******";                                                       // DSA account password
            string domain         = null;                                                                      // DSA account domain
            int    sigPageNum     = 1;                                                                         // Create signature on the first page
            int    sigX           = 145;                                                                       // Signature field X location
            int    sigY           = 125;                                                                       // Signature field Y location
            int    sigWidth       = 160;                                                                       // Signature field width
            int    sigHeight      = 45;                                                                        // Signature field height
            string timeFormat     = "hh:mm:ss";                                                                // Time appearance format mask
            string dateFormat     = "dd/MM/yyyy";                                                              // Date appearance format mask
            int    appearanceMask = (int)SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_GRAPHICAL_IMAGE | // Elements to display on the signature field
                                    (int)SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_SIGNED_BY |
                                    (int)SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_TIME;

            try
            {
                // Initialize SAPI library
                if ((rc = SAPI.Init()) != SAPI_OK)
                {
                    throw new Exception(string.Format("Failed to initialize SAPI ({0})", rc.ToString("X")));
                }

                // Acquire SAPI session handle
                if ((rc = SAPI.HandleAcquire(out sesHandle)) != SAPI_OK)
                {
                    throw new Exception(string.Format("Failed in SAPIHandleAcquire() ({0})", rc.ToString("X")));
                }

                // Personalize SAPI Session
                if ((rc = SAPI.Logon(sesHandle, username, domain, password)) != SAPI_OK)
                {
                    throw new Exception(string.Format("Failed to authenticate user ({0})", rc.ToString("X")));
                }

                SigFieldSettings SFS = new SigFieldSettings();
                TimeFormat       TF  = new TimeFormat();

                // Define signature field settings
                SFS.Page           = sigPageNum;
                SFS.X              = sigX;
                SFS.Y              = sigY;
                SFS.Width          = sigWidth;
                SFS.Height         = sigHeight;
                SFS.AppearanceMask = appearanceMask;
                SFS.SignatureType  = SAPI_ENUM_SIGNATURE_TYPE.SAPI_ENUM_SIGNATURE_DIGITAL;
                SFS.DependencyMode = SAPI_ENUM_DEPENDENCY_MODE.SAPI_ENUM_DEPENDENCY_MODE_INDEPENDENT;
                TF.DateFormat      = dateFormat;
                TF.TimeFormat      = timeFormat;
                TF.ExtTimeFormat   = SAPI_ENUM_EXTENDED_TIME_FORMAT.SAPI_ENUM_EXTENDED_TIME_FORMAT_GMT;  // Display GMT offset
                SFS.TimeFormat     = TF;

                // Create and sign a new signature field in the document
                if (SAPI_OK != (rc = SAPI.SignatureFieldCreateSign(
                                    sesHandle,                                // Session Handle
                                    SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_ADOBE, // Type of the file to sign - PDF
                                    filePath,                                 // Path to PDF file to sign
                                    SFS,                                      // Signature Field details
                                    0,                                        // Flags
                                    null)))                                   // Credentials (only if prompt-for-sign feature is enabled)
                {
                    throw new Exception(string.Format("Failed in SAPISignatureFieldCreateSign() ({0})", rc.ToString("X")));
                }

                Console.WriteLine("The document has been successfully signed!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sesHandle != null)
                {
                    SAPI.Logoff(sesHandle);         // Release user context
                    SAPI.HandleRelease(sesHandle);  // Release session handle
                }
            }
        }
        static void Main(string[] args)
        {
            string           InFileName  = "C:\\temp\\tag-sample.pdf";
            string           OutFileName = "C:\\temp\\tag-sample-signed.pdf";
            string           startDelim  = "<<";
            string           endDelim    = ">>";
            int              rc;
            SESHandle        SesHandle;
            SigFieldSettings SFS   = new SigFieldSettings();
            TimeFormat       TF    = new TimeFormat();
            int              Flags = AR_PDF_FLAG_FIELD_NAME_SET;
            int              LocNumber;

            SAPICrypt SAPI = new SAPICrypt();

            // SAPIInit() should be called once per process
            if ((rc = SAPI.Init()) != 0)
            {
                throw new Exception("Failed to initialize SAPI! (" + rc.ToString("X") + ")");
            }

            // Open a new SAPI context
            if ((rc = SAPI.HandleAcquire(out SesHandle)) != 0)
            {
                throw new Exception("Failed in SAPIHandleAcquire() with rc = " + rc.ToString("X"));
            }

            if ((rc = SAPI.Logon(SesHandle, "{DSA User}", null, "{DSA User password}")) != 0)
            {
                SAPI.HandleRelease(SesHandle);
                throw new Exception("Failed in SAPILogon() with rc = " + rc.ToString("X"));
            }

            //
            // Locate all field tags in the PDF
            // The PDF filename to be used is defined above.
            // The format of each field locator in the PDF should be:
            //   << w={width}; h={height}; n={field name}; a={appearance mask} >>
            //
            // For example:
            //  << w=120; h=80; n=Signer1; a=15; >>
            //
            SAPIContext SigLocatorCtx = new SAPIContext();

            Array         fileBytes = File.ReadAllBytes(InFileName);
            SAPIByteArray doc       = new SAPIByteArray();

            doc.FromArray(ref fileBytes);
            FileHandle fh;

            if ((rc = SAPI.CreateFileHandleByMem(out fh, SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_ADOBE, 0, doc)) != 0)
            {
                SAPI.HandleRelease(SesHandle);
                throw new Exception("Failed to create file handle with rc = " + rc.ToString("X"));
            }

            //  Initiate a new Field Locator enumerator.
            //  The argument LocNumber will contain the number of signature tags found in the PDF document.
            //
            if ((rc = SAPI.SignatureFieldLocatorEnumInit(SesHandle, SigLocatorCtx, fh, startDelim, endDelim, 0, out LocNumber)) != 0)
            {
                SAPI.HandleRelease(fh);
                SAPI.HandleRelease(SesHandle);
                throw new Exception("LocatorEnumInit failed with rc = " + rc.ToString("X"));
            }

            // Do for all tags in the document
            string strEncMsg;

            for (int i = 0; i < LocNumber; i++)
            {
                // Get settings for the next field locator.
                // (Use strEncMsg string to parse the field locator content if a custom string format has been used)
                if ((rc = SAPI.SignatureFieldLocatorEnumCont(SesHandle, SigLocatorCtx, SFS, out strEncMsg)) != 0)
                {
                    SAPI.ContextRelease(SigLocatorCtx);
                    SAPI.HandleRelease(fh);
                    SAPI.HandleRelease(SesHandle);
                    throw new Exception("LocatorEnumCont failed with rc = " + rc.ToString("X"));
                }

                // The following values of the SignatureFieldSettings will be automatically set by SAPI:
                //   X/Y Location (including page number)
                //   Width
                //   Height
                //   Field Name
                //   Appearance Mask
                //
                SFS.LabelsMask     = 0;
                SFS.DependencyMode = SAPI_ENUM_DEPENDENCY_MODE.SAPI_ENUM_DEPENDENCY_MODE_INDEPENDENT;
                SFS.SignatureType  = SAPI_ENUM_SIGNATURE_TYPE.SAPI_ENUM_SIGNATURE_DIGITAL;
                SFS.Flags          = 0;

                // time:
                TF.DateFormat    = "dd MMM yyyy";
                TF.TimeFormat    = "hh:mm:ss";
                TF.ExtTimeFormat = SAPI_ENUM_EXTENDED_TIME_FORMAT.SAPI_ENUM_EXTENDED_TIME_FORMAT_NONE;
                SFS.TimeFormat   = TF;

                //  Now create the signature field on the PDF
                //  We make use of SignatureFieldCreateEx function in order to pass the FileHandle for the in-memory
                //  file rather than a UNC file path.
                if ((rc = SAPI.SignatureFieldCreateSignEx2(SesHandle, SAPI_ENUM_FILE_TYPE.SAPI_ENUM_FILE_ADOBE,
                                                           "", fh, SFS, Flags, null)) != 0)
                {
                    SAPI.ContextRelease(SigLocatorCtx);
                    SAPI.HandleRelease(fh);
                    SAPI.HandleRelease(SesHandle);
                    throw new Exception("Failed to create new signature field with rc = " + rc.ToString("X"));
                }
            }

            //  Write the PDF to an output file
            if ((rc = SAPI.GetFileMemData(fh, 0, doc)) != 0)
            {
                SAPI.ContextRelease(SigLocatorCtx);
                SAPI.HandleRelease(fh);
                SAPI.HandleRelease(SesHandle);
                throw new Exception("Failed to GetFileMem with rc = " + rc.ToString("X"));
            }

            File.WriteAllBytes(OutFileName, (byte[])doc.ToArray());

            //  Finalize work with SAPI
            SAPI.ContextRelease(SigLocatorCtx);
            SAPI.HandleRelease(fh);
            SAPI.HandleRelease(SesHandle);
        }