Solved

read pdf files

Posted on 2009-05-06
10
345 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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