Example #1
0
        private static PdfDocument Load(IWin32Window owner, Stream stream, string password)
        {
            try
            {
                while (true)
                {
                    try
                    {
                        return(new PdfDocument(stream, password));
                    }
                    catch (PdfException ex)
                    {
                        if (owner != null && ex.Error == PdfError.PasswordProtected)
                        {
                            using (var form = new PasswordForm())
                            {
                                if (form.ShowDialog(owner) == DialogResult.OK)
                                {
                                    password = form.Password;
                                    continue;
                                }
                            }
                        }

                        throw;
                    }
                }
            }
            catch
            {
                stream.Dispose();
                throw;
            }
        }
        private void Open_Click(object sender, EventArgs e)
        {
            if (ofdOpen.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            while (true)
            {
                try
                {
                    c1PdfDocumentSource1.LoadFromFile(ofdOpen.FileName);
                    return;
                }
                catch (PdfPasswordException)
                {
                    string password = PasswordForm.DoEnterPassword(ofdOpen.FileName);
                    if (password == null)
                    {
                        return;
                    }
                    c1PdfDocumentSource1.Credential.Password = password;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
Example #3
0
 public static string DoEnterPassword(string fileName)
 {
     using (PasswordForm f = new PasswordForm())
         return(f.EnterPassword(fileName));
 }