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; } } }
public static string DoEnterPassword(string fileName) { using (PasswordForm f = new PasswordForm()) return(f.EnterPassword(fileName)); }