Hi Sagarnayak,
Refer below Sample.
HTML
<asp:Button ID="btnReport" runat="server" Text="Generate Report" OnClick="GenerateReport" />
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void GenerateReport(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
//Header Table
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
//Company Name and Address
phrase = new Phrase();
phrase.Add(new Chunk("Microsoft Northwind Traders Company\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.RED)));
phrase.Add(new Chunk("107, Park site,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Salt Lake Road,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(2);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.3f, 1f });
table.SpacingBefore = 20f;
//Employee Details
cell = PhraseCell(new Phrase("Employee Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
table.AddCell(cell);
table = new PdfPTable(2);
table.SetWidths(new float[] { 0.5f, 2f });
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
phrase = new Phrase();
phrase.Add(new Chunk("Mr. Mudassar Ahmed Khan\n", FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
phrase.Add(new Chunk("(Moderator)", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(" "), PdfPCell.ALIGN_LEFT);
cell.Colspan = 2;
table.AddCell(cell);
//Employee Id
table.AddCell(PhraseCell(new Phrase("Employee code:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("0001", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Address
table.AddCell(PhraseCell(new Phrase("Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
phrase = new Phrase(new Chunk("507 - 20th Ave. E.\nApt. 2A\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Seattle\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("WA USA 98122", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Date of Birth
table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("25 FEBRUARY", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
document.Add(table);
//Add border to page
PdfContentByte content = writer.DirectContent;
Rectangle rectangle = new Rectangle(document.PageSize);
rectangle.Left += document.LeftMargin;
rectangle.Right -= document.RightMargin;
rectangle.Top -= document.TopMargin;
rectangle.Bottom += document.BottomMargin;
content.SetColorStroke(BaseColor.BLACK);
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
content.Stroke();
document.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();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
VB.Net
Protected Sub GenerateReport(ByVal sender As Object, ByVal e As EventArgs)
Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F)
Dim NormalFont As Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK)
Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
Dim phrase As Phrase = Nothing
Dim cell As PdfPCell = Nothing
Dim table As PdfPTable = Nothing
document.Open()
table = New PdfPTable(1)
table.TotalWidth = 400.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk("Microsoft Northwind Traders Company" & vbLf & vbLf, FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.RED)))
phrase.Add(New Chunk("107, Park site," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Salt Lake Road," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(2)
table.HorizontalAlignment = Element.ALIGN_LEFT
table.SetWidths(New Single() {0.3F, 1.0F})
table.SpacingBefore = 20.0F
cell = PhraseCell(New Phrase("Employee Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
table.AddCell(cell)
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 30.0F
table.AddCell(cell)
table = New PdfPTable(2)
table.SetWidths(New Single() {0.5F, 2.0F})
table.TotalWidth = 340.0F
table.LockedWidth = True
table.SpacingBefore = 20.0F
table.HorizontalAlignment = Element.ALIGN_RIGHT
phrase = New Phrase()
phrase.Add(New Chunk("Mr. Mudassar Ahmed Khan" & vbLf, FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK)))
phrase.Add(New Chunk("(Moderator)", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
cell.Colspan = 2
table.AddCell(cell)
cell = PhraseCell(New Phrase(" "), PdfPCell.ALIGN_LEFT)
cell.Colspan = 2
table.AddCell(cell)
table.AddCell(PhraseCell(New Phrase("Employee code:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase("0001", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
table.AddCell(PhraseCell(New Phrase("Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
phrase = New Phrase(New Chunk("507 - 20th Ave. E." & vbLf & "Apt. 2A" & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Seattle" & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("WA USA 98122", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
table.AddCell(PhraseCell(New Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase("25 FEBRUARY", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
cell = PhraseCell(New Phrase(), PdfPCell.ALIGN_CENTER)
cell.Colspan = 2
cell.PaddingBottom = 10.0F
table.AddCell(cell)
document.Add(table)
Dim content As PdfContentByte = writer.DirectContent
Dim rectangle As Rectangle = New Rectangle(document.PageSize)
rectangle.Left += document.LeftMargin
rectangle.Right -= document.RightMargin
rectangle.Top -= document.TopMargin
rectangle.Bottom += document.BottomMargin
content.SetColorStroke(BaseColor.BLACK)
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height)
content.Stroke()
document.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 Using
End Sub
Private Shared Function PhraseCell(ByVal phrase As Phrase, ByVal align As Integer) As PdfPCell
Dim cell As PdfPCell = New PdfPCell(phrase)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0F
Return cell
End Function
Screenshot
