25

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

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

56

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");
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
-1

Use set margins to adjust the padding space of your page.

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); pdfDoc.SetMargins(20f, 20f, 20f, 20f);

You can adjust the size value according to your need.

  • The OP wanted additional space between title and table, not between page borders and content. – mkl Oct 22 '20 at 10:04