保存文本的PDF文件强调了使用C# [英] save text highlighted in pdf file using C#

查看:162
本文介绍了保存文本的PDF文件强调了使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建打开任何文本文件(甚至是PDF使用iTextSharp的DLL)中的Windows窗体应用程序,并查看了丰富的特箱中的内容,搜索字段在那里我可以搜索特定的模式,将在突出显示所有可能的匹配金色。我创建了一个保存按钮。

I built a windows form application that opens any text file (even pdf using iTextSharp Dll) and view its contents in a rich tex box, a search field where i can search for a certain pattern, all possible matches to be highlighted in "Gold" color. I created a save button.


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



在此先感谢!

Thanks in advance!

推荐答案

您可以请尝试使用这个与所有富文本格式代码相处的文字?

Can you please try using this to get text along with all rich text format codes?

string str = richTextBox.Rtf;

有关对于更多的信息和实施指引,这种情况下,请参考
http://www.codeproject.com/Articles/12932/节约型和恢复-RichTextBox的格式化文本 - 铝

For more information and implementation guidelines with respect to this context, please refer http://www.codeproject.com/Articles/12932/Saving-and-Restoring-RichTextBox-Formatted-Text-Al

这篇关于保存文本的PDF文件强调了使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆