我构建了一个Windows窗体应用程序,可以打开任何文本文件(甚至是使用iTextSharp Dll的pdf),并在丰富的tex框中查看其内容,在搜索字段中我可以搜索某个模式,所有可能的匹配都要突出显示在“金色。我创建了一个保存按钮。
代码:
private void open_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
tb.Clear();
label1.Text = openFileDialog1.FileName;
if (label1.Text.Contains(".pdf"))
{
// create a reader (constructor overloaded for path to local file or URL)
string location = openFileDialog1.FileName;
PdfReader reader = new PdfReader(location);
StringBuilder text = new StringBuilder();
for (int page = 1; page <= reader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(reader, page, strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
reader.Close();
}
tb.Text = text.ToString();
}
else
{
tb.Text = File.ReadAllText(label1.Text);
}
}
}
private void save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, tb.Text);
}
}
private void search_Click(object sender, EventArgs e)
{
int index = 0;
while (index < tb.Text.LastIndexOf(sb.Text))
{
tb.Find(sb.Text,index,tb.TextLength,RichTextBoxFinds.None);
tb.SelectionBackColor = Color.Gold;
index = tb.Text.IndexOf(sb.Text, index) + 1;
}
}
提前致谢!
答案 0 :(得分:0)
您可以尝试使用它来获取文本以及所有富文本格式代码吗?
string str = richTextBox.Rtf;
有关此背景的更多信息和实施指南,请参阅 http://www.codeproject.com/Articles/12932/Saving-and-Restoring-RichTextBox-Formatted-Text-Al