How to Create Barcode with Size of Label Printer in PDF WinForms?
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, create forms, compress, and secure PDF files.
Using this library, you can create a barcode with the size of a printer by resizing the page to the printer size. This sample explains how to create a barcode with the size of a label printer in C# and VB.NET.
Steps to create a barcode with the size of a label printer programmatically using C#:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the 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
- Include the following code snippet in the main method of the Program.cs file to create a label-size barcode on a 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 inches to points according to the size of the 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 the 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 the 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 the size of the 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 the 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 the 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 a barcode with the size of a label printer can be downloaded from LabelSizeBarcode.zip.
By executing the program, you will get the PDF document as follows.
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 of 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 the creation of a barcode.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.