Articles in this section
Category / Section

How to remove top and bottom borders in PDF table using C# and VB.NET

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can remove top and bottom borders in PDF table using C# and VB.NET.

Steps to remove top and bottom borders in PDF table programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Grid
Imports System.Drawing

 

  1. Use the following code snippet to remove top and bottom borders in PDF table.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add a page to the document
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Add columns
pdfGrid.Columns.Add(4);
string[] name = new string[3] { "Clay", "John", "Simons" };
//Add header
PdfGridRow header = pdfGrid.Rows.Add();
header.Cells[0].Value = "ID";
header.Cells[1].Value = "Name";
header.Cells[2].Value = "Type";
header.Cells[3].Value = "Date";
//Add style
header.Style.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12, PdfFontStyle.Bold);
//Add rows to the table
for (int i = 0; i < 3; i++)
{
    PdfGridRow row = pdfGrid.Rows.Add();
    row.Cells[0].Value = i.ToString();
    row.Cells[1].Value = name[i];
    row.Cells[2].Value = "AAA";
    row.Cells[3].Value = DateTime.Now.ToString();
}
//Create and customize the string formats
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Middle;
//Specify the style for PdfGridCell
PdfGridCellStyle cellStyle = new PdfGridCellStyle();
cellStyle.Borders.Bottom.Color = new PdfColor(Color.Transparent);
cellStyle.Borders.Top.Color = new PdfColor(Color.Transparent);
cellStyle.StringFormat = format;
for (int i = 0; i < pdfGrid.Rows.Count; i++)
{
    for (int j = 0; j < pdfGrid.Rows[i].Cells.Count; j++)
    {
        //Apply style
        pdfGrid.Rows[i].Cells[j].Style = cellStyle;                                 
    }
}
//Draw the PdfGrid
pdfGrid.Draw(pdfPage, PointF.Empty);
//Save the document
pdfDocument.Save("Table.pdf");
//Close the document
pdfDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Table.pdf");

 

VB.NET

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add a page to the document
Dim pdfPage As PdfPage = pdfDocument.Pages.Add()
'Create a new PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid()
'Add columns
pdfGrid.Columns.Add(4)
Dim name As String() = New String(2) {"Clay", "John", "Simons"}
'Add header
Dim header As PdfGridRow = pdfGrid.Rows.Add()
header.Cells(0).Value = "ID"
header.Cells(1).Value = "Name"
header.Cells(2).Value = "Type"
header.Cells(3).Value = "Date"
'Add style
header.Style.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 12, PdfFontStyle.Bold)
'Add rows to the table
For i As Integer = 0 To 3 - 1
    Dim row As PdfGridRow = pdfGrid.Rows.Add()
    row.Cells(0).Value = i.ToString()
    row.Cells(1).Value = name(i)
    row.Cells(2).Value = "AAA"
    row.Cells(3).Value = DateTime.Now.ToString()
Next
'Create and customize the string formats
Dim format As PdfStringFormat = New PdfStringFormat()
format.Alignment = PdfTextAlignment.Center
format.LineAlignment = PdfVerticalAlignment.Middle
 
'Specify the style for PdfGridCell
Dim cellStyle As PdfGridCellStyle = New PdfGridCellStyle()
cellStyle.Borders.Bottom.Color = New PdfColor(Color.Transparent)
cellStyle.Borders.Top.Color = New PdfColor(Color.Transparent)
cellStyle.StringFormat = format
 
For i As Integer = 0 To pdfGrid.Rows.Count - 1
 
    For j As Integer = 0 To pdfGrid.Rows(i).Cells.Count - 1
        'Apply style
        pdfGrid.Rows(i).Cells(j).Style = cellStyle
    Next
Next
'Draw the PdfGrid
pdfGrid.Draw(pdfPage, PointF.Empty)
'Save the document
pdfDocument.Save("Table.pdf")
'Close the document
pdfDocument.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Table.pdf")

 

A complete working sample can be downloaded from RemoveTableBorderSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot

Take a moment to peruse the documentation, where you can find options like creating a table using PdfLightTable and PdfGrid, row customization in PdfLightTable and PdfGrid, table pagination and built-in table styles to PdfGrid with code examples.

Refer here to learn more about PDF tables.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied