Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
How to open pdf files using C#

Thanks in advance
Posted
Comments
Manoj Kumar Choubey 17-Oct-12 7:43am    
Explain More your question, do you want to open in any viewer web based / desktop application based ?
Dee_Bee 17-Oct-12 8:13am    
Yes, it is viewer web based.
Dee_Bee 17-Oct-12 8:15am    
I added a pdf file as an attachment, I want to open it in my web application.

In order to get the full file path on the server you'll want to use Server.MapPath.
C#
string fullFileName = Server.MapPath("../myFile.pdf");

After that you'll need the Process object to "run" it:
C#
System.Diagnostics.Process.Start(fullFileName);

If you want the file to be opened on the client side, your best bet is to create and HTTP Handler and set the appropriate mime type on your response before streaming it out from your handler.

Code to stream a file out to client.
C#
public void ProcessRequest(HttpContext context)  
{   
   int newsId = int.Parse(context.Session["newsId"].ToString());
   int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
   string dirPathForTextFiles =  ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
   DataTable UpdatedDateTable = (DataTable)context.Session["theLastUpdatedTextFile"];
   UpdatedDateTable.AcceptChanges();
   context.Session.Add("currentTextFile", UpdatedDateTable);
   List<string> l = new List<string>(UpdatedDateTable.Rows.Count);

   try
   {

      l.Add(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       context.Response.ContentType = getContentType(dirPathForTextFiles + UpdatedDateTable.Rows[0]["fileName"].ToString());
       using (FileStream fs = new FileStream(l[0], FileMode.Open, FileAccess.Read))
       {
          long chunkSize = fs.Length; 
          byte[] buf = new byte[chunkSize]; 
          int bytesRead = 1; 
          bytesRead = fs.Read(buf, 0,(int)chunkSize); 
          if (bytesRead > 0) context.Response.OutputStream.Write(buf, 0, buf.Length);
          context.Response.OutputStream.Flush();
      }

  }
  catch (IOException e)
  {
     string message = e.Message;
  }   
}
 
Share this answer
 
v2
Comments
Dee_Bee 18-Oct-12 0:53am    
thanks Mr.Manoj
C#
private void button1_Click(object sender, EventArgs e)
        {
            // Create object of Open file dialog class
 
            {
                OpenFileDialog dlg = new OpenFileDialog();
                // set file filter of dialog 
                dlg.Filter = "pdf files (*.pdf) |*.pdf;";
                dlg.ShowDialog();
                if (dlg.FileName!= null)
                {
                    // use the LoadFile(ByVal fileName As String) function for open the pdf in control
                    axAcroPDF1.LoadFile(dlg.FileName);
                }
 
            }
 
        }


To use the Reader control, first make sure that you have downloaded and installed the free Acrobat Reader from Adobe.

You can download it from here
 
Share this answer
 
v2
Use this code to How to open pdf files

private void button1_Click(object sender, EventArgs e)
{
// Create object of Open file dialog class

{
OpenFileDialog dlg = new OpenFileDialog();
// set file filter of dialog
dlg.Filter = "pdf files (*.pdf) |*.pdf;";
dlg.ShowDialog();
if (dlg.FileName!= null)
{
// use the LoadFile(ByVal fileName As String) function for open the pdf in control
axAcroPDF1.LoadFile(dlg.FileName);
}

}

}
 
Share this answer
 
private void button1_Click(object sender, EventArgs e)
{
// Create object of Open file dialog class

{
OpenFileDialog dlg = new OpenFileDialog();
// set file filter of dialog
dlg.Filter = "pdf files (*.pdf) |*.pdf;";
dlg.ShowDialog();
if (dlg.FileName!= null)
{
// use the LoadFile(ByVal fileName As String) function for open the pdf in control
axAcroPDF1.LoadFile(dlg.FileName);
}

}

}
 
Share this answer
 
Comments
[no name] 26-Jun-13 6:27am    
Seriously? 3 separate answers to answer a question that is almost a year old and had already been answered. Are you that desperate for those rep points that mean absolutely nothing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900