How to align barcode text in WinForms PDF using C# and VB.NET?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can align barcode text in the PDF document using C# and VB.NET.
Steps to align barcode text in PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.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
- Use the following code snippet in the click event of the button to align barcode text in the PDF document.
C#
//Creating a new PDF document PdfDocument document = new PdfDocument(); document.PageSettings.SetMargins(10f); //Adding new page to the PDF document PdfPage page = document.Pages.Add(); //Barcode size SizeF size = new SizeF(250, 100); //Create a new instance for Code39 barcode PdfCode39Barcode code39 = new PdfCode39Barcode(); code39.Text = "CODE39$"; //Set center alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Center; //Setting size of the barcode code39.Size = size; //Draw barcode into the PDF page code39.Draw(page, new PointF(30, 30)); //Set left alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Left; //Draw barcode into the PDF page code39.Draw(page, new PointF(30, 150)); //Set right alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Right; //Draw barcode into the PDF page code39.Draw(page, new PointF(30, 270)); //Save and close the document document.Save("BarcodeTextAlignment.pdf"); document.Close(true);
VB.NET
'Creating a new PDF document Dim document As New PdfDocument() document.PageSettings.SetMargins(10.0F) 'Adding new page to the PDF document Dim page As PdfPage = document.Pages.Add() 'Barcode size Dim size As New SizeF(250, 100) 'Create a new instance for Code39 barcode Dim code39 As New PdfCode39Barcode() code39.Text = "CODE39$" 'Set center alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Center 'Setting size of the barcode code39.Size = size 'Draw barcode into the PDF page code39.Draw(page, New PointF(30, 30)) 'Set left alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Left 'Draw barcode into the PDF page code39.Draw(page, New PointF(30, 150)) 'Set right alignment to the barcode text code39.TextAlignment = PdfBarcodeTextAlignment.Right 'Draw barcode into the PDF page code39.Draw(page, New PointF(30, 270)) 'Save and close the document document.Save("BarcodeTextAlignment.pdf") document.Close(True)
A complete working sample can be downloaded from AlignBarcodeTestInPDF.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with barcode, where you will find other options like drawing one and two dimensional barcodes in the PDF document, setting location and size to the barcode, and customizing the barcode appearance.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to drawing one and two dimensional barcode in PDF document.
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.