How to Export Barcodes as HTML

by Hairil Hasyimi Bin Omar


C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
C#  DLL

Download DLL

Download DLL

Manually install into your project

An output from a good API must be versatile enough for users to use it further in their program or application, and not necessarily be saved in the disk. And that is why IronBarcode offer many options for users to export the GeneratedBarcode into various types, and one of them is exporting the barcodes generated as HTML.

Speaking of exporting the GeneratedBarcode into HTML, there are various ways we can see this, which is as Data URL, HTML Tag, and HTML File. Now let us discuss one by one on the options that we have when we talk about exporting GeneratedBarcode as HTML.

Export Barcode as Data URL

First, we need to know what exactly is data URL. Data URL, or also known as data URI, is a type of Uniform Resource Identifier(URI) that allows data to be embedded directly in the URL string, in-line in web pages as if they were external resources. This can be in any format, which includes text, images, audio, video, and binary data. The obtained data URL can later be used in HTML inside image tag as a srcattribute. Now let's see a code snippet of how we can convert our GeneratedBarcode into Data URL.

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsDataUrl.cs
using IronBarCode;
using System;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var dataUrl = myBarcode.ToDataUrl();
Console.WriteLine(dataUrl);
Imports IronBarCode
Imports System

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private dataUrl = myBarcode.ToDataUrl()
Console.WriteLine(dataUrl)
VB   C#

From the code snippet above, we started off with creating a barcode, using the CreateBarcode() method from BarcodeWriter class with barcode value and the barcode encoding as the arguments for this method. In order to get the Data URL of the barcode, we attached the ToDataUrl() method to the GeneratedBarcode. This Data URL can be stored in a variable to be used further in a program, or users can also call Console.WriteLine() method to the variable to see the Data URL string produced.

Export Barcodes as HTML Tag

Another way to export our GeneratedBarcode to HTML is by exporting it into a HTML tag using ToHtmlTag() method. This method renders the Generatedbarcode object as a fully formed HTML tag that can be injected directly into HTML without having to reference it to any JavaScript, CSS, or image dependencies. Lets see the code snippet below that demonstrates

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsHtmlTag.cs
using IronBarCode;
using System;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
var htmlTag = myBarcode.ToHtmlTag();
Console.WriteLine(htmlTag);
Imports IronBarCode
Imports System

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
Private htmlTag = myBarcode.ToHtmlTag()
Console.WriteLine(htmlTag)
VB   C#

From the code snippet above, we can simply attach the ToHtmlTag() method to the GeneratedBarcode in order to obtain the HTML tag of the barcode generated. The output from this method can be stored in a variable to be used further in a program or users can also call Console.WriteLine() method to see the HTML tag string produced. Users can see that the HTML tag and Data URL differs in which ToHtmltag() method outputs an HTML image tag which has src attribute pointing to the data URL, as well as specified the image size. This HTML tag can directly be embedded to a larger HTML file.

Export Barcode as HTML File

Users can also opt to save the GeneratedBarcode as a HTML file instead. Worry not, IronBarcode also provide method for this, which is by using SaveAsHtmlFile() method. Let us see the code snippet below on how to implement this

:path=/static-assets/barcode/content-code-examples/how-to/ExportBarcodeAsHtmlFile.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode);
myBarcode.SaveAsHtmlFile("myBarcode.html");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.QRCode)
myBarcode.SaveAsHtmlFile("myBarcode.html")
VB   C#

The code snippet shows that we can simply export our GeneratedBarcode to HTML file by attaching the SaveAsHtmlFile(). This method accepts a string of path where the HTML file will be saved in the disk. Looking into the HTML file generated, we can see that the barcode as HTML tag is included in addition with the

,, andtags that make up a complete HTML file.

With all that being said, we can conclude that IronBarcode provide flexibility for the users to be able to use the GeneratedBarcode as HTML by providing 3 options for users to choose from. This provides great value for users that are working with websites or web application.