Solved

read pdf files

Posted on 2009-05-06
10
339 Views
Last Modified: 2012-05-06
I want to create a page that Accessing Info hyperlink to when user cicks that it will go search to get the latest dated pdf file and opens up for user to view. It should always search for the latest updated file. Do I need to use File Class or FileINfoClass for that? anyone can help me to set up that please?
0
Comment
Question by:1jaws
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24319203
Dim dir As New System.IO.DirectoryInfo("Path")
        For Each file As System.IO.FileInfo In dir.GetFiles()
            ' Process each file here
            file.LastWriteTime -> Use this property
        Next

Use System.IO.StreamReader to read the file

the following to push the file to the browser
Response.AddHeader("content-disposition", "inline;")
            Response.AddHeader("Content-Type", "application/pdf")
            Response.BinaryWrite(pdf)  'pdf is a byte array
            Response.Flush()
            Response.Close()
            HttpContext.Current.ApplicationInstance.CompleteRequest()
0
 

Author Comment

by:1jaws
ID: 24319398
can I have this in C#? and I didnt understand what I need to do here   file.LastWriteTime -> Use this property
 can you be able to give example ..
lets say I have a pdf files in folder MY PDF and lets I have two files  
test1.pdf (updated  5/4/09)
test2.pdf (updated  5/5/09)
which means are I need to get the test2.pdf file because that is the latest updated pdf. So, this point how can I generate my code that user will click that Accessing Info link and see that pdf file test2?
0
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
ID: 24319654

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\myfolder");
    foreach (System.IO.FileInfo file in dir.GetFiles("*.pdf").OrderByDescending((System.Object x) => x.LastWriteTime)) {
        //Get the first file and exit
        processFile(file.FullName, file.Length);
        break; // TODO: might not be correct. Was : Exit For
    }

private void processFile(string fileName, Int64 fileLength)
{
    System.IO.BinaryReader s = new System.IO.BinaryReader(new System.IO.FileStream(fileName, IO.FileMode.Open));
    byte[] buf = null;
    s.Read(buf, 0, fileLength - 1);
    s.Close();
   
    Response.AddHeader("Content-Type", "application/pdf");
    Response.BinaryWrite(buf);
    //pdf is a byte array
    Response.Flush();
    Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

NOTE: Above code is not tested. It will compile, though.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:1jaws
ID: 24323026
thank you very much for showing in example, but for some reason I am still really not too clear about it. first part of code do I need to put it on page load  or the hyperlink clcik event I guess right? and do I need to specify (file.FullName,file.Lenght) part ..
sorry about that..
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24324288
replace c:\myfolder with your forlder name. That's all. When do you want to show the PDF? If it's the on click event of the hyperlink, then copy the 1st part of the code to the onclick event.
0
 

Author Comment

by:1jaws
ID: 24361091
yes, I need hyperlink click event but hyperlink click event is different than regular click event  like for the button for ex.  how can I do that?
0
 

Author Comment

by:1jaws
ID: 24361142
I used linkbutton , for it..

I tested the code , it is giving this problem
'System.Array' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)      

'object' does not contain a definition for 'LastWriteTime' and no extension method 'LastWriteTime' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)      
0
 

Author Comment

by:1jaws
ID: 24361170
also these
The best overloaded method match for 'System.IO.BinaryReader.Read(byte[], int, int)' has some invalid arguments

complaining about file  filelenght, cannot convert long to int -  do I need to put number that like 386 KB for ex.
      
0
 

Author Comment

by:1jaws
ID: 24367078
can anybody please help!
0
 

Author Closing Comment

by:1jaws
ID: 31578698
thank you!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question