Count no of Lines in a Paragraph


#1

Hi,

For a certain word document manipulation, I need to get the no of lines in a paragraph within a table cell. The requirement is that the text/content should not overflow to next line. If in case it is extending to next line, I need to decrease the font size of the text to fit into a single line. When I read the paragraph the only Control char I find is the paragraph break. Is there any way that I can achieve it using aspose.words dll ?


#2

@pprasad,

We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-17187. We will further look into the details of this requirement and will keep you updated on the status of this issue. We apologize for any inconvenience.


#3

Thank you @awais.hafeez for the quick reply. Aspose has all the features and functionalities of the microsoft office dll and I believe this requirement if incorporated in Aspose will definitely set it apart. It can make our life a bit easy.


#4

@pprasad,

Unfortunately, your issue is not resolved yet. This issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved. We apologize for your inconvenience.


#5

@awais.hafeez requesting any update on the issue ?


#6

@pprasad,

Unfortunately, your issue (WORDSNET-17187) is not resolved yet. This issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved. We apologize for any inconvenience.


#7

@pprasad,

It is to update you that, we will close the issue WORDSNET-17187. You can build logic on the following code to calculate number of lines in Paragraphs:

Document document = new Document("D:\\Temp\\input.doc");

var collector = new LayoutCollector(document);
var it = new LayoutEnumerator(document);

foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
{
    var paraBreak = collector.GetEntity(paragraph);

    object stop = null;
    var prevItem = paragraph.PreviousSibling;
    if (prevItem != null)
    {
        var prevBreak = collector.GetEntity(prevItem);
        if (prevItem is Paragraph)
        {
            it.Current = collector.GetEntity(prevItem); // para break
            it.MoveParent();    // last line
            stop = it.Current;
        }
        else if (prevItem is Table)
        {
            var table = (Table)prevItem;
            it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break
            it.MoveParent();    // cell
            it.MoveParent();    // row
            stop = it.Current;
        }
        else
        {
            throw new Exception();
        }
    }

    it.Current = paraBreak;
    it.MoveParent();

    // We move from line to line in a paragraph.
    // When paragraph spans multiple pages the we will follow across them.
    var count = 1;
    while (it.Current != stop)
    {
        if (!it.MovePreviousLogical())
            break;
        count++;
    }

    const int MAX_CHARS = 16;
    var paraText = paragraph.GetText();
    if (paraText.Length > MAX_CHARS)
        paraText = $"{paraText.Substring(0, MAX_CHARS)}...";

    Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s).");
}