19

I'm creating a PDF document consisting of text only, where all the text is the same point size and font family but each character could potentially be a different color. Everything seems to work fine using the code snippet below, but the default space between the lines is slightly greater than I consider ideal. Is there a way to control this? (FYI, type "ColoredText" in the code below merely contains a string and its color. Also, the reason I am treating the newline character separately is that for some reason it doesn't cause a newline if it's in a Chunk.)

Thanks, Ray

List<byte[]> pdfFilesAsBytes = new List<byte[]>();
iTextSharp.text.Document document = new iTextSharp.text.Document();
MemoryStream memStream = new MemoryStream();
iTextSharp.text.pdf.PdfWriter.GetInstance(document, memStream);
document.SetPageSize(isLandscape ? iTextSharp.text.PageSize.LETTER.Rotate() : iTextSharp.text.PageSize.LETTER);
document.Open();
foreach (ColoredText coloredText in coloredTextList)
{
    Font font = new Font(Font.FontFamily.COURIER, pointSize, Font.NORMAL, coloredText.Color);
    if (coloredText.Text == "\n")
       document.Add(new Paragraph("", font));
    else
        document.Add(new Chunk(coloredText.Text, font));
}
document.Close();
pdfFilesAsBytes.Add(memStream.ToArray());
BenevolentDeity
  • 693
  • 1
  • 4
  • 18
  • http://api.itextpdf.com/itext/com/itextpdf/text/Paragraph.html setSpacingAfter() – ray Feb 16 '14 at 11:16

2 Answers2

52

According to the PDF specification, the distance between the baseline of two lines is called the leading. In iText, the default leading is 1.5 times the size of the font. For instance: the default font size is 12 pt, hence the default leading is 18.

You can change the leading of a Paragraph by using one of the other constructors. See for instance: public Paragraph(float leading, String string, Font font)

You can also change the leading using one of the methods that sets the leading:

paragraph.SetLeading(fixed, multiplied);

The first parameter is the fixed leading: if you want a leading of 15 no matter which font size is used, you can choose fixed = 15 and multiplied = 0.

The second parameter is a factor: for instance if you want the leading to be twice the font size, you can choose fixed = 0 and multiplied = 2. In this case, the leading for a paragraph with font size 12 will be 24, for a font size 10, it will be 20, and son on.

You can also combine fixed and multiplied leading.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno, I thought that this wouldn't apply to Chunks but it was just my misunderstanding of how all this fits together. It worked perfectly. Ray – BenevolentDeity Feb 16 '14 at 20:15
  • Yes, I can see what caused the confusion: a `Chunk` doesn't have a leading, but when added to a document, the `Chunk` is always added in the context of a `Phrase` or `Paragraph` (explicitly or implicitly) in which case it uses the leading of that object. – Bruno Lowagie Feb 17 '14 at 06:51
  • So how can one control the spacing before the Paragraph independently of the spacing between lines in the paragraph? If I try setting the multiplier spacing to 1 (i.e. no spacing between lines), that also wipes out the space before the paragraph, when inserted into the cell. – Jeff Evans Apr 30 '14 at 21:21
  • Did you look at the `setSpacingBefore()` method? – Bruno Lowagie May 01 '14 at 06:12
  • If my understanding is correct, the default leading in iText 7 is now 1.35 (multiplied), see https://github.com/itext/itext7/blob/8ae1279ff81ebd3b3cd7c11468f53bff698b83c0/layout/src/main/java/com/itextpdf/layout/element/Paragraph.java#L178 – LCC Aug 19 '20 at 19:08
4
    private static Paragraph addSpace(int size = 1)
    {

        Font LineBreak = FontFactory.GetFont("Arial", size);      
        Paragraph paragraph = new Paragraph("\n\n", LineBreak);
        return paragraph;

    }
Pascal Carmoni
  • 554
  • 4
  • 8