You must Sign In to post a response.
  • Category: General

    To read a pdf file from filepath

    Hi,

    I am developing a windows forms application. I need to open the pdf file that is present in the shared folder while clicking a button. I have used the below code but not able to open it, provide suggestion to open

    if (File.Exists(filepath))
    {
    using (FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.None))
    {
    byte[] b = new byte[1024];
    UTF8Encoding temp = new UTF8Encoding(true);

    while (fs.Read(b, 0, b.Length) > 0)
    {
    Console.WriteLine(temp.GetString(b));
    }
    }

    }
  • #756037

    Hi Sabu,

    Please refer below code snippet to read pdf file from a shared folder. This may not be the what exactly you are looking for but I hope it will help you to solve your issues.

    private void linklblBanner_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
    // Code for Local PC
    System.Diagnostics.Process.Start(@"D:\PayrollManagementSystem\PayrollManagementSystem\PDF-Files\SOP.pdf");
    byte[] byteArray = System.IO.File.ReadAllBytes(@"D:\PayrollManagementSystem\PayrollManagementSystem\PDF-Files\SOP.pdf");

    // Code for Remote PC
    System.Diagnostics.Process.Start(@"\\PayrollServer\D\PayrollManagementSystem\PayrollManagementSystem\PDF-Files\SOP.pdf");
    byte[] byteArray = System.IO.File.ReadAllBytes(@"\\PayrollServer\D\PayrollManagementSystem\PayrollManagementSystem\PDF-Files\SOP.pdf");
    }

    Hope it will be helpful to you.


    Thanks,
    Ram Prasad

  • #756110
    You can also make use of a third party component ItextSharp to read hte PDF file.

    here is a sample code, but before using it make sure that you have added the required DLL



    using iTextSharp.text.pdf;
    using iTextSharp.text.pdf.parser;

    // ...

    public static string ExtractTextFromPdf(string path)
    {
    using (PdfReader reader = new PdfReader(path))
    {
    StringBuilder text = new StringBuilder();

    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
    text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
    }

    return text.ToString();
    }
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM


  • Sign In to post your comments