0

.pdf ドキュメントを読み取り、そこから有用なテキストをすべて抽出するアプリケーションを構築しようとしています。私の目標は、これを学校のプロジェクトの剽窃チェッカーとして使用することです。

Visual Studio C# で GroupDocs を使用しています。以下のコードでは、ファイルを開くダイアログを使用して適切なファイル パスを取得していることがわかります。「MessageBox.Show(filePath)」を使用すると、デバッグ中にこれを確認できます。

ただし、オブジェクト Parser parser = new Parser(filePath) を初期化しようとすると、次のようになります。

using (Parser parser = new Parser(filePath)) //VS specifically points the 
                                               error to this line
            {
                using (TextReader reader = parser.GetText())
                {
                    Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd());
                }
            }

SystemInitializationException という例外が発生し、FileNotFoundException という内部例外が発生します。上記のコード スニペットでは、filePath 変数をディレクトリ名に置き換えてみました。たとえば、C://School Files//Why Should People Go to Space.pdf などです。

Visual Studio によって作成されたエラー メッセージ

写真のファイル名と書かれている場所は、実際のファイル名ではないという手がかりがあります。ただし、実際にこれを変更する方法はわかりません。必要なすべての情報を提供しようとしましたが、さらに必要な場合はお知らせください。以下にコード全体を掲載します。

using System;
using System.Windows.Forms;
using GroupDocs.Viewer;
using GroupDocs.Search;
using GroupDocs.Parser;
using GroupDocs.Comparison;
using System.Net;

using System.IO;

using System.Web;

namespace project
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var client = new WebClient();
            client.Headers.Add("User-Agent", "C# console program");

            string url = "https://peerj.com/articles/cs-132/";
            string content = client.DownloadString(url);
            //content contains ENTIRE html code for the webpage
            richTextBox1.Text = content;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var filePath = string.Empty;

            //To retrieve the file path with a dialog window
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "DOCX files (*.docx)|*.docx|PDF files (*.pdf)|*.pdf";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //Get the path of specified file
                    filePath = openFileDialog.FileName;

                }
            }
            MessageBox.Show(filePath);

            //Operate on the filePath string to get text from file
            using (Parser parser = new Parser(filePath))
            {
                using (TextReader reader = parser.GetText())
                {
                    Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd());
                }
            }
        }
    }
}
4

0 に答える 0