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, 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#:
- 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 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 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.
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.
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.