How to Read Text Pdf File in Textbox wpf (c#)

KwebenaAcquah-9104 306 Reputation points
2021-08-04T09:34:09.79+00:00

please; hello; i am having a txtspeaker and would like to show pdf files each time a user clicks a button; like this

this is what i have tried; works in displaying text files but i would like to display pdf file in txtspeaker but i don't know how to do this;

                var path = @"Oxford guide to English grammar - PDF Room.pdf";
                using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                using var sr = new StreamReader(fs, Encoding.UTF8);
                string content = sr.ReadToEnd();
                txtSpeaker.Text = content;
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} vote

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-08-05T02:37:57.92+00:00

    The textbox control cannot display the content of the pdf anyway, and it cannot parse the format of the pdf file.

    To display pdf in WPF, the easiest way should be to add a nuget package: Microsoft.Web.WebView2

    Then use the following code to do it:

        <Grid>  
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="697,159,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>  
            <Wpf:WebView2 x:Name="webview2" Height="299" Margin="41,47,0,0" VerticalAlignment="Top" Width="480"/>  
        </Grid>  
    
            public MainWindow()  
            {  
                InitializeComponent();  
                webview2.Source = new Uri(@"http://www.bing.com");  
            }  
      
            private void button_Click(object sender, RoutedEventArgs e)  
            {  
                webview2.CoreWebView2.Navigate(@"C:\...\sample.pdf");  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-08-04T13:44:28.15+00:00

    You need to parse the text from the PDF and for that you need some sort of PDF library. One such library is the Leadtools.pdf nuget:
    https://www.nuget.org/packages/Leadtools.Pdf/

    This will allow you to load the pdf page-by-page and parse out the text per-page (even if it's an image based PDF scan) and speak it aloud via your txtspeaker. Here is some code in a tutorial to get you started:
    https://www.leadtools.com/help/sdk/v21/tutorials/dotnet-console-parse-the-text-of-a-document.html

    I am a vendor of this library, so let me know if you have any difficulties using it or any other questions!


  2. Dheeraj Malik 1 Reputation point
    2021-08-10T03:18:24.927+00:00

    You can install Spire.PDF via NuGet using the Package Manager Console:

    PM> Install-Package Spire.PDF -Version 7.8.2

    Then use the following code to read text in PDF.

    //Load PDF
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile(@"..\Sample.pdf");
    
    StringBuilder buffer = new StringBuilder();
    foreach (PdfPageBase page in doc.Pages)
    {
        buffer.Append(page.ExtractText());    
    }
    
    doc.Close();
    //save text
    String fileName = "TextInPdf.txt";
    File.WriteAllText(fileName, buffer.ToString());
    

    Apart from reading text, you can also read images or do other manipulations on PDF.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.