예제 #1
0
        private void btnGetFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openedDialog = new OpenFileDialog();
            PDFParser      pdfParser    = new PDFParser();
            StringBuilder  sbText       = new StringBuilder();

            try
            {
                openedDialog.CheckFileExists  = true;
                openedDialog.CheckPathExists  = true;
                openedDialog.DefaultExt       = "txt";
                openedDialog.DereferenceLinks = true;
                openedDialog.Filter           = "PDF files (*.pdf)|*.pdf|" +
                                                "Text files (*.txt)|*.txt|" +
                                                "RTF files (*.rtf)|*.rtf|" +
                                                " + Works 6 and 7 (*.wps)|*.wps|" +
                                                "Windows Write (*.wri)|*.wri|" +
                                                "WordPerfect document (*.wpd)|*.wpd";
                openedDialog.Multiselect      = false;
                openedDialog.RestoreDirectory = true;
                openedDialog.ShowHelp         = true;
                openedDialog.ShowReadOnly     = false;
                openedDialog.Title            = "select a file ";
                openedDialog.ValidateNames    = true;

                if (openedDialog.ShowDialog() == DialogResult.OK)
                {
                    if (Path.GetExtension(openedDialog.FileName.ToString()) == ".pdf")
                    {
                        PdfReader pdfReader = new PdfReader(openedDialog.FileName.ToString());
                        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                        {
                            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

                            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                            sbText.Append(currentText);
                        }
                        pdfReader.Close();
                        pdfParser.ExtractText(openedDialog.FileName.ToString(),
                                              Path.GetFileNameWithoutExtension(openedDialog.FileName.ToString()) + ".txt");


                        rtxtbxBookText.Text = sbText.ToString();
                    }

                    else
                    {
                        StreamReader sr = new StreamReader(openedDialog.OpenFile());
                        rtxtbxBookText.Text = sr.ReadToEnd();
                    }
                    textToRead = sbText.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void btnGetFile_Click(object sender, EventArgs e)
        {
            string         fileName;
            string         fileType;
            OpenFileDialog openedDialog = new OpenFileDialog();
            PDFParser      pdfParser    = new PDFParser();

            try
            {
                openedDialog.CheckFileExists  = true;
                openedDialog.CheckPathExists  = true;
                openedDialog.DefaultExt       = "txt";
                openedDialog.DereferenceLinks = true;
                openedDialog.Filter           = "PDF files (*.pdf)|*.pdf" +
                                                "Text files (*.txt)|*.txt|" +
                                                "RTF files (*.rtf)|*.rtf|" +
                                                " + Works 6 and 7 (*.wps)|*.wps|" +
                                                "Windows Write (*.wri)|*.wri|" +
                                                "WordPerfect document (*.wpd)|*.wpd";
                openedDialog.Multiselect      = false;
                openedDialog.RestoreDirectory = true;
                openedDialog.ShowHelp         = true;
                openedDialog.ShowReadOnly     = false;
                openedDialog.Title            = "select a file ";
                openedDialog.ValidateNames    = true;
                fileName = openedDialog.FileName.ToString();

                if (openedDialog.ShowDialog() == DialogResult.OK)
                {
                    fileType = fileName.Substring(fileName.Length - 3, fileName.Length);
                    if (fileType == "pdf")
                    {
                        PdfReader pdfReader = new PdfReader(fileName);
                        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                        {
                        }
                        pdfParser.ExtractText(fileName,
                                              Path.GetFileNameWithoutExtension(fileName) + ".txt");
                        StreamReader sr = new StreamReader(fileName + ".txt");
                        rtxtbxBookText.Text = sr.ReadToEnd();
                    }
                    else
                    {
                        StreamReader sr = new StreamReader(openedDialog.OpenFile());
                        rtxtbxBookText.Text = sr.ReadToEnd();
                    }
                }
            }
            catch
            {
                MessageBox.Show("can not open the file", "Text to speech");
            }
        }