Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text line spacing

Tags:

itextsharp

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());
like image 718
BenevolentDeity Avatar asked Feb 16 '14 10:02

BenevolentDeity


People also ask

What is the way to change line spacing of a text?

Change the line spacing in a portion of the documentGo to Home > Line and Paragraph Spacing. Choose the number of line spaces you want or select Line Spacing Options, and then select the options you want under Spacing.

How do I get 1.15 line spacing?

The default line spacing in Word is 1.15. By default, paragraphs are followed by a blank line and headings have a space above them. Go to Home > Line and Paragraph Spacing. Select Line Spacing Options, and then choose the options you want under Spacing.


2 Answers

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.

like image 104
Bruno Lowagie Avatar answered Oct 24 '22 08:10

Bruno Lowagie


    private static Paragraph addSpace(int size = 1)
    {

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

    }
like image 41
Pascal Carmoni Avatar answered Oct 24 '22 08:10

Pascal Carmoni