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

Donald Symmons 2,856 Reputation points
2023-05-16T04:37:25.45+00:00

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

Gridview

<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%">
                                    <EmptyDataTemplate>
                                        <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>
                                        </div>
                                    </EmptyDataTemplate>
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="check" runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <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" />
                                    </Columns>
                                </asp:GridView>

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;
                    this.GetRegistrationDetails();

                    GridView1.RenderControl(hw);
                    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);
                    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
                    cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
                    pdfDoc.Open();
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                    pdfDoc.Close();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ";");
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Write(pdfDoc);
                    Response.End();
                }
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,525 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,363 questions
C#
C#
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
    2023-05-16T06:21:42.2533333+00:00

    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