Solved

read pdf files

Posted on 2009-05-06
10
297 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XML to SQL Table using c# 5 47
C# Update Status Bar.... 1 24
Image(7) 1 37
index Out OF Range Exception error 4 32
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now