How can I increase the spacing within a paragraph with iTextSharp?

From this, I thought that I could increase the inter-paragraph spacing by doing one of these:

par.SetLeading(15, 2.5f); // doesn't do anything
par.SetLeading(0, 2);     // doesn't do anything

In context:

Chunk boldpart = new Chunk("Important: ", helvetica9BoldRed);
Chunk ini = new Chunk("Form must be filled out in ", helvetica9Red);

Anchor anchor = new Anchor("Adobe Reader", LinkFont);
anchor.Reference = "http://www.adobe.com";

Chunk middlePart = new Chunk(" or Acrobat Professional 8.1 or above. To save completed forms, Acrobat Professional is required. For technical and accessibility assistance, contact the ", helvetica9Red);

Anchor anchorCCO = new Anchor("Campus Controller's Office", LinkFont);
anchor.Reference = "mailto:[email protected]";

PdfPTable tbl = new PdfPTable(1);
tbl.WidthPercentage = 55;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
//par.SetLeading(15, 2.5f); // doesn't do anything
par.SetLeading(0, 2);       // doesn't do anything

par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);                          
doc.Add(tbl);

As the comments indicate, neither do anything. Here's what I see:

enter image description here

How can I increase the distance between lines within a paragraph?

UPDATE

When I attempt this (in response to the answer):

PdfPCell chunky = new PdfPCell();
chunky.AddCell(par);

I get, "'iTextSharp.text.pdf.PdfPCell' does not contain a definition for 'AddCell' and no extension method 'AddCell' accepting a first argument of type 'iTextSharp.text.pdf.PdfPCell' could be found"

Upvotes: 2

Views: 7753

Answers (2)

This works:

var par = new Paragraph();
par.SetLeading(0, 1.2f);
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell();
chunky.AddElement(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);                          

Increase or decrease the "1.2f" to taste.

Upvotes: 0

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Your question is a duplicate of many questions already answered on StackOverflow. These are some of them:

There are plenty more, but those mentioned above are the ones that were selected for the free ebook The Best iText Questions on StackOverflow, a book that answers many of the questions you've asked in the past week.

When you do this:

PdfPCell chunky = new PdfPCell(par);

You do not treat par as a Paragraph. The PdfPCell looks at par as if it were a Phrase (Phrase is the superclass of Paragraph). As documented on many pleases, you are creating a PdfPCell in text mode. Text mode means that all the properties defined at the paragraph level are ignored in favor of the properties at the level of the cell.

In other words: if you define a leading for par, it will be ignored. Instead, the leading of chunky will be used. This is a design choice that was made based on experience: defining properties at the level of the cell makes more sense when you are mainly interested in adding text.

In some cases, you do not want to define properties at the level of the cell. For instance: maybe you have a cell with content that consists of different Paragraph objects with different leadings, alignments, etc...

In that case, you'll switch from text mode to composite mode. You can do that like this:

PdfPCell chunky = new PdfPCell();
chunky.AddCell(par);

Properties such as leading and alignment defined at the level of the cell will be ignored. Instead, the leading and alignment of par will be taken into account (and par will be treated as a real Paragraph, not as a Phrase).

All of this is explained in iText in Action, in The Best iText Questions on StackOverflow, and many other places.

Upvotes: 3

Related Questions