How to add padding and margin inbetween gridview cells while printing in PDF?

Donald Symmons 2,856 Reputation points

My gridview PDF print does not appear good when I printed it. How do I add margin and padding I order to create some horizontal and vertical space in the cells and make it look good?

The way it appears below is just too compact, no vertical space between the valuesgrid print


<asp:GridView ID="GridView1" runat="server" GridLines="None" DataKeyNames="Id" AllowPaging="true" PageSize="10" HeaderStyle-ForeColor="#05214d" HeaderStyle-Font-Size="10pt" Font-Size="10pt"
                                    AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" HeaderStyle-HorizontalAlign="left" RowStyle-HorizontalAlign="Left" OnPageIndexChanging="OnPageIndexChanging" HeaderStyle-BackColor="#f5f5f5" RowStyle-BackColor="#fdfdfd" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" class="table" Width="100%">
                                        <div style="text-align: center; font-weight: 500; margin-top: 2%;">
                                            <i class="fal fa-file-times" style="margin: 0 auto; font-size: 30pt; color: #145c7c;"></i>
                                            <p id="P1" runat="server" style="font-size: 11pt; font-weight: 400;">No Record</p>
                                                <asp:CheckBox ID="check" runat="server" />
                                        <asp:BoundField DataField="Id" HeaderText="ID" HeaderStyle-Font-Bold="false" />
                                        <asp:BoundField DataField="RegName" HeaderText="Name" HeaderStyle-Font-Bold="false" ItemStyle-Font-Size="10pt" />
                                        <asp:BoundField DataField="phoneNumber" HeaderText="Phone Number" HeaderStyle-Font-Bold="false" ItemStyle-Font-Size="10pt" />
                                        <asp:BoundField DataField="email" HeaderText="Email" HeaderStyle-Font-Bold="false" ItemStyle-Font-Size="10pt" />
                                        <asp:BoundField DataField="RegDate" HeaderText="Date Registered" HeaderStyle-Font-Bold="false" ItemStyle-Font-Size="10pt" />

Gridview PDF print

 protected void ExportToPDF(object sender, EventArgs e)
            string fileName = Topic.Text + ".pdf";
            using (StringWriter sw = new StringWriter())
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    GridView1.AllowPaging = false;

                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

                    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
                    cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ";");
        public override void VerifyRenderingInServerForm(Control control)
            /* Verifies that the control is rendered */
Microsoft Technologies based on the .NET software framework.
3,525 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
3,363 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,482 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 27,721 Reputation points Microsoft Vendor

    Hi @Donald Symmons,

    You can set the GridView.CellPadding property directly.

    GridView.CellPadding Property:Gets or sets the amount of space between the contents of a cell and the cell's border.

    You can also set the GridView.CellSpacing property at the same time.

    GridView.CellSpacing Property:Gets or sets the amount of space between cells.

    <asp:GridView ID="GridView1" runat="server" CellPadding="10" CellSpacing="5" 

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful