Hi rhino000,
Refer the below code. Based on the dropdownlist value you need assign custom font. But make sure all the font(.ttf) related to your type must present in windows/fonts/[fontName.ttf] folder.
Pass folder path like - C:\Windows\Fonts\arial.ttf
HTML
<div>
<asp:DropDownList ID="ddlFont" runat="server">
<asp:ListItem Text="---Font Style---"></asp:ListItem>
<asp:ListItem Text="Arial" Value="Arial"></asp:ListItem>
<asp:ListItem Text="Verdana" Value="Verdana"></asp:ListItem>
<asp:ListItem Text="Helvetica" Value="HELVETICA"></asp:ListItem>
<asp:ListItem Text="TimesNewRoman" Value="TIMES_ROMAN"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button Text="Generate Pdf" runat="server" OnClick="GeneratePdf" />
</div>
Code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.pdf.draw;
public partial class _Default : System.Web.UI.Page
{
protected void GeneratePdf(object sender, EventArgs e)
{
string ddlValue = ddlFont.SelectedItem.Text.ToLower();
Document ds = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(ds, memoryStream);
Font font = null;
ds.Open();
if (ddlValue == "arial")
{
font = FontFactory.GetFont(@"C:\Windows\Fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 29f, Font.BOLD, BaseColor.RED);
}
else if (ddlValue == "verdana")
{
font = FontFactory.GetFont(@"C:\Windows\Fonts\verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 25f, Font.BOLD, BaseColor.GREEN);
}
else if (ddlValue == "timesnewroman")
{
font = FontFactory.GetFont(@"C:\Windows\Fonts\times.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 39f, Font.BOLD, BaseColor.YELLOW);
}
Paragraph p2 = new Paragraph(new Chunk("This is test", font));
ds.Add(p2);
Paragraph p = new Paragraph(new Chunk(new LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
//LineSeparator line1 = new LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1);
ds.Add(p);
ds.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.pdf.draw
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub GeneratePdf(sender As Object, e As EventArgs)
Dim ddlValue As String = ddlFont.SelectedItem.Text.ToLower()
Dim ds As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
Dim memoryStream As New System.IO.MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(ds, memoryStream)
Dim font__1 As Font = Nothing
ds.Open()
If ddlValue = "arial" Then
font__1 = FontFactory.GetFont("C:\Windows\Fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 29.0F, Font.BOLD, BaseColor.RED)
ElseIf ddlValue = "verdana" Then
font__1 = FontFactory.GetFont("C:\Windows\Fonts\verdana.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 25.0F, Font.BOLD, BaseColor.GREEN)
ElseIf ddlValue = "timesnewroman" Then
font__1 = FontFactory.GetFont("C:\Windows\Fonts\times.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 39.0F, Font.BOLD, BaseColor.YELLOW)
End If
Dim p2 As New Paragraph(New Chunk("This is test", font__1))
ds.Add(p2)
Dim p As New Paragraph(New Chunk(New LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)))
'LineSeparator line1 = new LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1);
ds.Add(p)
ds.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.[End]()
Response.Close()
End Sub
End Class
Screenshot
