public static string DoEnterPassword(string fileName) { using (PasswordForm f = new PasswordForm()) return(f.EnterPassword(fileName)); }
/// <summary> /// Start the text search asynchronously. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFind_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbFile.Text)) { MessageBox.Show(this, "Please select a PDF file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!File.Exists(tbFile.Text)) { MessageBox.Show(this, string.Format("File \"{0}\" does not exist.", tbFile.Text), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.Compare(_loadedFile, tbFile.Text, true) != 0) { // Load the specified PDF file into c1PdfDocumentSource1. // The loop allows the user to specify a password for an encrypted file. while (true) { try { c1PdfDocumentSource1.LoadFromFile(tbFile.Text); _loadedFile = tbFile.Text; break; } catch (PdfPasswordException) { string password = PasswordForm.DoEnterPassword(tbFile.Text); if (password == null) { return; } c1PdfDocumentSource1.Credential.Password = password; } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } // Clear the previously found positions, if any, and start the search. // Note the use of the FindStartAsync version of the FindStart method (a synchronous version is also available): lvFoundPositions.Items.Clear(); C1FindTextParams ftp = new C1FindTextParams(tbFindText.Text, cbxWholeWord.Checked, cbxCaseSensitive.Checked); if (rbWholeDocument.Checked) { _textSearchManager.FindStartAsync(0, SearchScope.WholeDocument, ftp, false); } else { int pageNum = (int)nudPageIndex.Value; if (pageNum < 1 || pageNum > c1PdfDocumentSource1.PageCount) { MessageBox.Show(string.Format("Page should be from 0 to {0}.", c1PdfDocumentSource1.PageCount), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { _textSearchManager.FindStartAsync(pageNum - 1, SearchScope.SinglePage, ftp, false); } } // Update controls and show the progress bar: UpdateControls(); pbFind.Value = 0; pbFind.Visible = true; }