Breaking News

Editors Picks

Showing posts with label aspx page to pdf. Show all posts
Showing posts with label aspx page to pdf. Show all posts

Wednesday, September 22, 2010

how to export aspx page to pdf

Code For Default.aspx Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="sunil" runat="server">
<asp:Panel ID="Panel1" runat="server" >

<table cellpadding="0"  cellspacing="0" style="width: 90%">
<tr>
<td align="center"  valign="top">
<asp:Image ID="Image1" runat="server" ImageUrl="ganesha2.thumbnail.jpg" Width="50px" Height="31px" />
</td>
</tr>
<tr><td>
<table border="1" cellpadding="0"  cellspacing="0">

<tr>
<td valign="top" align="left">
&nbsp;<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td colspan="3">
<asp:Label ID="lblcompanyname" runat="server" Font-Names="Castellar" Width="322px" Height="53px" Font-Size="XX-Large" Font-Bold="False"></asp:Label></td>
</tr>
<tr>
<td  colspan="3" align="left">
<div id="dvEmailID" runat="server"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left"  valign="top" style="border-bottom-width:1px; border-bottom-color:Black; border-bottom-style:solid">
Client Name:&nbsp;<asp:Label ID="lblclientname" runat="server"></asp:Label>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
City :&nbsp; <asp:Label ID="lblcustcity" runat="server" ></asp:Label>

</td>


</tr>
<tr><td style="border-top:solid 1px black"></td></tr>
<tr>
<td valign="top">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">

<tr>
<td valign="top" >
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td style="width:5%">
SNO.</td>
<td style="width:55%">
Name</td>
<td style="width:10%" >
Sex</td>
<td style="width:10%">
Age</td>
<td style="width:20%">
Fare</td>
</tr>
<tr>
<td style="width:5%">
1</td>
<td>
<asp:Label ID="lblpassername1" runat="server" Width="201px"></asp:Label></td>
<td  >
<asp:Label ID="lblsex1" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblage1" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblfare1" runat="server" ></asp:Label></td>
</tr>
<tr>
<td style="width:5%; height: 21px;" >
2</td>
<td style="height: 21px" >
<asp:Label ID="lblpassansername2" runat="server" Width="201px"></asp:Label></td>
<td style="height: 21px"  >
<asp:Label ID="lblsex12" runat="server" ></asp:Label></td>
<td style="height: 21px" >
<asp:Label ID="lblage2" runat="server" ></asp:Label></td>
<td style="height: 21px" >
<asp:Label ID="lblfare2" runat="server" ></asp:Label></td>
</tr>
<tr>
<td style="width:5%" >
3</td>
<td>
<asp:Label ID="lblpassansername3" runat="server" Width="200px"></asp:Label></td>
<td  >
<asp:Label ID="lblsex3" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblage3" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblfare3" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width:5%">
4</td>
<td >
<asp:Label ID="lblpassansername4" runat="server" Width="200px"></asp:Label></td>
<td  >
<asp:Label ID="lblsex4" runat="server"></asp:Label></td>
<td >
<asp:Label ID="lblage4" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblfare4" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width:5%; height: 21px;">
5</td>
<td style="height: 21px" >
<asp:Label ID="lblpassansername5" runat="server" Width="200px"></asp:Label></td>
<td style="height: 21px">
<asp:Label ID="lblsex5" runat="server"></asp:Label></td>
<td style="height: 21px" >
<asp:Label ID="lblage5" runat="server" ></asp:Label></td>
<td style="height: 21px" >
<asp:Label ID="lblfare5" runat="server" ></asp:Label></td>
</tr>
<tr>
<td style="width:5%" >
6</td>
<td >
<asp:Label ID="lblpassansername6" runat="server" Width="200px"></asp:Label></td>
<td  >
<asp:Label ID="lblsex6" runat="server" ></asp:Label></td>
<td >
<asp:Label ID="lblage6" runat="server"></asp:Label></td>
<td >
<asp:Label ID="lblfare6" runat="server" ></asp:Label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td valign="top">
</td>
<td  valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td >
Advance</td>
<td>
<asp:Label ID="lbladnace" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td>
Cancel Fare</td>
<td >
<asp:Label ID="lblcancelfare" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td >
Commission</td>
<td >
<asp:Label ID="lblcomminstion" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td>
TOTAL</td>
<td>
<asp:Label ID="lbltotal" runat="server" Width="116px"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="width: 100%; height: 255px;">
<tr>
<td colspan="2" >
&nbsp;<asp:Label ID="Label2" runat="server" Text="Bill No :" Width="116px"></asp:Label></td>
<td colspan="2" >
<asp:Label ID="lblbillno" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" >
&nbsp;Bill Date :</td>
<td colspan="2" >
<asp:Label ID="lblbilldate" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td colspan="2">
&nbsp;Phone No.</td>
<td colspan="2">
<asp:Label ID="lblmobileno" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td >
&nbsp;From</td>
<td >
<asp:Label ID="lblfromstation" runat="server" Width="87px"></asp:Label></td>
<td >
To</td>
<td >
<asp:Label ID="lbltostation" runat="server" Width="77px"></asp:Label></td>
</tr>
<tr>
<td colspan="2">
&nbsp;Train / Flight No.</td>
<td colspan="2">
<asp:Label ID="lblTrainNO" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td >
&nbsp;PNR</td>
<td >
<asp:Label ID="lblPNRNo" runat="server" Width="82px"></asp:Label></td>
<td >
Class</td>
<td >
<asp:Label ID="lblClass" runat="server" Width="78px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" >
&nbsp;J Date</td>
<td colspan="2" >
<asp:Label ID="lbljarnydate" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" >
&nbsp;Quota</td>
<td colspan="2">
<asp:Label ID="lblQuto" runat="server" Width="116px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" >
&nbsp;Status</td>
<td colspan="2" >
<asp:Label ID="lblstatus" runat="server" Width="116px"></asp:Label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 360px">
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2" align="center" >
Thank You. We appreciate your business.</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
</table>

</asp:Panel>


</div>
</div>
</form>
</body>
</html>


Code For Default.aspx.cs page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;


public partial class Default3 : System.Web.UI.Page
{
    connection con;
    string comname;
    public Default3()
    {
        con = new connection();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
      
       
    }
    protected override void Render(HtmlTextWriter writer)
    {

        //HtmlTextWriter writer = new HtmlTextWriter();
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
       string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);

    }
    public void CreatePDFDocument(string strHtml)
    {

      string  strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
      Document document = new Document(PageSize.A4.Rotate(), 80, 50, 30, 65);
   //    String src = Server.MapPath("ganesha2.thumbnail.jpg");
        //String src = Server.MapPath(Image1.ImageUrl);
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
   //     iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(new Uri(src));
       
        //  gif.Width=10;
       // gif.Height=10;
        //gif.AbsoluteX = Convert.ToDecimal(80.0);
        //gif.AbsoluteY = Convert.ToDecimal(80);
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
    //    document.Add(gif);
        obj.Parse(se);
        document.Close();
        ShowPdf(strFileName);


    }
    public void ShowPdf(string strFileName)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        Response.ContentType = "application/pdf";
        Response.WriteFile(strFileName);
        Response.Flush();
        Response.Clear();
    }
}
Read more ...

Contact Us

Name

Email *

Message *