Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ..
C#
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);
    }
    // we add the table at an absolute position (starting at the top of the page)
    PdfContentByte canvas = writer.DirectContent;
    int currentRowStart = 0;
    int currentRow = 0;
    int totalRows = Firsttable.Rows.Count();
    while (true)
    {
        // available height of the page
        float available_height = 770;
        // how many rows fit the height?
        while (available_height > 0 && currentRow < totalRows)
        {
            available_height -= Firsttable.GetRowHeight(currentRow++);
        }
        // we stop as soon as all the rows are counted
        if (currentRow == totalRows)
        {
            break;
        }
        // we draw part the rows that fit the page and start a new page
        Firsttable.WriteSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
        doc.NewPage();
        currentRowStart = currentRow;
        //currentRow -= 5;
        if (currentRow < 1)
        {
            currentRow = 1;
            currentRowStart = 1;
        }
    }
    // we draw the remaining rows
    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);
    }
    // we add the table at an absolute position (starting at the top of the page)
    PdfContentByte canvas1 = writer.DirectContent;
    int currentRowStart1 = 0;
    int currentRow1 = 0;
    int totalRows1 = Secondtable.Rows.Count();
    while (true)
    {
        // available height of the page
        float available_height = 770;
        // how many rows fit the height?
        while (available_height > 0 && currentRow1 < totalRows1)
        {
            available_height -= Secondtable.GetRowHeight(currentRow1++);
        }
        // we stop as soon as all the rows are counted
        if (currentRow1 == totalRows1)
        {
            break;
        }
        // we draw part the rows that fit the page and start a new page
        Secondtable.WriteSelectedRows(currentRowStart1, --currentRow1, doc.Left + 300, 806, canvas1);
        doc.NewPage();
        currentRowStart1 = currentRow1;
        //currentRow1 -= 5;
        if (currentRow1 < 1)
        {
            currentRow1 = 1;
            currentRowStart1 = 1;
        }
    }
    // we draw the remaining rows
    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();

        }
    }
}
Posted
Updated 18-Dec-14 0:14am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900