Exemple #1
0
        /// <summary>
        /// Authenticates the specified document.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        public static bool Authenticate(DecoderBase decoder)
        {
            // if authentication is required
            if (decoder.IsAuthenticationRequired)
            {
                // get the document filename
                string filename = null;
                if (decoder.SourceStream != null && decoder.SourceStream is FileStream)
                {
                    filename = ((FileStream)decoder.SourceStream).Name;
                }

                while (true)
                {
                    // create the document password dialog
                    using (DocumentPasswordForm enterPasswordDialog = new DocumentPasswordForm())
                    {
                        enterPasswordDialog.Filename = filename;

                        // show the document password dialog
                        DialogResult showDialogResult = enterPasswordDialog.ShowDialog();

                        // if "OK" button is clicked
                        if (showDialogResult == DialogResult.OK)
                        {
                            DocumentAuthenticationRequest authenticationRequest = new DocumentAuthenticationRequest(
                                enterPasswordDialog.authenticateTypeComboBox.Text,
                                enterPasswordDialog.Password);

                            // uuthenticate the decoder
                            DocumentAuthorizationResult result = decoder.Authenticate(authenticationRequest);
                            // if user is NOT authorized
                            if (!result.IsAuthorized)
                            {
                                MessageBox.Show(
                                    string.Format("The {0} password is incorrect.", enterPasswordDialog.authenticateTypeComboBox.Text),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            // if user is authorized
                            else
                            {
                                if (enterPasswordDialog.authenticateTypeComboBox.Text != result.UserName)
                                {
                                    MessageBox.Show(
                                        string.Format("Authorized as: {0}", result.UserName),
                                        "Authorization Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                break;
                            }
                        }
                        // if "Cancel" button is clicked
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
        /// <summary>
        /// Opens a form that allows to select image of image collection
        /// and returns image collection and selected index.
        /// </summary>
        /// <param name="filename">The filename of image file.</param>
        /// <param name="selectedIndex">Selected index.</param>
        /// <returns>Image collection.</returns>
        public static ImageCollection SelectImageFromFile(string filename, out int selectedIndex)
        {
            selectedIndex = -1;

            // create image collection
            ImageCollection images = new ImageCollection();

            DocumentPasswordForm.EnableAuthentication(images);
            try
            {
                try
                {
                    // add an image file to the image collection
                    images.Add(filename);
                }
                catch (Exception e)
                {
                    DemosTools.ShowErrorMessage(e);
                    return(null);
                }

                selectedIndex = 0;


                // if image file contains more than 1 image
                if (images.Count > 1)
                {
                    // create a dialog that allows to select image from multipage image file
                    using (SelectImageForm selectImageForm =
                               new SelectImageForm(images))
                    {
                        // show the dialog
                        DialogResult result = selectImageForm.ShowDialog();
                        // if image is selected
                        if (result == DialogResult.OK)
                        {
                            // get the selected index
                            selectedIndex = selectImageForm.SelectedImageIndex;
                        }
                        else
                        {
                            selectedIndex = -1;
                        }
                    }
                }
            }
            finally
            {
                DocumentPasswordForm.DisableAuthentication(images);

                // if image is not selected
                if (selectedIndex == -1)
                {
                    // clear and dispose images from the image collection
                    images.ClearAndDisposeItems();

                    // dispose image collection
                    images.Dispose();
                }
            }
            return(images);
        }