-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
76 lines (64 loc) · 2.51 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#region #Reference
using DevExpress.Pdf;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.IO;
using System.Text;
using System.Windows.Forms;
// ...
#endregion #Reference
namespace WindowsFormsApplication1 {
public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm {
public MainForm() {
InitializeComponent();
}
private void MainForm_Load(object sender, System.EventArgs e) {
pdfViewer1.LoadDocument(@"..\..\TextExtraction.pdf");
repositoryItemButtonEdit3.ButtonClick += repositoryItemButtonEdit3_ButtonClick;
}
#region #ExtractText
string ExtractTextFromPDF(string filePath) {
string documentText = "";
try {
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
documentProcessor.LoadDocument(filePath);
documentText = documentProcessor.Text;
}
}
catch { }
return documentText;
}
#endregion #ExtractText
#region #WordCount
private int WordCount(string filePath, string word) {
int count = 0;
try {
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
documentProcessor.LoadDocument(filePath);
while (documentProcessor.FindText(word).Status == PdfTextSearchStatus.Found) {
count++;
}
}
}
catch { }
return count;
}
#endregion #WordCount
private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e) {
string path = pdfViewer1.DocumentFilePath;
TextViewerForm textViewer = new TextViewerForm();
textViewer.textBox1.Text = ExtractTextFromPDF(path);
textViewer.ShowDialog();
}
void repositoryItemButtonEdit3_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
string path = pdfViewer1.DocumentFilePath;
string word = (sender as ButtonEdit).Text;
StringBuilder message = new StringBuilder();
message.Append("The number of times the word \"");
message.Append(word);
message.Append("\" appears in the PDF document: ");
message.Append(WordCount(path, word).ToString());
MessageBox.Show(message.ToString());
}
}
}