How to place two tables side by side using itext sharp

I need to create a pdf file with two tables. and these two tables should be placed horizontally in the document. I tried 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 indicated on the right side of table1 with a spacingbefore = 50 step. Please help me find out the problem.

+4
source share
1 answer

You may need to update the layout to use the 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.

.


( html-) , :

PdfPTable outer = new PdfPTable(2);

outer.AddCell(table1);

outer.AddCel(table2);
+2

All Articles