I have used iTextSharp for pdf generation.
My Scenario is two different table generate side by side with larger amount of data and also some data store in second page also so i have create some sample but i get one error when i add data in first table and data is larger so data data store in second page and then start add data in second table data store not in first row but data store in first table last row so can u guys please help me ..
I have create sample demo for this ..
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
PdfPTable Firsttable = new PdfPTable(1) { HorizontalAlignment = 1 };
Firsttable.TotalWidth = 262f;
Chunk leader = new Chunk(new LineSeparator(0f, 100f, BaseColor.BLACK, Element.ALIGN_CENTER, -1));
Paragraph p1 = new Paragraph("Project");
p1.Add(leader);
cell = new PdfPCell(p1) { Border = Rectangle.NO_BORDER };
Firsttable.AddCell(cell);
float[] aa = { 270f };
Firsttable.SetTotalWidth(aa);
for (int i = 1; i < 50; i++)
{
cell = new PdfPCell(new Phrase("row" + i)) { Border = Rectangle.NO_BORDER };
Firsttable.AddCell(cell);
}
PdfContentByte canvas = writer.DirectContent;
int currentRowStart = 0;
int currentRow = 0;
int totalRows = Firsttable.Rows.Count();
while (true)
{
float available_height = 770;
while (available_height > 0 && currentRow < totalRows)
{
available_height -= Firsttable.GetRowHeight(currentRow++);
}
if (currentRow == totalRows)
{
break;
}
Firsttable.WriteSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
doc.NewPage();
currentRowStart = currentRow;
if (currentRow < 1)
{
currentRow = 1;
currentRowStart = 1;
}
}
Firsttable.WriteSelectedRows(currentRowStart, -1, 36, 806, canvas);
PdfPTable Secondtable = new PdfPTable(1) { };
Secondtable.TotalWidth = 270f;
cell = new PdfPCell(new Phrase("test")) { };
Secondtable.AddCell(cell);
float[] aa1 = { 270f };
Secondtable.SetTotalWidth(aa1);
for (int i = 1; i < 30; i++)
{
cell = new PdfPCell(new Phrase("row" + i)) { Border = Rectangle.NO_BORDER };
Secondtable.AddCell(cell);
}
PdfContentByte canvas1 = writer.DirectContent;
int currentRowStart1 = 0;
int currentRow1 = 0;
int totalRows1 = Secondtable.Rows.Count();
while (true)
{
float available_height = 770;
while (available_height > 0 && currentRow1 < totalRows1)
{
available_height -= Secondtable.GetRowHeight(currentRow1++);
}
if (currentRow1 == totalRows1)
{
break;
}
Secondtable.WriteSelectedRows(currentRowStart1, --currentRow1, doc.Left + 300, 806, canvas1);
doc.NewPage();
currentRowStart1 = currentRow1;
if (currentRow1 < 1)
{
currentRow1 = 1;
currentRowStart1 = 1;
}
}
Secondtable.WriteSelectedRows(currentRowStart1, -1, doc.Left + 300, 806, canvas1);
doc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
using (MemoryStream input = new MemoryStream(bytes))
{
using (MemoryStream output = new MemoryStream())
{
bytes = input.ToArray();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test" + ".pdf");
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
}
}
}