How to Export Barcodes as HTML
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 as either a Data URL, HTML Tag, or HTML File. In this article, we'll discuss how we can export to each format.
How to Export Barcodes as HTML in C#
- Download C# library to export barcodes
- Export barcodes as Data URL
- Export barcodes as HTML tag
- Export barcodes as HTML file
Get started with IronBarcode
Start using IronBarcode in your project today with a free trial.
Export Barcode as Data URL
Before discussing how to export a barcode as a Data URL, we need to first know what exactly is a data URL. Data URL, 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 src
attribute. Here's a code snippet demonstrating how we can convert our GeneratedBarcode
into a 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)
From the code snippet above, we started off with creating a barcode, using the CreateBarcode()
method from the BarcodeWriter
class with the 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
.
Export Barcode as HTML Tag
Another way to export our GeneratedBarcode
to HTML is by exporting it into a HTML tag using the 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 in any JavaScript, CSS, or image dependencies. The following code snippet demonstrates how to export as an HTML tag.
: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)
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. This HTML tag can directly be embedded into a larger HTML file.
Export Barcode as HTML File
Users can also opt to save the GeneratedBarcode
as a HTML file instead. To do this, use the SaveAsHtmlFile()
method. The following code snippet demonstrates how to use this method.
: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")
This method accepts the string of a filepath. Looking into the HTML file generated, we can see that the barcode outputted as an HTML tag within the
,, andtags that make up a complete HTML file.