How can add space\margin between two elements in iTextSharp\iText?

AndreaNobili picture AndreaNobili · Apr 7, 2014 · Viewed 40.7k times · Source

I am pretty new in iTextSharpt (the iText porting for C#) and I have the following doubt.

In my code I have something like it:

iTextSharp.text.Paragraph titolo = new iTextSharp.text.Paragraph(currentVuln.Title, _fontTitolo0);
titolo.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
_document.Add(titolo);

table = new PdfPTable(3);
table.WidthPercentage = 98;

cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);

table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");

_document.Add(table);

As you can see I simply print a title (usinga Paragraph object) and under it a place a table.

The problem is that there is no space (margin) between my title and my table and the graphic result is not good, this is what I obtain in the generated PDF:

enter image description here

What can I do to add some space\margin between the title paragraph and the table? What is the best way to do it? I am trying to do it but, untill now, I have found no solution

Tnx

Answer

Chris Haas picture Chris Haas · Apr 7, 2014

You have a couple of different options. You could set the SpacingAfter on your paragraph:

titolo.SpacingAfter = 20;

You could also set the SpacingBefore on the table:

table.SpacingBefore = 20;

Or you could just add some returns to your paragraph:

iTextSharp.text.Paragraph titolo = new iTextSharp.text.Paragraph("Hello World\n\n");