Hi,
Please refer below code.
Please below article to get ItextSharp
HTML
<div>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
<hr />
<asp:PlaceHolder ID="plBarCode" runat="server" />
<br />
<asp:Button Text="Export to PDF" runat="server" OnClick="ExportToPDF" />
</div>
Code
protected void btnGenerate_Click(object sender, EventArgs e)
{
string barCode = txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
System.Drawing.Font oFont = new System.Drawing.Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(System.Drawing.Color.Black);
SolidBrush whiteBrush = new SolidBrush(System.Drawing.Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
ViewState["ImageBase64"] = Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
}
protected void ExportToPDF(object sender, EventArgs e)
{
if (ViewState["ImageBase64"] != null)
{
string base64 = (string)ViewState["ImageBase64"];
byte[] imagebytes = Convert.FromBase64String(base64);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagebytes);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4, 88f, 88f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(image);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=Image.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}
}
VB
Protected Sub btnGenerate_Click(sender As Object, e As EventArgs)
Dim barCode As String = txtCode.Text
Dim imgBarCode As New System.Web.UI.WebControls.Image()
Using bitMap As New Bitmap(barCode.Length * 40, 80)
Using graphics__1 As Graphics = Graphics.FromImage(bitMap)
Dim oFont As New System.Drawing.Font("IDAutomationHC39M", 16)
Dim point As New PointF(2F, 2F)
Dim blackBrush As New SolidBrush(System.Drawing.Color.Black)
Dim whiteBrush As New SolidBrush(System.Drawing.Color.White)
graphics__1.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics__1.DrawString((Convert.ToString("*") & barCode) + "*", oFont, blackBrush, point)
End Using
Using ms As New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Dim byteImage As Byte() = ms.ToArray()
ViewState("ImageBase64") = Convert.ToBase64String(byteImage)
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage)
End Using
plBarCode.Controls.Add(imgBarCode)
End Using
End Sub
Protected Sub ExportToPDF(sender As Object, e As EventArgs)
If ViewState("ImageBase64") IsNot Nothing Then
Dim base64 As String = DirectCast(ViewState("ImageBase64"), String)
Dim imagebytes As Byte() = Convert.FromBase64String(base64)
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagebytes)
Using memoryStream As New System.IO.MemoryStream()
Dim document As New iTextSharp.text.Document(PageSize.A4, 88F, 88F, 10F, 10F)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
document.Open()
document.Add(image)
document.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=Image.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
End Using
End If
End Sub
I hope this will help you out.