如何从文本文件中获取文本并将其显示在文本框中?

问题描述 投票:0回答:1

我正在使用 asp.net webforms。我使用 UploadFile 控件加载文本文件,我需要加载不同的文本文件 txt、doc、pdf 等...我只需要从文件中获取文本应该忽略任何图像,当获取纯文本时将其显示在 TextBox1

我已经使用 StreamReader 读取文件...

protected void UploadText_Click(object sender, EventArgs e)

        {

      StreamReader readTextFile = new StreamReader(FileUpload1.FileContent, Encoding.Unicode);
        TextBox1.Text = readTextFile.ReadToEnd();

        }

这里是上传文件控件:

<asp:FileUpload ID="FileUpload1" runat="server" />

它读取文件但无法从显示许多不同字符代码的文件中获取纯文本。我尝试了 Encode.Utf8/Encode.Unicode/Encode.ASCII 但仍然无法在 TextBox1 中显示纯文本。是否有任何编码/解码/转换解决方案可以从任何文本类型的文件中获取纯文本?

c# asp.net asp.net-mvc webforms
1个回答
1
投票

好吧,首先,你不能把一个文本文件、一个 pdf 文件和一个 word 文档放在同一个句子中!!!

例如,要编写代码,您需要能够“辨别”猫和珠穆朗玛峰之间的区别。

所以,这是珠穆朗玛峰:

这是一只猫:

所以,你能看出猫和山是不同的东西吗?

所以,就像上面的逻辑课一样?

你需要具备与上述相同的能力才能编写计算机代码。

那么,那么:

PDF 与文本文件有很大的不同。

Word 文档与文本文件有很大不同

PDF 与 word 文档文件有很大的不同。

以上逻辑你能掌握吗?

因此,您需要一些软件来阅读 PDF 并提取文本。

你需要一些软件来从word文档中提取文本。

还有一个文本文件?好吧,这已经是文本了,所以不需要任何软件。

因此,简单的逻辑在这里规定必须处理每种文件类型,并且您将需要能够读取或使用或“处理”这些 VAST 不同类型的文件格式的软件。

对于 pdf?我建议使用 GhostScript.net,这是从 pdf 中提取/提取文本的好方法。它有一个文本处理器,可以提取文本(并忽略图形)作为 GhostScript.net 的一部分。

对于 word 文档?那么这又在很大程度上取决于什么样的 word 文档。

如果 word 文档是一个 docx(自 2007 年以来一直如此——足足有 16 年了!!),那么文本部分可以轻松提取,因为这些文档被保存为 .zip 文件,如果您重新将 docx 文件命名为 .zip,然后你可以打开 zip 存档,在里面你会发现只有 TEXT 的 xml 部分。

但是,如果您要支持 word doc 文件,它们是 Microsoft 提供的封闭“二进制”格式。因此,虽然 word docx 文件是开放的 xml 标准,并且您可以很容易地提取文本,但 doc 文件是完全不同的事情。在这种情况下,您最好使用 doc 到 docx 转换器。您可以考虑在网络服务器上安装 word,但实际上不支持该设置。

编辑:一些工作代码示例:

第一个例子,一个word文档。这么说:

所以这个标记:

        <h4>Read Word doc text</h4>
        <asp:Button ID="cmdWord" runat="server" Text="Read word text"
            OnClick="cmdWord_Click"
            />
        <br />
        <br />

        <asp:TextBox ID="TextBox1" runat="server" 
            TextMode="MultiLine" Height="235px" Width="377px"></asp:TextBox>
        <br />
        <br />

nuget 库:

按钮代码:

    protected void cmdWord_Click(object sender, EventArgs e)
    {
        string sWordFile = @"c:\test\Example.docx";
        using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(sWordFile,false))
        {
            string sText = "";

            // get all text - no line breaks
            // sText = wordDocument.MainDocumentPart.Document.Body.InnerText;


            // get all text - line breaks
            var MySections 
                = wordDocument.MainDocumentPart.RootElement.Descendants<Paragraph>();

            foreach (Paragraph MySection in MySections)
                sText += MySection.InnerText + System.Environment.NewLine;

            TextBox1.Text = sText;

        }

    }

结果:

阅读 PDF 中的文本。

库:GhostScript.net (nuget)

(您需要 gsdll 的副本)来自实际的 ghostscript 库:

标记:

        <h4>Read PDF doc text</h4>
        <asp:Button ID="cmdPDF" runat="server" Text="Read PDF text"
            OnClick="cmdPDF_Click"
            />
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" 
            Height="235px" Width="377px"></asp:TextBox>

背后的代码:

    protected void cmdPDF_Click(object sender, EventArgs e)
    {
        // pdf extract
        string sPDFFile = @"c:\test\Example.pdf";

        string sText = "";
        sText = GetPDFText(sPDFFile);
        TextBox2.Text = sText;

    }

    public string GetPDFText(string strPDF)
    {

        Ghostscript.NET.GhostscriptVersionInfo gVER;
        // get locaiton of dll
        string strgDLL = AppDomain.CurrentDomain.BaseDirectory + @"\MyDLL\gsdll64.dll";
        gVER = new GhostscriptVersionInfo(new Version(0, 0, 0), strgDLL, "", Ghostscript.NET.GhostscriptLicense.GPL);

        using (Ghostscript.NET.Processor.GhostscriptProcessor gP = new Ghostscript.NET.Processor.GhostscriptProcessor(gVER, true))
        {
            using (Ghostscript.NET.GhostscriptPipedOutput gpH = new Ghostscript.NET.GhostscriptPipedOutput())
            {
                string outputPieHande = "%handle%" + int.Parse(gpH.ClientHandle).ToString("X2");

                List<string> Myargs = new List<string>();
                {
                    Myargs.Add("-empty");
                    Myargs.Add("-dSAFER");
                    Myargs.Add("-dBATCH");
                    Myargs.Add("-dNOPAUSE");
                    Myargs.Add("-dNOPROMPT");
                    Myargs.Add("-dFirstPage=1");
                    Myargs.Add("-sDEVICE=txtwrite");
                    Myargs.Add("-dNEWPDF=false");
                    Myargs.Add("-dNumRenderingThreads=4");
                    Myargs.Add("-sOutputFile=" + outputPieHande);
                    Myargs.Add("-q");      // quiet mode
                    Myargs.Add("-f");
                    Myargs.Add(strPDF);
                }

                try
                {
                    gP.StartProcessing(Myargs.ToArray(), null);
                    byte[] raw = null;

                    raw = gpH.Data;
                    var strOut = System.Text.Encoding.UTF8.GetString(raw);
                    gpH.Dispose();
                    return strOut;
                }
                catch (Exception ex)
                {
                    Debug.Print("my exception " + ex.Message);
                    return "threadexit";
                }
            }
        }
    }

文档示例:

结果:

© www.soinside.com 2019 - 2024. All rights reserved.