Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp Vertical Spacing

Tags:

c#

itextsharp

I'm using iTextSharp to generate some PDF files. I have two tables which have content, and I want to put some space between the two tables, say the equivalent of 1 line of text (using the same font as the tables around the space).

Below is the code I'm using to add the two tables. What I cannot figure out is how to place a vertical space between the two tables.

Table upperTable = new Table(1);
upperTable.Border = Rectangle.NO_BORDER;
upperTable.DefaultCell.Border = Rectangle.NO_BORDER;
upperTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
upperTable.AddCell(new Phrase("some text", font3));
d.Add(upperTable);
Table lowerTable= new Table(1);
lowerTable.Border = Rectangle.NO_BORDER;
lowerTable.DefaultCell.Border = Rectangle.NO_BORDER;
lowerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
lowerTable.AddCell(new Phrase("some other text", font3));
d.Add(lowerTable);

Can someone tell me how I can add the vertical space between the two tables?

Thanks!

like image 491
Elie Avatar asked May 18 '09 01:05

Elie


2 Answers

I found a solution that sort of works... add the new lines as part of either the preceding string, or the following string to the space that I want to create. For example:

Table upperTable = new Table(1);
upperTable.Border = Rectangle.NO_BORDER;
upperTable.DefaultCell.Border = Rectangle.NO_BORDER;
upperTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
upperTable.AddCell(new Phrase("some text" + '\n', font3));
d.Add(upperTable);
Table lowerTable= new Table(1);
lowerTable.Border = Rectangle.NO_BORDER;
lowerTable.DefaultCell.Border = Rectangle.NO_BORDER;
lowerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
lowerTable.AddCell(new Phrase('\n' + "some other text", font3));
d.Add(lowerTable);

would cause 2 lines whose height is defined by font3 to be added between the "some text" and "some other text"

like image 26
Elie Avatar answered Nov 07 '22 10:11

Elie


Use PdfPTable instead. It has properties SpacingBefore and SpacingAfter

For example:

PdfPTable upperTable = new PdfPTable(1);
upperTable.AddCell(new Phrase("some text", font3));
upperTable.SpacingAfter = 10f;
like image 150
Eugene Z Avatar answered Nov 07 '22 10:11

Eugene Z