Specifying line height

I am converting a text report into a PDF file using Aspose.PDF and need to reduce the default line height that is specified by my font (in this case, Courier New). I see that TextFragmentState and various other classes have a LineSpacing property, but this only seems to increase the line height.

The reason for doing so is to obtain more lines on the page without reducing the font size.

Is this possible?

@nullpainter

Could you please provide your sample .txt file and code snippet that you are using to generate PDF from it? We will test the scenario in our environment and address it accordingly.

Thank you for your prompt reply. Unfortunately I can’t supply the sample text file as it contains commercially-sensitive financial information. I have written a standalone program which uses similar code and placeholder text.

I can only fit 48 lines of text on a single page with the selected page margin, font size and font face. I would really like to reduce the line height so I can fit closer to, say, 60 lines.

class Program
{
    static void Main()
    {
        const string outputPath = @"c:\temp\PdfLineHeightSample.pdf";
        
        var document = new Document();
        var page = CreatePage(document);

        for (var i = 0; i < 48; i++)
        {
            var line = $"#{i} Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
            var text = BuildTextFragment(line);
            page.Paragraphs.Add(text);
        }

        document.Save(outputPath);
        Console.WriteLine($"Output written to {outputPath}");
    }
    
    private static TextFragment BuildTextFragment(string line)
    {
        var text = new TextFragment(line);

        text.TextState.FontSize = 10;
        text.TextState.Font = FontRepository.FindFont("Courier New");
        
        // This reduces the line spacing a bit
        text.TextState.FormattingOptions = new TextFormattingOptions(TextFormattingOptions.WordWrapMode.NoWrap)
        {
            LineSpacing = TextFormattingOptions.LineSpacingMode.FullSize
        };
        
        return text;
    } 
    
    private static Page CreatePage(Document pdfDocument)
    {
        const int margin = 20;

        var page = pdfDocument.Pages.Add();
        
        page.PageInfo.IsLandscape = true;
        
        page.PageInfo.Margin.Left = margin;
        page.PageInfo.Margin.Right = margin;
        page.PageInfo.Margin.Top = margin;
        page.PageInfo.Margin.Bottom = margin;

        return page;
    }
}

@nullpainter

We have reproduced the similar issue in our environment while testing the scenario using Aspose.PDF for .NET 21.3. Therefore, we have logged an investigation ticket as PDFNET-49743 in our issue tracking system. We will further look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like