0

我构建了一个 Windows 窗体应用程序,它可以打开任何文本文件(甚至是使用 iTextSharp Dll 的 pdf 文件)并在富 tex 框中查看其内容,这是一个搜索字段,我可以在其中搜索特定模式,所有可能的匹配项都会在“Gold”中突出显示颜色。我创建了一个保存按钮。

  1. 如何通过保留文本格式用突出显示的文本覆盖文本文件(.doc)?
  2. 我怎样才能用pdf做同样的步骤?(因为覆盖文件后pdf会崩溃)

编码:

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;
    }
}

提前致谢!

4

1 回答 1

0

您能否尝试使用它来获取文本以及所有富文本格式代码?

string str = richTextBox.Rtf;

有关此上下文的更多信息和实施指南,请参阅 http://www.codeproject.com/Articles/12932/Saving-and-Restoring-RichTextBox-Formatted-Text-Al

于 2012-07-13T22:51:24.377 回答