Tuesday, July 29, 2008

How to display PDF documents in ASP.Net --Same page

ASPX

<div>
<table border="1"><tr><td>
<asp:Button ID="DisplayPDF" runat=server Text = "Display PDF" OnClick="DisplayPDF_Click" />
</td></tr>
<tr><td width="500" height="500">
<asp:PlaceHolder ID ="placeHolder1" runat=server></asp:PlaceHolder>
</td></tr></table>
</div>

Code behind

private void displayPDFFile(string PDFFileName)
{
int Width = 500;
int Height = 500;

//Build the PDF Document Content
StringBuilder sb = new StringBuilder();
sb.Append("<iframe src=" + PDFFileName.ToString() + " ");
sb.Append("width=" + Width.ToString() + " height=" + Height.ToString() + " ");
sb.Append("<View PDF: <a href=" + PDFFileName.ToString() + "</a></p> ");
sb.Append("</iframe>");

//Create Control to hold that PDF document
Literal literal1 = new Literal();
literal1.Text = sb.ToString();
literal1.DataBind();

//Clear PlaceHolder Controls and Add current literal to display in PlaceHolder
placeHolder1.Controls.Clear();
placeHolder1.Controls.Add(literal1);

}
protected void DisplayPDF_Click(object sender, EventArgs e)
{
displayPDFFile(@"fw9.pdf");
}

we are using System.Text name space. so don't forget to add using System.Text in code behind.

Thursday, July 10, 2008

C# Sample Projects



C# Sample Projects with optional source code links

eggheadcafe

cplus.about.com

codeproject

download3k

itu

codeproject/game

codebeach/game

gamedev

deitel

csharp-source.net

getafreelancer

getacoder