Articles in this section
Category / Section

How to insert a barcode to ASP.NET Core PDF using C#?

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. Using this library, you can insert a barcode when converting HTML to PDF document using C#.

Steps to insert a barcode when converting HTML to PDF document programmatically:

  1. Create a new console application project.Screenshot (1337).png
  2. Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your console application from Nuget.org.
    Screenshot (1519).png
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;
using Syncfusion.Drawing;

VB.NET

Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Drawing
  1. Use the following code sample in Program.cs to insert a barcode when converting HTML to PDF document

C#

// Create an instance of the HtmlToPdfConverter class
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

// Create an instance of the BlinkConverterSettings class
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();

// Set Blink viewport size to 800 width and auto height
blinkConverterSettings.ViewPortSize = new Size(800, 0);

// Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

// Convert the given HTML string to a PDF document
PdfDocument document = htmlConverter.Convert("<!DOCTYPE html>\r\n<html>\r\n \r\n<head>\r\n    <title>\r\n        First Web Page\r\n    </title>\r\n</head>\r\n \r\n<body>\r\n    Hello World!\r\n</body>\r\n \r\n</html>", string.Empty);

// Get the first page of the PDF document
PdfPage page = document.Pages[0];

// Create an instance of the PdfQRBarcode class
PdfQRBarcode barcode = new PdfQRBarcode();

// Set the error correction level to high
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;

// Set the XDimension to 3
barcode.XDimension = 3;

// Set the text to be encoded in the QR code
barcode.Text = "https://www.syncfusion.com";

// Draw the barcode on the first page of the PDF document at the specified position
barcode.Draw(page, new PointF(25, 70));

// Create a file stream to save the PDF document
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.Create, FileAccess.ReadWrite);

// Save the PDF document to the file stream
document.Save(fileStream);

// Close the PDF document to release all resources
document.Close(true);


VB.NET

' Create an instance of the HtmlToPdfConverter class
Dim htmlConverter As New HtmlToPdfConverter()

' Create an instance of the BlinkConverterSettings class
Dim blinkConverterSettings As New BlinkConverterSettings()

' Set Blink viewport size to 800 width and auto height
blinkConverterSettings.ViewPortSize = New Size(800, 0)

' Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

' Convert the given HTML string to a PDF document
Dim document As PdfDocument = htmlConverter.Convert("<!DOCTYPE html>" & vbCrLf & "<html>" & vbCrLf & " " & vbCrLf & "<head>" & vbCrLf & "    <title>" & vbCrLf & "        First Web Page" & vbCrLf & "    </title>" & vbCrLf & "</head>" & vbCrLf & " " & vbCrLf & "<body>" & vbCrLf & "    Hello World!" & vbCrLf & "</body>" & vbCrLf & " " & vbCrLf & "</html>", String.Empty)

' Get the first page of the PDF document
Dim pdfLoadedPage As page = document.Pages(0)

' Create an instance of the PdfQRBarcode class
Dim barcode As New PdfQRBarcode()

' Set the error correction level to high
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High

' Set the XDimension to 3
barcode.XDimension = 3

' Set the text to be encoded in the QR code
barcode.Text = "http://www.syncfusion.com"

' Draw the barcode on the first page of the PDF document at the specified position
barcode.Draw(page, New PointF(25, 70))

' Create a file stream to save the PDF document
Using fileStream As New FileStream("HTML-to-PDF.pdf", FileMode.Create, FileAccess.ReadWrite)
   ' Save the PDF document to the file stream
   document.Save(fileStream)
End Using

' Close the PDF document and release all resources
document.Close(True)

A complete working sample can be downloaded from Insert_barcode_converting_HTML_to_PDF.zip

By executing the program, you will get the PDF document as follows.Screenshot (1526).png

Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.

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

Conclusion

I hope you enjoyed learning about how to insert a barcode to ASP.NET PDF using C#?

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied