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
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With