Kavya Reddy
Kavya Reddy

Reputation: 5

how to display pdf document in aspx page using web control?

I have created a Web application, in that user can do his course by reading the PDF document. For that purpose I need to open that PDF file in new aspx page.

Is there any control to open the PDF? Or Is there any other way to open the PDF?

If you know please help me.

Thanks & Regards, k.kavya

Upvotes: 1

Views: 4932

Answers (3)

GeekyCoder
GeekyCoder

Reputation: 428

You can try this

private void ReadPdfFile()
        {
            string path = @"C:\Swift3D.pdf";
            WebClient client = new WebClient();
            Byte[] buffer =  client.DownloadData(path);



     if (buffer != null)
        {
            Response.ContentType = "application/pdf"; 
            Response.AddHeader("content-length",buffer.Length.ToString()); 
            Response.BinaryWrite(buffer); 
        }

    }

You can find more info at.. http://geekswithblogs.net/azamsharp/archive/2005/09/18/54294.aspx


Now your requirement is that you basically want to read a pdf file you can use a good library for that such as iTextSharp. http://sourceforge.net/projects/itextsharp/

and then use it to extract the data from the pdf into a string

public string ReadPdfFile(string fileName)
{
    StringBuilder text = new StringBuilder();

    if (File.Exists(fileName))
    {
        PdfReader pdfReader = new PdfReader(fileName);

        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
        {
            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
            text.Append(currentText);
        }
        pdfReader.Close();
    }
    return text.ToString();
}

then display that string into your web page.... It should'nt be difficult now.. Hope it helps.

Upvotes: 2

Naresh Pansuriya
Naresh Pansuriya

Reputation: 2045

Viewing the PDF file in the browser (without download) requires an add-on to the client's browser. Google Chrome, for example, has a built-in PDF viewer and can open files directly, while IE and Firefox require that you install a plug-in (the one that comes with Adobe Reader).

There are two other approaches:

Convert the file to HTML, image, or any other format that can be directly viewed in the browser. This conversion can be on-the-fly using a server-side (written in PHP, Python, ASP, whatever language), or you can just pre-convert all files to a readable one.

The other approach, which is the best, is to use a Flash-based PDF viewer (such as http://flexpaper.devaldi.com/). This is easy, flexible and doesn't require writing server-side code. This approach is used by many Document-sharing sites (e.g. http://www.scribd.com/, http://www.slideshare.net/, http://www.docstoc.com/)

Upvotes: 0

Eugene Pavlov
Eugene Pavlov

Reputation: 698

This kind of behavior is handled by the browsers itself, all you need to just flush pdf doc to browser (as suggested by GeekyCoder), and then depends on browser you will see document or will be presented with Save As dialog.

There is no native controls that will display pdf. In case you would like to be sure pdf is always displayed on page you would need to create control for it on your own (or maybe search for free/paid custom one).

Upvotes: 0

Related Questions