[Solved] HTML table Background color not appearing in exported PDF using iTextSharp in ASP.Net

on Sep 15, 2021 11:05 PM

I have this pdf page i got through Itext pdf, but it is not coming out well.

The html table does not appear with their background colour in pdf file, it is just plain table background in the original  page.

Lastly the text are clumsy, it is not looking professional at all.

Please help 

My code 

    #customers {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;

        #customers td, #customers th {
            border: 1px solid #ddd;
            padding: 8px;

        #customers tr:nth-child(even) {

        #customers th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #4CAF50;
            color: white;

    #customers0 {
        font-family: Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 100%;

    table.roundedCorners {
        border: 1px solid DarkOrange;
        border-radius: 13px;
        border-spacing: 0;

        table.roundedCorners td,
        table.roundedCorners th {
            border-bottom: 1px solid DarkOrange;
            padding: 4px;

        table.roundedCorners tr:last-child > td {
            border-bottom: none;
<asp:Panel ID="Panel2" runat="server">
    <table align="center" height="70%" style="width: 1100px;">
            <td align="center">
                <asp:Button ID="btnExport" runat="server" Text="EXPORT TO PDF" OnClick="ExportToPDF" />
                <asp:Button ID="btnExport0" runat="server" OnClick="PRINT" Text="PRINT" Width="154px" />
                    ID="Label25" runat="server" Visible="False"></asp:Label>
                <asp:Label ID="Label26" runat="server" Visible="False"></asp:Label>
                <asp:Label ID="Label27" runat="server" Visible="False"></asp:Label>
                <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False"
                    Font-Names="Arial Narrow"
                    Font-Size="13px" DataKeyNames="unit"
                    Width="100%" Style="margin-bottom: 0px"
                    CssClass="grid" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                        <asp:BoundField DataField="unit" HeaderText="UNIT" />
                        <asp:BoundField DataField="colour" HeaderText="COLOUR" />
                        <asp:BoundField DataField="inkcoverage" HeaderText="INK.COV" />
                        <asp:BoundField DataField="inkcode" HeaderText="INK CODE" />
                        <asp:BoundField DataField="posino" HeaderText="POSI NO" />
                        <asp:BoundField DataField="referencecolour" HeaderText="REF.COLOUR" />
                        <asp:BoundField DataField="screen" HeaderText="SCREEN/ANGLE/LENS SURF" />
                        <asp:BoundField DataField="stylusangle" HeaderText="STYLUS ANGLE" />
                        <asp:BoundField DataField="depth" HeaderText="DEPTH" />
                        <asp:BoundField DataField="xdiagonal" HeaderText="XDIAGONAL/BRD SURF" />
                        <asp:BoundField DataField="stretchfactor" HeaderText="STRETCHFACTOR" />
                        <asp:BoundField DataField="RAVINGtype" HeaderText="RAVING" />
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />


protected void ExportToPDF(object sender, EventArgs e)
    using (StringWriter sw = new StringWriter())
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Sep 16, 2021 04:01 AM
on Sep 16, 2021 04:03 AM