繁体   English   中英

使用C#保存pdf文件中突出显示的文本

[英]save text highlighted in pdf file using C#

我构建了一个Windows窗体应用程序,该应用程序可以打开任何文本文件(甚至使用iTextSharp Dll的pdf),并在一个丰富的tex框中查看其内容。颜色。 我创建了一个保存按钮。

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

提前致谢!

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

string str = richTextBox.Rtf;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM