Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to place two tables side by side using itext sharp

I have to create a pdf file with two tables. and these two table should place horizontally in the document .I have tried out like this ,

   var doc1 = new Document(PageSize.A4);
   PdfWriter.GetInstance(doc1, new FileStream(path + "/" + pdf_name + "", FileMode.Create));
            doc1.Open();

  var table1 = new PdfPTable(1); //table1
           table1.HorizontalAlignment = Element.ALIGN_LEFT;
           table1.SpacingBefore = 50;
           table1.DefaultCell.Border = 1;
           table1.WidthPercentage = 40;
            PdfPCell cell = new PdfPCell(new Phrase(student_name, boldTableFont));
           // cell.Border = 1;
           // cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
              cell.HorizontalAlignment = Element.ALIGN_CENTER;
            table1.AddCell(cell);
            doc1.Add(table1);


           var table2= new PdfPTable(1); //table2

           table2.DefaultCell.Border = 1;
           table2.HorizontalAlignment = 2;

           table2.SpacingBefore = 50;
           table2.WidthPercentage = 40;

              PdfPCell cell21 = new PdfPCell(new Phrase("success", body));
             cell21.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
             table2.AddCell(cell21);
           doc1.Add(table2);
           doc1.Close();

but the second table is not come on the right side of table1 with spacingbefore=50. Please help me for finding out the problem

like image 655
nsds Avatar asked Dec 18 '13 07:12

nsds


2 Answers

You may need to look at updating your layout to use columns (see here for more details):

http://www.mikesdotnetting.com/Article/89/iTextSharp-Page-Layout-with-Columns

Without seeing more about your layout it's hard to say which column based layout is best.

Alternatively you could absolutely position your tables and write them that way.


As a third option (which is very much like an old html page), you could nest tables like this:

PdfPTable outer = new PdfPTable(2);

outer.AddCell(table1);

outer.AddCell(table2);

document.Add(outer);
like image 53
Paddy Avatar answered Nov 09 '22 05:11

Paddy


The answer that Paddy said helped me a lot.

First I created the 2 tables the way I wanted.

Then I used Paddy's 3rd example, my code looked like this:

// Tabela para os trajetos de ida
    var tablePercursosIda = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();

    foreach (var item in Content.itinerarios)
    {
        foreach (var percurso in item.percursos.ida)
        {
            tablePercursosIda.AddCell(new Cell()
                .Add(new Paragraph(
                    new Text(percurso.logradouroNome)
                    )
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetBold()
                )
            ).SetFontSize(10);
        }
    }

    // Tabela para os trajetos de volta
    var tablePercursosVolta = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();

    foreach (var item in Content.itinerarios)
    {
        foreach (var percurso in item.percursos.volta)
        {
            tablePercursosVolta.AddCell(new Cell()
                .Add(new Paragraph(
                    new Text(percurso.logradouroNome)
                    )
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetBold()
                )
            ).SetFontSize(10);
        }
    }

    // Tabela aninhada

    var tableAninhada = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();

    tableAninhada.AddCell(tablePercursosIda);
    tableAninhada.AddCell(tablePercursosVolta);

    table.SetHorizontalAlignment(HorizontalAlignment.CENTER);

    Report.Document.Add(table);
    Report.Document.Add(tableAninhada);

The result below, I will need to improve the layout, but it was as I needed

Result

like image 27
Sanches Avatar answered Nov 09 '22 04:11

Sanches