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
Solved

read pdf files

Posted on 2009-05-06
10
328 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

861 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