convert asp.net page in pdf

Convert asp.net page in Pdf


Today  i  am going to share with you  my real story what i am faced when i am trying to generate a  automated and secure system for generating company invoice....

  The major problem was with me  is  that i m going to work on tally data .
  Firstly i had work on data.convert all tally data in sql format ,so that i ma use it  on run time.

After  that i m came on real work with asp.net....

i   just simply  write some code with sql  query on .aspx  form   fro selecting a  unique invoice number from database  and then direct this  inovice no to another page  for its  real (PDF generation)..

Download some dll

(1)itextsharp.dll
(2)QueryStringEncryption.dll
(3)Interop.LCC11.dll

I made a  page with name  .docs.aspx, and its  code is ....

        <%
            ad = new SqlDataAdapter("SELECT distinct Invoice_ref from  tally_bill_data_4_pdf  where invoice_ref <>'null'", con);
            dt1 = new DataTable();
            ad.Fill(dt1);
            int count = dt1.Rows.Count;
     if(count>0)
     {
         for (int i = 0; i <count;i++ )
         {
             string invoice_number =dt1.Rows[i]["Invoice_ref"].ToString();
  
         %>
      
        <script language ="javascript" type="text/javascript">window.open("pdf_doc.aspx?inev=<%=invoice_number %>")</script>
        <%}
         }%>




An then using this invoice No. from querystring  i made a  20000 invoice number..





My another  page name is pdf_doc.aspx....


On  pdf_doc.aspx  i completely design my Invoice  format,assgined its value using Label Control..

 On  pdf_doc.cs...
 i assign value  to label and  done  real work...




use this some namespace



using System.Text;
using System.IO;
using System.Diagnostics;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;



 protected void Page_Load(object sender, EventArgs e)
    {
        string q_str = Request.QueryString["inev"].ToString();

        string qu = "select * from tally_bill_data_4_pdf where Invoice_ref='" + q_str + "'";

        SqlDataAdapter ad1 = new SqlDataAdapter(qu, con);
        DataTable dt2 = new DataTable();
        ad1.Fill(dt2);
        if (dt2.Rows.Count > 0)
        {
            lbl_invoice_no.Text = dt2.Rows[0]["invoice_ref"].ToString();
            lbl_date.Text = DateTime.Now.ToString();
            lbl_region.Text = dt2.Rows[0]["region"].ToString();
            lbl_branch.Text = dt2.Rows[0]["branch"].ToString();
            lbl_wbs_elmnt.Text = dt2.Rows[0]["wbs"].ToString();
            lbl_circuit_id.Text = dt2.Rows[0]["cid"].ToString();
            lbl_other_ref.Text = "";
        }
      
        string attachment = "attachment; filename='" + q_str + "'.pdf";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/pdf";

        StringWriter stw = new StringWriter();

        HtmlTextWriter htextw = new HtmlTextWriter(stw);

        docs1.RenderControl(htextw);

        Document document = new Document();

        PdfWriter.GetInstance(document, Response.OutputStream);

        document.Open();

        StringReader str = new StringReader(stw.ToString());

        iTextSharp.text.html.simpleparser.HTMLWorker htmlworker = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        ;
        htmlworker.Parse(str);

        document.Close();

        Response.Write(document);
        Response.End();

    }





Run this projetc by making docs.aspx as  start page..




Thats  all........

I hope  it will definitely Help  You.........
    
























Comments

  1. The 'Docs1' is Div on .aspx page where all control are placed inside it.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click