I am using a iTextsharp to create a table.I want a table at particular position in the page and I am using a table.WriteSelectedRows(0, -1, 300, 300, pcb); method but its not working. Here is my code.
using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create))
{
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfContentByte pcb = writer.DirectContent;
PdfPTable table = new PdfPTable(9);
table.TotalWidth = 595f;
// Hdeaer row.
table.AddCell(GetCell("Header 1", 1, 2));
table.AddCell(GetCell("Header 2", 1, 2));
table.AddCell(GetCell("Header 3", 5, 1));
table.AddCell(GetCell("Header 4", 1, 2));
table.AddCell(GetCell("Header 5", 1, 2));
// Inner middle row.
table.AddCell(GetCell("H1"));
table.AddCell(GetCell("H2"));
table.AddCell(GetCell("H3"));
table.AddCell(GetCell("H4"));
table.AddCell(GetCell("H5"));
// Bottom row.
table.AddCell(GetCell("D1"));
table.AddCell(GetCell("D2"));
table.AddCell(GetCell("D3"));
table.AddCell(GetCell("D4"));
table.AddCell(GetCell("D5"));
table.AddCell(GetCell("D6"));
table.AddCell(GetCell("D7"));
table.AddCell(GetCell("D8"));
table.AddCell(GetCell("D9"));
table.WriteSelectedRows(0, -1, 300, 300, pcb);
doc.Close();
}
private PdfPCell GetCell(string text)
{
return GetCell(text, 1, 1);
}
private PdfPCell GetCell(string text, int colSpan, int rowSpan)
{
PdfPCell cell = new PdfPCell(new Phrase(text));
cell.HorizontalAlignment = 1;
cell.Rowspan = rowSpan;
cell.Colspan = colSpan;
return cell;
}