Hi moquamar,
You have to use WriteAllBytes method of File class.
Refer below sample code.
HTML
<asp:Button Text="ExportToPdf" runat="server" OnClick="Export" />
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Code
C#
protected void Export(object sender, EventArgs e)
{
string name = "Nancy Davolio";
string id = "1";
string address = "507 - 20th Ave. E. Apt. 2A";
string dob = "1948-12-08";
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;
BaseColor color = null;
document.Open();
//Header Table
table = new PdfPTable(2);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 0.3f, 0.7f });
phrase = new Phrase();
phrase.Add(new Chunk("Excelasoft Pvt Ltd\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.RED)));
phrase.Add(new Chunk("Goregaon,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Mumbai,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Maharashtra, India", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
//Separater Line
DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f);
DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f);
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;
table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(name, 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);
table.AddCell(PhraseCell(new Phrase("Designation:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("Software Engineer", 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);
//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("000" + id, 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(address + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Mumbai" + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
phrase.Add(new Chunk("Maharashtra" + " " + "India" + " " + "400097", 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(Convert.ToDateTime(dob).ToString("dd MMMM, yyyy"), 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);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
File.WriteAllBytes(@"SharedPath" + "Export.pdf", bytes);
}
}
private static void DrawLine(PdfWriter writer, float x1, float y1, float x2, float y2)
{
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetColorStroke(BaseColor.BLACK);
contentByte.MoveTo(x1, y1);
contentByte.LineTo(x2, y2);
contentByte.Stroke();
}
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;
}
private static PdfPCell ImageCell(string path, float scale, int align)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path));
image.ScalePercent(scale);
PdfPCell cell = new PdfPCell(image);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 0f;
cell.PaddingTop = 0f;
return cell;
}
VB.Net
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = "Nancy Davolio"
Dim id As String = "1"
Dim address As String = "507 - 20th Ave. E. Apt. 2A"
Dim dob As String = "1948-12-08"
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
Dim color As BaseColor = Nothing
document.Open()
table = New PdfPTable(2)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {0.3F, 0.7F})
phrase = New Phrase()
phrase.Add(New Chunk("Excelasoft Pvt Ltd" & vbLf & vbLf, FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.RED)))
phrase.Add(New Chunk("Goregaon," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Mumbai," & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Maharashtra, India", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
DrawLine(writer, 25.0F, document.Top - 79.0F, document.PageSize.Width - 25.0F, document.Top - 79.0F)
DrawLine(writer, 25.0F, document.Top - 80.0F, document.PageSize.Width - 25.0F, document.Top - 80.0F)
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
table.AddCell(PhraseCell(New Phrase("Name:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase(name, 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("Designation:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase("Software Engineer", 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("Employee code:", FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT))
table.AddCell(PhraseCell(New Phrase("000" & id, 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(address & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Mumbai" & vbLf, FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
phrase.Add(New Chunk("Maharashtra" & " " & "India" & " " & "400097", 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(Convert.ToDateTime(dob).ToString("dd MMMM, yyyy"), 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)
document.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
File.WriteAllBytes("SharedPath" & "Export.pdf", bytes)
End Using
End Sub
Private Shared Sub DrawLine(ByVal writer As PdfWriter, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single)
Dim contentByte As PdfContentByte = writer.DirectContent
contentByte.SetColorStroke(BaseColor.BLACK)
contentByte.MoveTo(x1, y1)
contentByte.LineTo(x2, y2)
contentByte.Stroke()
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
Private Shared Function ImageCell(ByVal path As String, ByVal scale As Single, ByVal align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path))
image.ScalePercent(scale)
Dim cell As PdfPCell = New PdfPCell(image)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0F
cell.PaddingTop = 0F
Return cell
End Function