Style not applying while generating PDF using iTextsharp in ASP.Net

Last Reply 7 months ago By Mudassar

Posted 7 months ago

 hi sir,

 

It is displaying fine when it run on page. But when i generate pdf style not adding in html page and even image also showing in actual in size . I set text-align:center it showing left in pdf. please suggest how can i solve this issue .

 <div>
            <asp:Panel ID="pnlPerson" runat="server">
                <div id="divprint" runat="server" style="margin-top: 45px;">
                    <div style="width: 100%">
                        <div id="divBill" runat="server">
                            <table style="width: 100%; ">
                                <tr>
                                    <td>
                                        <asp:Image ID="imgcompany" runat="server" Width="150px" />

                                    </td>


                                    <td>
                                        <b>
                                            <asp:Label ID="lblcompanyname" runat="server" Text="Bill" /></b><br />
                                        <asp:Label ID="lblfarmname" runat="server" Text="Bill" /><br />
                                    </td>
                                </tr>
                            </table>
                            <table style="width: 100%;" class="example-table" border="1">
                                <tr>
                                    <td>
                                        <asp:Label ID="lblcompaddress" runat="server" Text="" />
                                    </td>
                                    <td>GSTIN
                                    </td>
                                    <td>
                                        <asp:Label ID="lblGSTIN" runat="server" Text="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblcityname" runat="server" Text="" />,
                                        <asp:Label ID="lblstateadd" runat="server" Text="" /></td>
                                    <td>PAN No :-
                                    </td>
                                    <td>
                                        <asp:Label ID="lblcomppanno" runat="server" Text="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Phone No.-
                                        <asp:Label ID="lbllandlineno" runat="server" Text="" /></td>
                                    <td>Email :-
                                    </td>
                                    <td>
                                        <asp:Label ID="lblemail" runat="server" Text="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Mobile No.-
                                        <asp:Label ID="lblcontactno" runat="server" Text="" /></td>
                                    <td>Web :-
                                    </td>
                                    <td>
                                        <asp:Label ID="lblwebsite" runat="server" Text="" />
                                    </td>
                                </tr>
                            </table>
                            <div>BILL TO</div>
                            <table style="width: 100%;" border="1">
                                <tr>

                                    <td style="width: 59%" rowspan="2">
                                        <asp:Label ID="lbllitle" runat="server"></asp:Label>
                                        <asp:Label ID="lblname" runat="server"></asp:Label><br />
                                        <asp:Label ID="lbladdress" runat="server"></asp:Label><br />
                                        <asp:Label ID="lblState" runat="server"></asp:Label><br />
                                        <asp:Label ID="lblmobile" runat="server"></asp:Label><br />
                                        <asp:Label ID="lblclientEmail" runat="server"></asp:Label>

                                    </td>
                                    <td>
                                        <asp:Label ID="date" runat="server" Text="Bill Date  :"></asp:Label>
                                    </td>
                                    <td>

                                        <asp:Label ID="lbldate" runat="server"></asp:Label>

                                    </td>
                                </tr>

                                <tr>
                                    <td style="vertical-align: top;">Invoice No&nbsp;:</td>
                                    <td style="padding-left: 23px">

                                        <asp:Label ID="lblbillnum" runat="server"></asp:Label>

                                    </td>
                                </tr>
                            </table>

                            <asp:GridView ID="gvservices" runat="server" AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true">
                                <Columns>
                                    <asp:TemplateField>
                                        <HeaderStyle Width="50px" Height="20px" />

                                        <HeaderTemplate>
                                            SL#
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <%#Container.DataItemIndex+1 %>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField ItemStyle-CssClass="testL">
                                        <HeaderTemplate>
                                            Service Description
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <%# Eval("Name") %><br />
                                            (  <%# Eval("Description") %>)
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField ItemStyle-CssClass="test" HeaderStyle-HorizontalAlign="Right">
                                        <HeaderTemplate>
                                            Amount(in Rs.)
                                        </HeaderTemplate>

                                        <ItemTemplate>
                                            <asp:Label ID="lblamount" runat="server" Text='<%# Eval("Amount") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <%-- <asp:TemplateField ItemStyle-CssClass="test" HeaderStyle-HorizontalAlign="Right">
                                        <HeaderTemplate>
                                            Tax(in Rs.)
                                        </HeaderTemplate>

                                        <ItemTemplate>
                                            <asp:Label ID="lbltax" runat="server" Text='<%# Eval("TaxAmount") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField ItemStyle-CssClass="test" HeaderStyle-HorizontalAlign="Right">
                                        <HeaderTemplate>
                                         Total Amount(in Rs.)
                                        </HeaderTemplate>

                                        <ItemTemplate>
                                            <asp:Label ID="lblsumamount" runat="server" Text='<%# Eval("TotalsumAmount") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>--%>
                                </Columns>
                            </asp:GridView>
                            <table style="width: 100%;" border="1">
                                <tr id="tr1" runat="server">
                                    <td style="text-align: right" colspan="2">
                                        <asp:Label ID="rstotal" runat="server" Text=" Total amount" Style="text-align: right; padding-right: 60px"></asp:Label>
                                        <asp:Label ID="lbltotal" runat="server" Style="padding-right: 2px"></asp:Label>

                                    </td>
                                </tr>

                                <tr id="trsgst" runat="server" visible="false" style="text-align: right;">
                                    <td style="padding-right: 19px;">SGST 
                                    </td>
                                    <td>
                                        <asp:Label ID="lblsgst" runat="server" Text="0.00"></asp:Label></td>
                                </tr>
                                <tr id="trCGST" runat="server" visible="false" style="text-align: right;">
                                    <td style="padding-right: 19px;">CGST 
                                    </td>
                                    <td>
                                        <asp:Label ID="lblCGST" runat="server" Text="0.00"></asp:Label></td>
                                </tr>
                                <tr id="trIGST" runat="server" visible="false" style="text-align: right;">
                                    <td style="padding-right: 19px;">IGST 
                                    </td>
                                    <td>
                                        <asp:Label ID="lblIGST" runat="server" Text="0.00"></asp:Label></td>
                                </tr>
                                <tr id="trUTGST" runat="server" visible="false" style="text-align: right;">
                                    <td style="padding-right: 19px;">UTGST 
                                    </td>
                                    <td>
                                        <asp:Label ID="lblUTGST" runat="server" Text="0.00"></asp:Label></td>
                                </tr>
                                <%-- <tr id="trAdvMgmt" runat="server" visible="false">
                                    <td style="text-align: right;padding-right: 19px;">
                                        <asp:Label ID="lbladvR" runat="server" Text="Advance Received" ></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblArAmt" runat="server"></asp:Label>
                                    </td>
                                </tr>
                                <tr id="tr3" runat="server">
                                    <td style="text-align: right;padding-right: 19px;">
                                        <asp:Label ID="lblPaidAmtTitle" runat="server" Text="Paid Amount" Visible="false"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblPaidAmt" runat="server" Style="padding-right: 1px" Visible="false"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right;">

                                        <asp:Label ID="lblcardname" runat="server"  Text=""></asp:Label>
                                    </td>
                                </tr>--%>


                                <tr id="tr4" runat="server">

                                    <td style="text-align: right; padding-right: 19px;" width="85%">
                                        <asp:Label ID="lblt" runat="server" Text="Net Amount "></asp:Label>
                                    </td>
                                    <td style="text-align: right">
                                        <asp:Label ID="lblfinalamt" runat="server"></asp:Label></td>
                                </tr>
                                <tr id="tr5" runat="server">

                                    <td style="text-align: right; padding-right: 19px;">
                                        <asp:Label ID="Label2" runat="server" Text="Amount Paid "></asp:Label>
                                    </td>
                                    <td style="text-align: right">
                                        <asp:Label ID="lbladvance" runat="server"></asp:Label></td>
                                </tr>
                                <tr id="tr6" runat="server">

                                    <td style="text-align: right; padding-right: 19px;">
                                        <asp:Label ID="lblpayableamt" runat="server" Text="Total Professional Fees Due "></asp:Label></td>
                                    <td style="text-align: right">

                                        <asp:Label ID="lblpayable" runat="server"></asp:Label></td>
                                </tr>


                            </table>
                            <br />
                        </div>
                        <div style="width: 100%" id="divReceipt" runat="server">
                            <table style="width: 100%;">
                                <tr>
                                    <td c style="text-align: center; font-weight: bold">
                                        <asp:Label ID="lblreceiptTilte" runat="server" Text="RECEIPT" />
                                    </td>

                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblr" runat="server" Text="Receipt Number:"></asp:Label>
                                        <asp:Label ID="lblrecptnNum" runat="server" Text="..."></asp:Label>
                                        <asp:Label ID="reciept" runat="server" Text="Receipt Date:" Style="padding-left: 300px;"></asp:Label>
                                        <asp:Label ID="lblRcdate" runat="server"></asp:Label>
                                    </td>

                                </tr>
                                <tr>
                                    <td>

                                        <asp:Label ID="lblnarr" runat="server" Text="Received with thanks from" />&nbsp
                               
                                   <asp:Label ID="lbtitle" runat="server"></asp:Label>
                                        <asp:Label ID="lblrecepitfrom" runat="server"></asp:Label>
                                        a sum of Rupees
                                       <asp:Label ID="lblammount" runat="server"></asp:Label>
                                        (<asp:Label ID="lblSumofRupees" runat="server"></asp:Label>)

                                        <asp:Label ID="lblfor" runat="server" Text="against Bill No "></asp:Label><asp:Label ID="lblbla" runat="server"></asp:Label>
                                        <asp:Label ID="lblto" runat="server" Text="to" Visible="false" />

                                        <asp:Label ID="lbtitleRefund" runat="server" Visible="false" />
                                        <asp:Label ID="lblrecepitfromRefund" runat="server" Visible="false"></asp:Label>

                                        <asp:Label ID="lblCompAccountMsg" runat="server" />
                                    </td>
                                </tr>
                            </table>

                        </div>

                        <div style="height: 100px; text-align: right; height: 50px;">
                            <asp:Label ID="lblsignature" runat="server" Style="text-align: right"></asp:Label>

                            <br />
                            <br />
                            <asp:Label ID="lblDesignation" runat="server" Text="Authorized Signatory" Style="text-align: right"></asp:Label>
                        </div>
                    </div>
                    <div id="dvtermcondition" runat="server">
                    </div>
                    <%--<table style="width: 100%; font-size: 11px;">
                        <tr>
                            <td></td>
                        </tr>
                        <tr>
                            <td>1. Payment terms : The payments should be released within seven day's of completion of the activity.</td>
                        </tr>
                        <tr>
                            <td>2. All Cheque should be drawn " A/C payee" only & in favour of
                                <asp:Label ID="lblcompname" runat="server" Style="font-weight: bold;"></asp:Label>
                                only.
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblpanno" runat="server" Style="font-weight: bold;"></asp:Label></td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblactno" runat="server" Style="font-weight: bold;"></asp:Label>
                                <asp:Label ID="lblbankname" runat="server" Style="font-weight: bold;"></asp:Label>,
                                <asp:Label ID="lblstaten" runat="server" Style="font-weight: bold;"></asp:Label>
                                <asp:Label ID="lblifsc" runat="server" Style="font-weight: bold;"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                        </tr>
                    </table>--%>
                </div>
            </asp:Panel>
        </div>
        <br />
        <br />
        <div style="margin-left: 40%;">

            <asp:ImageButton ID="btnprint" runat="server" CssClass="" ImageUrl="~/images/print_button.png" Width="100" OnClientClick="javascript:PrintPage();" />
            <asp:Button ID="btbBack" runat="server" CssClass="btn" Text="Back" OnClick="btnBack_Click" /><br />
            <asp:Button ID="btngenpdf" runat="server" CssClass="btn" Text="Send Mail with attachement" OnClick="btngenpdf_Click" />

        </div>
        <asp:Label ID="lblmsg" runat="server" />

        <asp:HiddenField ID="hdnclientemail" runat="server" />

        <asp:HiddenField ID="hdncleitnid" runat="server" />

        <asp:HiddenField ID="hdnIsunionter" runat="server" />
        <asp:HiddenField ID="hdncompstateID" runat="server" />
        <asp:HiddenField ID="hdnClientstateID" runat="server" />
        <asp:HiddenField ID="hdnpassword" runat="server" />

 

 int userid = 0;
        int taskID = 0;
        string BillNo = string.Empty;
        string RecptNo = string.Empty;
        string clienttype = string.Empty;
        DataTable dtservice;
        void Page_Init(object sender, EventArgs e)
        {

            if (Request.QueryString.AllKeys.Contains("clientID"))
            {
                int id;
                int.TryParse(Request.QueryString["clientID"], out id);
                hdncleitnid.Value = id.ToString();
                clienttype = Request.QueryString["clienttype"];
                BillNo = Request.QueryString["InvoiceNo"];
                RecptNo = Request.QueryString["recpNo"];
                if (string.IsNullOrEmpty(RecptNo))
                {
                    divReceipt.Visible = false;
                }
                ViewState["ClientID"] = id;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //  Addcolumnsservices();
                GetUserInformation();
                GetBillDetailsByRecpt();


            }

        }
        private void GetUserInformation()
        {
            // ViewState["ClientID"] = hdncleitnid.Value;
            string strConnString = ConfigurationManager.ConnectionStrings["MSIS_SQLConnection"].ConnectionString;
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(strConnString))
            {
              //  string strQuery = "select u.Address,U.Name,U.Mobile,u.EmailID, ud.NAME  as State,U.State as StateID,UD.Isunionterritories from ClientMaster U join STATE UD on U.State = ud.ID where U.ID =" + ViewState["ClientID"] + "";
                string strQuery = "";
                if (clienttype== "NEW")
                {
                    strQuery = "select u.Address,U.Name,U.Mobile,u.City,u.Email as EmailID ,u.State as StateID,UD.Isunionterritories, ud.NAME  as State from NewClient U left outer join STATE UD on U.State = ud.ID where U.ID  =" + hdncleitnid.Value + "";
                }
                else
                {
                    strQuery = "select u.Address,U.Name,U.Mobile,u.City,u.EmailID ,u.State as StateID,UD.Isunionterritories, ud.NAME  as State from ClientMaster U left outer join STATE UD on U.State = ud.ID where U.ID =" + hdncleitnid.Value + "";
                }
                
                SqlCommand cmd = new SqlCommand(strQuery);
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        lblname.Text = dt.Rows[0]["Name"].ToString();
                        lbladdress.Text = dt.Rows[0]["Address"].ToString();
                        lblmobile.Text = "Mobile No. : " + dt.Rows[0]["Mobile"].ToString();
                        lblState.Text = dt.Rows[0]["State"].ToString();
                        lblbillnum.Text = BillNo;
                        lblrecptnNum.Text = RecptNo;
                        hdnClientstateID.Value = dt.Rows[0]["StateID"].ToString();
                        hdnclientemail.Value = dt.Rows[0]["EmailID"].ToString();
                        lblclientEmail.Text = " Email : " + dt.Rows[0]["EmailID"].ToString();
                        hdnIsunionter.Value = dt.Rows[0]["Isunionterritories"].ToString();

                    }
                }
            }

            //  lblinvoiceno.Text = recptNo;
        }

        private void GetBillDetailsByRecpt()
        {
            TaskListBL taskListBL = new TaskListBL();
            DataSet dsTaskList = taskListBL.GetBillDetailsByRecpt(BillNo, RecptNo);
            if (dsTaskList.Tables[0].Rows.Count > 0)
            {
                gvservices.DataSource = dsTaskList;
                gvservices.DataBind();

                DataRow row = dsTaskList.Tables[0].Rows[0];
                hdncompstateID.Value = row["State"].ToString();
                lblRcdate.Text = lbldate.Text = row["GenerateDate"].ToString();
                lblcompanyname.Text = row["CompanyName"].ToString();
                lblfarmname.Text = row["FarmName"].ToString();
              //  string host = HttpContext.Current.Request.Url.Authority;
                String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
                String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
                imgcompany.ImageUrl = strUrl + "images/CopmanyLogo/" + row["LogoName"].ToString();
                lblcityname.Text = row["City"].ToString();
                lblstateadd.Text = lblState.Text = row["StateName"].ToString();

                // lblcomppanno.Text= lblpanno.Text = row["PANNo"].ToString();
                lblGSTIN.Text = row["ServiceTaxNo"].ToString();
                dvtermcondition.InnerHtml = row["TermsContidion"].ToString();
                //  lblactno.Text = "A/C No. " + row["ACTNO"].ToString();
                //  lblbankname.Text = " held with " + row["BankName"].ToString();
                lblcompaddress.Text = row["Address"].ToString();
                lblemail.Text = row["Email"].ToString();
                lblcontactno.Text = row["ContactNo"].ToString();
                hdnpassword.Value = row["Password"].ToString();
                lblwebsite.Text = "Website : " + row["Website"].ToString();
                //lblbillfrom.Text = row["CompanyName"].ToString();
                decimal total = 0;
                // Label lbltotalamt = (Label)gridBdetails.FooterRow.FindControl("lbltotalamt");
                foreach (GridViewRow gridViewRow in gvservices.Rows)
                {

                    Label totalval = (Label)gridViewRow.FindControl("lblamount");

                    decimal rowValue = 0;
                    if (decimal.TryParse(totalval.Text.Trim(), out rowValue))
                        total += rowValue;
                }
                lbltotal.Text = total.ToString();
                // lbltotal.Text = row["TotalAmount"].ToString();
                lblsgst.Text = dsTaskList.Tables[0].Compute("SUM(SGST)", string.Empty).ToString();// row["SGST"].ToString();
                lblCGST.Text = dsTaskList.Tables[0].Compute("SUM(CGST)", string.Empty).ToString();// row["CGST"].ToString();
                lblIGST.Text = dsTaskList.Tables[0].Compute("SUM(IGST)", string.Empty).ToString();// row["IGST"].ToString();
                lblUTGST.Text = dsTaskList.Tables[0].Compute("SUM(UTGST)", string.Empty).ToString();// row["UTGST"].ToString();
                lblfinalamt.Text = row["FinalValue"].ToString();
                decimal ammount = Convert.ToDecimal(row["FinalValue"]);
                int amt = Convert.ToInt32(ammount);
                string mywrd = AppUtility.NumbersToWords(amt);
                lblSumofRupees.Text = mywrd + " only";
                lblbla.Text = BillNo;
                lbladvance.Text = row["AdvanceAmount"].ToString();
                lblpayable.Text = row["payableamt"].ToString();
                lblsignature.Text = row["CompanyName"].ToString();
                //lblifsc.Text = " (IFSC Code # " + row["IFSCCode"].ToString() + " )";
                DisplayTaxType();
            }
        }
        protected void btngenpdf_Click(object sender, EventArgs e)
        {
           
            btngenpdf.Visible = false;
            btbBack.Visible = false;
            btnprint.Visible = false;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + lblbillnum.Text + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnlPerson.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            string host = HttpContext.Current.Request.Url.Host;
            
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);

                pdfDoc.Close();
                //Response.Write(pdfDoc);
                //Response.End();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();

                //   MailMessage mm = new MailMessage("info@cabmsa.in","singh.satan@gmail.com");// hdnclientemail.Value
                MailMessage mm = new MailMessage(lblemail.Text, hdnclientemail.Value);
                mm.To.Add("satansingh.jsp@gmail.com");
                mm.Subject = "Invoce attachement";
                mm.Body = "Invoice Attachment";
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes), lblbillnum.Text + ".pdf"));
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                string smtpaddress = "";
                // string hostdomain = "";
                int portaddress = 0;
                string words = lblemail.Text.Split('@')[1];

                // var valu = word[1];
                // Console.WriteLine(word);

                if (words.ToLower() == "gmail.com")
                {
                    smtpaddress = "smtp.gmail.com";
                    portaddress = 587;
                    smtp.EnableSsl = true;
                }
                else
                {
                    smtpaddress = "mail." + words.ToLower() + "";
                    portaddress = 25;
                    smtp.EnableSsl = false;
                };
                smtp.Host = smtpaddress;
                smtp.UseDefaultCredentials = false;
               // 
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                //NetworkCred.UserName = "info@cabmsa.in";
                //NetworkCred.Password = "welcome@123";
                NetworkCred.UserName = lblemail.Text;
                NetworkCred.Password = hdnpassword.Value;
                smtp.UseDefaultCredentials = false;
                
                smtp.Credentials = NetworkCred;
                smtp.Port = portaddress;
                smtp.Send(mm);
            }
            //pdfDoc.Open();
            //htmlparser.Parse(sr);
            //pdfDoc.Close();
            //Response.Write(pdfDoc);
            //Response.End();
            btngenpdf.Visible = true;
            btbBack.Visible = true;
            btnprint.Visible = true;
        }
        protected void btngenpdf_Click1(object sender, EventArgs e)
        {
            ServicesBL servicesBL = new ServicesBL();
            string strInvoiceNo = string.Empty;
            strInvoiceNo = BillNo;
            TaskListBL taskListBL = new TaskListBL();
            DataSet dsState = taskListBL.GetBillDetailsByRecpt(BillNo, RecptNo);
            if (dsState.Tables[1] != null && dsState.Tables[1].Rows.Count > 0)
            {
                DataTable dt = dsState.Tables[1];
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        StringBuilder sb = new StringBuilder();

                        //Generate Invoice (Bill) Header.
                        sb.Append("<table style='width: 100%;border: 1px solid Black;border-spacing: 0;font-size:14px'>");

                        sb.Append(" <tr><td  style='text-align:center;'>");
                        sb.Append("<b style='font-size:25px;'>" + lblcompanyname.Text + "  </b><br>");
                        sb.Append("<b style='font-size:20px;'>" + lblfarmname.Text + "</b><br>");
                        sb.Append(" <b>" + lblcompaddress.Text + ",");
                        sb.Append(lblcityname.Text + ",");
                        sb.Append(lblstateadd.Text + ",<br>");
                        sb.Append(lblcontactno.Text + ",");
                        sb.Append(lblemail.Text + ",<br>");
                        sb.Append(lblwebsite.Text + "</b>");
                        sb.Append("</td></tr>");

                        sb.Append("<tr><td style='border:1px solid Black; width: 70%;vertical-align: top' colspan='2'>");
                        sb.Append("<table style='width: 100%; border:1px solid Black;'>");
                        sb.Append("<tr><td>Name :</td>");
                        sb.Append("<td style='width: auto'><b>" + lbllitle.Text + " " + lblname.Text + "</b></td></tr>");
                        sb.Append("<tr><td style='font-size: 14px'>Address : </td><td>" + lbladdress.Text + "</td> </tr>");
                        sb.Append(" <tr><td style='width: 100px; font-size: 14px'>Mobile No:</td><td>" + lblmobile.Text + "></td></tr>");
                        sb.Append("<tr><td style='width: 100px; font-size: 14px'>State:</td><td>" + lblState.Text + "</td></tr>");
                        sb.Append("</table></td>");

                        sb.Append(" <td style='vertical-align: top; border-bottom: 1px solid Black; width: 80%;' colspan='3'>");
                        sb.Append("<table style='width: 100%; font-size: 14px; border-spacing: 0;'>");
                        sb.Append("<tr><td>");
                        sb.Append("<table style='border-spacing: 0px'>");
                        sb.Append("<tr><td style='vertical-align: top; font-size: 14px;'>Invoice No&nbsp;:</td>");
                        sb.Append("<td style='padding-left: 23px'><b>" + lblbillnum.Text + "</b></td></tr>");
                        sb.Append("<tr><td>Bill Date  :</td><td style='padding-left: 23px'><b>" + lbldate.Text + "</b></td></tr>");
                        // sb.Append("<tr><td>Bill From:</td><td style='padding-left: 23px'>"+ lblbillfrom.Text +"</td>");
                        sb.Append("</tr></table></td></tr></table></td></tr>");








                        sb.Append("<tr><td colspan = '2'></td></tr>");
                        sb.Append("<tr><td><b>Invoice No: </b>");
                        sb.Append(strInvoiceNo);
                        sb.Append("</td><td align = 'right'><b>Date: </b>");
                        sb.Append(DateTime.Now);
                        sb.Append(" </td></tr>");
                        sb.Append("<tr><td colspan = '2'><b>Company Name: </b>");
                        sb.Append("satan");
                        sb.Append("</td></tr>");
                        sb.Append("</table>");
                        sb.Append("<br />");

                        //Generate Invoice (Bill) Items Grid.
                        sb.Append("<table border = '1'>");
                        sb.Append("<tr>");
                        foreach (DataColumn column in dt.Columns)
                        {
                            sb.Append("<th style = 'bgcolor: #D20B0C;color:#ffffff'>");
                            sb.Append(column.ColumnName);
                            sb.Append("</th>");
                        }
                        sb.Append("</tr>");
                        foreach (DataRow row in dt.Rows)
                        {
                            sb.Append("<tr>");
                            foreach (DataColumn column in dt.Columns)
                            {
                                sb.Append("<td>");
                                sb.Append(row[column]);
                                sb.Append("</td>");
                            }
                            sb.Append("</tr>");
                        }
                        sb.Append("<tr><td align = 'right'>");
                        sb.Append(dt.Columns.Count - 1);
                        sb.Append("'>Total</td>");
                        sb.Append("<td>");
                        //sb.Append(dt.Compute("sum(Total)", ""));

                        sb.Append(dt.Rows[0]["TotalAmount"].ToString());
                        sb.Append("</td>");
                        sb.Append("</tr></table>");
                        sb.Append(" </td></tr></table>");
                        //Export HTML String as PDF.
                        StringReader sr = new StringReader(sb.ToString());
                        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
                            pdfDoc.Open();
                            htmlparser.Parse(sr);

                            pdfDoc.Close();

                            byte[] bytes = memoryStream.ToArray();
                            memoryStream.Close();



                            MailMessage mm = new MailMessage("info@eagleinnovations.co.in", "singh.satan@gmail.com");
                            mm.Subject = "QuickeServices";
                            mm.Body = "Invoice Attachment";
                            mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Invoice.pdf"));
                            mm.IsBodyHtml = true;
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = "mail.eagleinnovations.co.in";
                            smtp.EnableSsl = true;
                            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                            NetworkCred.UserName = "info@eagleinnovations.co.in";
                            NetworkCred.Password = "welcome@123";
                            smtp.UseDefaultCredentials = false;
                            smtp.EnableSsl = false;
                            smtp.Credentials = NetworkCred;
                            smtp.Port = 25;
                            smtp.Send(mm);
                        }
                    }
                }
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
        private void DisplayTaxType()
        {


            if (hdnClientstateID.Value == hdncompstateID.Value)
            {
                trsgst.Visible = true;
                trCGST.Visible = true;
                trIGST.Visible = false;
                trUTGST.Visible = false;
            }
            else if (hdnIsunionter.Value == "1")
            {
                trUTGST.Visible = true;
                trsgst.Visible = false;
                trCGST.Visible = false;
                trIGST.Visible = false;
            }
            else
            {
                trUTGST.Visible = false;
                trsgst.Visible = false;
                trCGST.Visible = false;
                trIGST.Visible = true;
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("givenServices.aspx");
        }

 

<style>
        hr {
            display: block;
            -webkit-margin-before: 0.5em;
            -webkit-margin-after: 0.5em;
            -webkit-margin-start: auto;
            -webkit-margin-end: auto;
        }

        .test {
            text-align: right;
        }

        .testL {
            text-align: left;
        }
    </style>
    <script type="text/javascript">
        function PrintPage() {
            document.getElementById('<%=btnprint.ClientID%>').style.visibility = "hidden";
            document.getElementById('<%=btbBack.ClientID%>').style.visibility = "hidden";
            window.print();
        }
    </script>
    <style type="text/css">
        table {
            border-collapse: collapse;
        }

        th, td {
            border: 1px solid #ccc;
            padding-left: 10px;
        }

        tr:nth-child(even) {
            background-color: #fff;
        }

        tr:nth-child(odd) {
            background-color: #fff;
        }
    </style>

 

Posted 7 months ago

With iTextSharp Style will not come. It is basic Library and does not support CSS3