We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find Line Position in Word Document in C#

So I want to be able to specify a given line in a word document and see where it is vertically on the page. i.e. With Aspose.Words can I find if a sentence is 2 inches from the top of page?

We send out letters and need the address content to show in the envelope window.

I was wondering if there’s get/set functionality for this?

Get Vertical Location in Points or Inches of a Sentence or Paragraph Lines in Word Document | C# .NET

@emcclellan,

For example, the following C# code of Aspose.Words for .NET API will print on console the Top positions of all the lines of Paragraphs in Word document. This will help you to get the (x, y) coordinates and find line position In Word document in C#:

using Aspose.Words.Layout;
using Aspose.Words.Tables;
using System;

namespace Aspose.Words.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            ApplyLicense();

            Document document = new Document("word.docx");

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

            foreach (Paragraph paragraph in document.FirstSection.Body.GetChildNodes(NodeType.Paragraph, true))
            {
                // Go to Last Character Position in Paragraph
                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); // Paragraph Break Character
                        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 Character
                        it.MoveParent(); // Cell
                        it.MoveParent(); // Row
                        stop = it.Current;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }

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

                Console.WriteLine(it.Rectangle.Top);

                // We move from Line to Line in a Paragraph.
                // When Paragraph spans multiple Pages then we will follow across them.
                while (it.Current != stop)
                {
                    if (!it.MovePreviousLogical())
                        break;

                    Console.WriteLine(it.Rectangle.Top);
                }

                Console.WriteLine("---------------------");
            }
        }

        public static void ApplyLicense()
        {
            //License license = new License();
            //license.SetLicense("path to .lic");
        }
    }
}
1 Like

Thanks this helped me get what I needed i.e. find line position in Word Document in C#

More for others to reference this is how I looked for the sentence I was looking for and used the above info to get the inches from the top of the page:

foreach (Paragraph paragraph in docBuilder.Document.Sections[0].Body.GetChildNodes(NodeType.Paragraph, true))
{
    if (paragraph.Range.Text.Contains("Sentence I'm looking for"))
    {

        LayoutCollector layoutCollector = new LayoutCollector(docBuilder.Document);
        LayoutEnumerator layoutEnumerator = new LayoutEnumerator(docBuilder.Document);

        var paraBreak = layoutCollector.GetEntity(paragraph);
        layoutEnumerator.Current = paraBreak;

        var topVal = ConvertUtil.PointToInch(layoutEnumerator.Rectangle.Top);

        Console.WriteLine(topVal);

    }
}

@emcclellan,

It is great that you were able to find Line Position in Word Document in C#. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.