1. Simply change the contenttype and open the PDF file

Response.Clear();
Response.ContentType = "application/pdf"; //change the content here based on different files type
Response.AddHeader("Content-Disposition", "inline;filename=" + strFilePath);
Response.WriteFile(strFilePath);
Response.End();

2. use Iframe Html control, set the src attribute of the control as PDF file and thats it..

Following is a ASP.net server control which uses the iframe,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PdfViewer
{
    [DefaultProperty("FilePath")]
    [ToolboxData("<{0}:ShowPdf runat=server></{0}:ShowPdf>")]
    public class ShowPdf : WebControl
    {
        #region "Declarations"

        private string mFilePath;

        #endregion
        #region "Properties"

        [Category("Source File")]
        [Browsable(true)]
        [Description("Set path to source file.")]
        [Editor(typeof(System.Web.UI.Design.UrlEditor),
            typeof(System.Drawing.Design.UITypeEditor))]

        public string FilePath
        {
            get
            {
                return mFilePath;
            }

            set
            {
                if (value == string.Empty)
                {
                    mFilePath = string.Empty;
                }
                else
                {
                    int tilde = -1;
                    tilde = value.IndexOf('~');

                    if (tilde != -1)
                    {
                        mFilePath = value.Substring((tilde + 2))
                                         .Trim();
                    }
                    else
                    {
                        mFilePath = value;
                    }
                }
            }
        }   //end FilePath property

        #endregion

        #region "Rendering"

        protected override void RenderContents(HtmlTextWriter writer)
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("<iframe src="
                          + FilePath.ToString() + " ");

                sb.Append("width=" +
                          Width.ToString() + " height=" +
                          Height.ToString() + " ");

                sb.Append("<View PDF: <a href=" + FilePath.ToString() + "</a></p>");

                sb.Append("</iframe>");

                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.Write(sb.ToString());

                writer.RenderEndTag();
            }
            catch
            {

                // with no properties set, this will render
                // "Display PDF Control" in
                // a box on the page

                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                writer.Write("Display PDF Control");

                writer.RenderEndTag();
            }  // end try-catch

            //end RenderContents

        #endregion

        }
    }
}

Refer the above control in WebPage :
<%@ Register TagPrefix="PDF" Namespace="PdfViewer" Assembly="<yourAssemblyname>" %>

Reference:- http://www.codeproject.com/aspnet/EmbedPDFinWebpages.asp

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Opening PDF document inside Webform

Humm... interesting,

Great,so can we compile it and add it to the tool box instead of registering it in the page?

Anyway, thanks for the post 8/19/2009 9:47 AM | web development company

# re: Opening PDF document inside Webform

There are various approaches used to display PDF documents in a webpage. Displaying PDF documents in a webpage using simple ASP.NET custom server control or Directly rendering the PDF documents on the webpage. So if you want to know more details on these approaches, you can refer following links.

http://www.protalk.in/dotnet/how-to-display-pdf-documents-with-asp-net-part-1/

http://www.protalk.in/dotnet/how-to-display-pdf-documents-with-asp-net-part-2/ 10/12/2010 10:00 AM | Subash Rao

Post a comment





 

 

News

Employers
Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with MyGooglePageRank.com

Archives

Post Categories

Image Galleries

Articles & Magazines

ASP.Net 2.0 Compilation

ASP.Net, Blogs I refer...

Atlas

Dost

Drivers and Software Download

Garhwal

Travel Domain

WSS and WebParts

Syndication: