STEP 1 : add references of 'itextsharp.dll',
STEP 2 : add namespace
using iTextSharp.text;
using iTextSharp.text.pdf;
STEP 3 : write below code,
public ActionResult ExportPDF() {
PdfPTable fieldServiceReportDesign = MainPage();
Document document = new Document(PageSize.A4, 0 f, 0 f, 0 f, 17 f);
MemoryStream stream = new MemoryStream();
using(PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream)) {
pdfWriter.CloseStream = false;
document.Open();
document.Add(fieldServiceReportDesign);
document.Close();
stream.Flush(); //Always catches me out
stream.Position = 0;
}
return File(stream, "application/vnd.pdf", "PDF_" + DateTime.Now.Ticks + ".pdf");
}
private PdfPTable MainPage() {
//Main table
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
table.DefaultCell.BackgroundColor = new BaseColor(255, 255, 255);
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.HeaderRows = 2;
#region Header
//header details
PdfPHeaderCell hcell = new PdfPHeaderCell();
hcell.Border = 0;
hcell.Padding = 7 f;
hcell.BackgroundColor = new BaseColor(5, 150, 215);
hcell.HorizontalAlignment = Element.ALIGN_CENTER;
hcell.AddElement(GetHeader("PDF"));
table.AddCell(hcell);
//space
hcell = new PdfPHeaderCell();
hcell.Border = 0;
hcell.Padding = 7 f;
hcell.MinimumHeight = 10 F;
hcell.BackgroundColor = new BaseColor(255, 255, 255);
hcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(hcell);
#endregion
PdfPCell cell = new PdfPCell();
hcell.Border = 0;
hcell.Padding = 7 f;
hcell.MinimumHeight = 10 F;
hcell.BackgroundColor = new BaseColor(255, 255, 255);
hcell.HorizontalAlignment = Element.ALIGN_MIDDLE;
hcell.AddElement(GetData());
table.AddCell(hcell);
//leave blank
cell = new PdfPCell();
cell.Border = 0;
cell.Padding = 0;
cell.BackgroundColor = new BaseColor(255, 255, 255);
cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
table.SplitLate = false;
return table;
}
private PdfPTable GetHeader(string headerText) {
PdfPCell cell = null;
Phrase phrs = null;
//Header design table
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
table.DefaultCell.Padding = 0;
table.DefaultCell.Border = 0;
table.HorizontalAlignment = Element.ALIGN_CENTER;
phrs = new Phrase();
phrs.Add(new Chunk(headerText, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11 f, iTextSharp.text.Font.BOLD, new BaseColor(255, 255, 255))));
cell = new PdfPCell(phrs);
cell.Border = 0;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
return table;
}
private PdfPTable GetData() {
PdfPCell cell = null;
Phrase phrs = null;
//body design table
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
table.DefaultCell.Padding = 0;
table.DefaultCell.Border = 0;
table.HorizontalAlignment = Element.ALIGN_CENTER;
phrs = new Phrase();
phrs.Add(new Chunk("Hello !", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10 f, iTextSharp.text.Font.NORMAL, new BaseColor(0, 0, 0))));
cell = new PdfPCell(phrs);
cell.Border = 0;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
return table;
}