How to rotate barcode vertically in 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 add or draw rotated barcode in a PDF document using C# and VB.NET.
Steps to add or draw rotated barcode in a PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Barcode; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports System.Drawing Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Barcode Imports Syncfusion.Pdf.Graphics
- Use the following code snippet to add or draw rotated barcode in PDF document.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Adding new page to PDF document PdfPage page = document.Pages.Add(); //Drawing Code39 barcode PdfCode39Barcode barcode = new PdfCode39Barcode(); //Set height of the barcode barcode.BarHeight = 45; //Setting text of the barcode barcode.Text = "CODE39"; //Save the current graphics state PdfGraphicsState gs = page.Graphics.Save(); //Draw string page.Graphics.DrawString("Code39 Barcode drawn vertically", new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(20, 30)); //Set translate transform in the position where the barcode should be drawn page.Graphics.TranslateTransform(100, 170); //Rotate the graphics to 90 degrees page.Graphics.RotateTransform(-90); //Printing barcode on to the PDF barcode.Draw(page, new PointF(0, 0)); //Restore the previous graphics state page.Graphics.Restore(gs); //Save the document document.Save("Barcode.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Barcode.pdf");
VB.NET
Dim document As PdfDocument = New PdfDocument 'Adding new page to PDF document Dim page As PdfPage = document.Pages.Add 'Drawing Code39 barcode Dim barcode As PdfCode39Barcode = New PdfCode39Barcode 'Set height of the barcode barcode.BarHeight = 45 'Setting text of the barcode barcode.Text = "CODE39" Dim gs As PdfGraphicsState = page.Graphics.Save 'Draw string page.Graphics.DrawString("Code39 Barcode drawn vertically", New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, New PointF(20, 30)) 'Set translate transform in the position where the barcode should be drawn page.Graphics.TranslateTransform(100, 170) 'Rotate the graphics to 90 degrees page.Graphics.RotateTransform(-90) 'Printing barcode on to the PDF barcode.Draw(page, New PointF(0, 0)) 'Restore the previous graphics state page.Graphics.Restore(gs) 'Save the document document.Save("Barcode.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Barcode.pdf")
A complete working sample can be downloaded from RotatePDFBarcode.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find the options like adding one dimensional and two dimensional barcodes to the PDF document, set location and size to the barcode, and customizing the barcode appearance.
Refer here to explore the rich set of Syncfusion Essential PDF features.
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.