For Create PDF in asp.net using c# with row span & colspan
1) Please install the Install-Package iTextSharp-LGPL nuget
2)For Rowspan
private static void addCellWithRowSpan(PdfPTable table, string text, int rowspan, bool colorStatus, bool fontStatus)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times;
if (fontStatus == false)
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
}
else
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
}
PdfPCell cell = new PdfPCell(new Phrase(text, times));
cell.Rowspan = rowspan;
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
if (colorStatus == true)
{
cell.BackgroundColor = new Color(179, 179, 179);
}
table.AddCell(cell);
}
3)For Colspan
private static void addCellWithColSpan(PdfPTable table, string text, int colspan, bool colorStatus, bool alignmentStatus, bool fontStatus)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times;
if (fontStatus == false)
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, Color.BLACK);
}
else
{
times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, Color.BLACK);
}
PdfPCell cell = new PdfPCell(new Phrase(text, times));
cell.Colspan = colspan;
if (alignmentStatus == false)
{
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
}
else
{
cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
}
if (colorStatus == true)
{
cell.BackgroundColor = new Color(179, 179, 179);
}
table.AddCell(cell);
}
4)Below code will gives you an idea of how to use addCellWithRowSpan and addCellWithColSpan
public void GenerateInvoice()
{
var doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(@"D:\" + "/Doc19.pdf", FileMode.Create));
doc.Open();
PdfPTable headerTable = new PdfPTable(new float[] { 30f, 40f });
// add a image
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font fontH1 = new Font(bfTimes, 9, Font.BOLD);
PdfPCell imageCell;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("ImagePath");
logo.ScaleToFit(90f, 90f);
imageCell = new PdfPCell(logo);
imageCell.Colspan = 1; // either 1 if you need to insert one cell
imageCell.Border = 0;
// add a image
PdfPCell rowCell;
float[] headerWidths = new float[] { 250f, 250f };
headerTable.AddCell(imageCell);
rowCell = new PdfPCell(new Phrase("Invoice No : INV10001\nInvoices Type : ", fontH1));
rowCell.Border = 0;
rowCell.PaddingTop = 30f;
headerTable.AddCell(rowCell);
headerTable.HorizontalAlignment = 0;
headerTable.TotalWidth = 700f;
headerTable.LockedWidth = true;
headerTable.SetWidths(headerWidths);
headerTable.DefaultCell.Border = Rectangle.NO_BORDER;
doc.Add(headerTable);
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, new Color(65, 105, 225), Element.ALIGN_LEFT, 1)));
doc.Add(p);
Paragraph p1 = new Paragraph("\n");
doc.Add(p1);
PdfPTable ContentTable = new PdfPTable(7);
ContentTable.HorizontalAlignment = 0;
ContentTable.TotalWidth = 523f;
ContentTable.LockedWidth = true;
float[] widths = new float[] { 20f, 60f, 60f, 60f, 60f, 60f, 60f };
ContentTable.SetWidths(widths);
addCellWithRowSpan(ContentTable, "#", 1, true, true);
addCellWithRowSpan(ContentTable, "Request NO", 1, true, true);
addCellWithRowSpan(ContentTable, "Shipment Date", 1, true, true);
addCellWithRowSpan(ContentTable, "VIN", 1, true, true);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, true, false, true);
addCellWithRowSpan(ContentTable, "Amount", 1, true, true);
addCellWithRowSpan(ContentTable, "1", 3, false, false);
addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
addCellWithRowSpan(ContentTable, "POL", 1, false, false);
addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
addCellWithRowSpan(ContentTable, "2", 3, false, false);
addCellWithRowSpan(ContentTable, "SHC!10002", 1, false, false);
addCellWithRowSpan(ContentTable, "1-1-1", 1, false, false);
addCellWithRowSpan(ContentTable, "VIEEEIEWWNdfssssssssssssssssssssssssssssssssssssssssss", 1, false, false);
addCellWithColSpan(ContentTable, "Vehicle Name", 2, false, false, false);
addCellWithRowSpan(ContentTable, "254567576", 3, false, false);
addCellWithRowSpan(ContentTable, "Consignee Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Notify Name", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Loading", 1, true, true);
addCellWithRowSpan(ContentTable, "Port Of Destination", 1, true, true);
addCellWithRowSpan(ContentTable, "Millage", 1, true, true);
addCellWithRowSpan(ContentTable, "Tata Birla", 1, false, false);
addCellWithRowSpan(ContentTable, "Dipanki Jadav", 1, false, false);
addCellWithRowSpan(ContentTable, "POL", 1, false, false);
addCellWithRowSpan(ContentTable, "POD@", 1, false, false);
addCellWithRowSpan(ContentTable, "Millagekhefjkhjhf", 1, false, false);
addCellWithColSpan(ContentTable, "20i083408", 6, true, true, true);
addCellWithRowSpan(ContentTable, "-----", 1, true, true);
doc.Add(ContentTable);
doc.Add(p1);
Font fontH2 = new Font(bfTimes, 9, Font.BOLD);
Paragraph p2 = new Paragraph("Company Name \n -----------\nPhone No: ----------------", fontH2);
doc.Add(p2);
doc.Close();
}