Articles in this section
Category / Section

How to create barcode with size of label printer in C# and VB.NET?

4 mins read

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.

Using this library, you can create barcode with size of a printer by resizing the page to printer size. This sample explains how to create barcode with size of a label printer in C# and VB.NET.

Steps to create barcode with size of label printer programmatically using C#:

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

C#

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

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Include the following code snippet in main method of Program.cs file to create label size barcode on PDF page.

C#

//Create a new PDF Document 
PdfDocument document = new PdfDocument();
 
//Set the orientation and margins for document pages
document.PageSettings.Orientation = PdfPageOrientation.Landscape;
document.PageSettings.SetMargins(0, 0);
 
//Initialize unit converter
PdfUnitConvertor converter = new PdfUnitConvertor();
 
//Convert inchs to points according to size of label printer
float pageHeight = converter.ConvertUnits(2, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
float pageWidth = converter.ConvertUnits(1, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
 
//Set the size for document pages
document.PageSettings.Size = new SizeF(pageWidth, pageHeight);
 
//Add a new page to the document
PdfPage page = document.Pages.Add();
 
//Create PdfGraphics for the page
PdfGraphics graphics = page.Graphics;
 
//Draw the string on PDF page
graphics.DrawString("Label Size Barcode", new PdfStandardFont(PdfFontFamily.Helvetica, 7), PdfBrushes.DarkBlue, new PointF(43, 5));
 
//Initialize PdfCode39Barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
 
//Set the text and bar height
barcode.Text = "CODE39$";
barcode.BarHeight = 10;
 
//Draw the barcode on PDF page
barcode.Draw(page, new PointF(10, 30));
 
//Save the PDF document
document.Save("Barcode.pdf");
 
//Close the instance of PdfDocument
document.Close(true);

 

VB.NET

'Create a new PDF Document 
Dim document As PdfDocument = New PdfDocument
 
'Set the orientation and margins for document pages
document.PageSettings.Orientation = PdfPageOrientation.Landscape
document.PageSettings.SetMargins(0, 0)
 
'Initialize unit converter
Dim converter As PdfUnitConvertor = New PdfUnitConvertor
 
'Convert inches to points according to size of label printer
Dim pageHeight As Single = converter.ConvertUnits(2, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
Dim pageWidth As Single = converter.ConvertUnits(1, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
 
'Set the size for document pages
document.PageSettings.Size = New SizeF(pageWidth, pageHeight)
 
'Add a new page to the document
Dim page As PdfPage = document.Pages.Add
 
'Create PdfGraphics for the page
Dim graphics As PdfGraphics = page.Graphics
 
'Draw the string on PDF page
graphics.DrawString("Label Size Barcode", New PdfStandardFont(PdfFontFamily.Helvetica, 7), PdfBrushes.DarkBlue, New PointF(43, 5))
 
'Initialize PdfCode39Barcode
Dim barcode As PdfCode39Barcode = New PdfCode39Barcode
 
'Set the text and bar height
barcode.Text = "CODE39$"
barcode.BarHeight = 10
 
'Draw the barcode on PDF page
barcode.Draw(page, New PointF(10, 30))
 
'Save the PDF document
document.Save("Barcode.pdf")
 
'Close the instance of PdfDocument
document.Close(True)

 

A complete work sample to create barcode with size of a label printer can be downloaded from LabelSizeBarcode.zip.

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

Barcode with size of label printer

Take a moment to peruse the documentation, where you will find other options like drawing one and two dimensional barcodes in PDF document, set location and size to the barcode, Exporting Barcode as Image and customizing the barcode appearance.

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

An online sample link for creation of barcode.

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 sign in to leave a comment
Access denied
Access denied