Articles in this section
Category / Section

How to draw a barcode in a PDF table cell

6 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw a barcode in a PDF table cell.

Steps to draw a barcode in a PDF table cell programmatically:

  1. Create a new C# console application project.

Create a console application

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

NuGet package reference

  1. Include the following namespace in the Program.cs file.

C#

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

 

VB.NET

Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Grid
Imports Syncfusion.Pdf.Barcode

 

  1. Use the following code snippet to draw a barcode in a PDF table cell.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add a page
PdfPage page = pdfDocument.Pages.Add();
//Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();
//Create a DataTable
DataTable dataTable = new DataTable();
//Add columns to the DataTable
dataTable.Columns.Add("Barcode Text");
dataTable.Columns.Add("Barcode");
//Add rows to the DataTable
dataTable.Rows.Add(new object[] { "CODE39$", "" });
//Assign data source
pdfGrid.DataSource = dataTable;
//Set height 
pdfGrid.Rows[0].Height = 70;
pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout;
//Draw grid to the page of PDF document
pdfGrid.Draw(page, new PointF(10, 10));
//Save the document
pdfDocument.Save("Output.pdf");
//Close the document
pdfDocument.Close(true);
//This will open the PDF file so, the result will be seen in the default PDF viewer
Process.Start("Output.pdf");
 
/// <summary>
/// Event handler to draw barcode in grid cell 
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
    if(args.RowIndex==0 && args.CellIndex==1 && !args.IsHeaderRow)
    {
        //Drawing Code39 barcode 
        PdfCode39Barcode barcode = new PdfCode39Barcode();
 
        //Setting height of the barcode 
        barcode.BarHeight = 45;
        barcode.Text = "CODE39$";
 
        //Draw barcode with respect to the grid cell bounds
        barcode.Draw(page, new PointF(args.Bounds.X+5, args.Bounds.Y+5));
    }
}

 

VB.NET

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add a page
Dim page As PdfPage = pdfDocument.Pages.Add()
'Create a new PdfGrid
Dim pdfGrid As PdfGrid = New PdfGrid()
'Create a DataTable
Dim dataTable As DataTable = New DataTable()
'Add columns to the DataTable
dataTable.Columns.Add("Barcode Text")
dataTable.Columns.Add("Barcode")
'Add rows to the DataTable
dataTable.Rows.Add(New Object() {"CODE39$", ""})
'Assign data source
pdfGrid.DataSource = dataTable
'Set height 
pdfGrid.Rows(0).Height = 70
AddHandler pdfGrid.BeginCellLayout, AddressOf PdfGrid_BeginCellLayout
'Draw grid to the page of PDF document
pdfGrid.Draw(page, New PointF(10, 10))
'Save the document
pdfDocument.Save("Output.pdf")
'Close the document
pdfDocument.Close(True)
'This will open the PDF file so, the result will be seen in the default PDF viewer
Process.Start("Output.pdf")
 
 
''' <summary>
''' Event handler to draw barcode in grid cell 
''' </summary>
''' <param name="sender"></param>
''' <param name="args"></param>
Private Sub PdfGrid_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs)
    If args.RowIndex = 0 AndAlso args.CellIndex = 1 AndAlso Not args.IsHeaderRow Then
        'Drawing Code39 barcode 
        Dim barcode As PdfCode39Barcode = New PdfCode39Barcode()
        'Setting height of the barcode 
        barcode.BarHeight = 45
        barcode.Text = "CODE39$"
        'Draw barcode with respect to the grid cell bounds
        barcode.Draw(page, New PointF(args.Bounds.X + 5, args.Bounds.Y + 5))
    End If
End Sub

 

A complete working sample can be download from DrawBarcodeInTable.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 the options like creating the table using the PdfLightTable and PdfGrid, cell, and row customization in the PdfLightTable and PdfGrid, built-in table styles to PdfGrid, pagination in PdfGrid, and PdfLightTable, and add the one-dimensional barcode, Add two-dimensional barcode, export barcode as an image, and customize barcode appearance.

 

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Note:

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

 

See Also:

Add barcode in the PDF document

Create a table in a PDF file

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