Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Mar 22, 2024 3:45 am

trying to get the vertical location of a paragraph in a word file. See the code below, this line (int paragraphStartPosition = paragraph.Range.Start) does not work. Is there any way to get this or should I move on to something else?
Thanks!

foreach (Section section in doc.Sections)
{
foreach (Paragraph paragraph in section.Paragraphs)
{
// Get the start position of the paragraph
int paragraphStartPosition = paragraph.Range.Start;

// Print the start position of the paragraph
Console.WriteLine($"Start position of paragraph: {paragraphStartPosition}");
}
}

bcamp011
 
Posts: 6
Joined: Wed Feb 07, 2024 9:10 pm

Fri Mar 22, 2024 9:13 am

Hello,

Thanks for your inquiry.
Please refer to the following code to obtain the starting position of the paragraph. If you have any questions, please feel free to write to us at any time.
Code: Select all
Document document = new Document();
document.LoadFromFile("test.docx");
FixedLayoutDocument fixedLayoutDocument = new FixedLayoutDocument(document);
LayoutCollection<LayoutElement> lines = fixedLayoutDocument.GetChildEntities(LayoutElementType.Line, true);
Dictionary<Paragraph, LayoutElement> keyValuePairs = new Dictionary<Paragraph, LayoutElement>();
foreach (LayoutElement line in lines)
{

    if (line.ParentNode is Paragraph)
    {
        Paragraph paragraph = (Paragraph)line.ParentNode;
        if (!keyValuePairs.ContainsKey(paragraph))
        {
            keyValuePairs.Add(paragraph, line);
        }

    }
}
foreach (KeyValuePair<Paragraph, LayoutElement> keyValuePair in keyValuePairs)
{
    Paragraph para = keyValuePair.Key;
    LayoutElement line = keyValuePair.Value;
    Section owner = (Section)para.Owner.Owner;
    int secIndex = document.Sections.IndexOf(owner);
    int paraIndex = para.Owner.ChildObjects.IndexOf(para);
    System.Drawing.RectangleF rectangle = line.Rectangle;

    Console.WriteLine("Section {0} Paragraph{1} conten: {2} \r\n start postion is:{3},{4}\r\n", secIndex, paraIndex, para.Text,rectangle.X, rectangle.Y);
}
Console.ReadKey();

Sincerely
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Tue Mar 26, 2024 3:06 am

Thank you! This is what I needed!

bcamp011
 
Posts: 6
Joined: Wed Feb 07, 2024 9:10 pm

Tue Mar 26, 2024 8:20 am

Hello,

Thanks for your feedback.
I'm glad to hear that. If you encounter any questions about our products in the future, please feel free to write to us at any time.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Thu Apr 25, 2024 12:32 am

thanks for your help! I have one last question, I think. Is there anyway to get the end location each paragraph, or at least the end location of each line in a paragraph?

bcamp011
 
Posts: 6
Joined: Wed Feb 07, 2024 9:10 pm

Thu Apr 25, 2024 2:50 am

bcamp011 wrote:thanks for your help! I have one last question, I think. Is there anyway to get the end location each paragraph, or at least the end location of each line in a paragraph?


Hi,

Thanks for your inquiry.
For your requirement, you just make adjustment for the code our provided, I put the modified code below for your reference.
Code: Select all
     
// Create a Document object
       Document document = new Document();
           
            // Load the document from a file
            document.LoadFromFile(@"../../data/test.docx");

            // Create a FixedLayoutDocument object for handling documents with fixed layouts
            FixedLayoutDocument fixedLayoutDocument = new FixedLayoutDocument(document);
           
            // Get all line elements in the document
            LayoutCollection<LayoutElement> lines = fixedLayoutDocument.GetChildEntities(LayoutElementType.Line, true);

            // Define a list to store line elements within a paragraph
            List<LayoutElement> ParaLines;
           
            // Define a dictionary to store the paragraph and its corresponding list of line elements
            Dictionary<Paragraph, List<LayoutElement>> keyValuePairs1 = new Dictionary<Paragraph, List<LayoutElement>>();
           
            // Iterate over all line elements
            foreach (LayoutElement line in lines)
            {
                // If the parent node of the current line is a paragraph
                if (line.ParentNode is Paragraph)
                {
                    // Cast the current line to a paragraph object
                    Paragraph paragraph = (Paragraph)line.ParentNode;
                   
                    // If the dictionary does not contain the current paragraph
                    if (!keyValuePairs1.ContainsKey(paragraph))
                    {
                        // Create a new list of line elements and add the current line to it
                        ParaLines = new List<LayoutElement>();
                        ParaLines.Add(line);
                     
                        // Add the paragraph and the list of line elements to the dictionary
                        keyValuePairs1.Add(paragraph, ParaLines);
                    }
                    else
                    {
                        // If the dictionary already contains the current paragraph, try to get the corresponding list of line elements
                        keyValuePairs1.TryGetValue(paragraph, out ParaLines);
                     
                        // Add the current line to the list of line elements
                        ParaLines.Add(line);
                    }
                }
            }

            // Iterate over all key-value pairs in the dictionary
            foreach (KeyValuePair<Paragraph, List<LayoutElement>> keyValuePair in keyValuePairs1)
            {
                // Get the paragraph object and its corresponding list of line elements
                Paragraph para = keyValuePair.Key;
                List<LayoutElement> paraLines = keyValuePair.Value;
               
                // Get the first and last line of the paragraph
                LayoutElement FirstLineOfPara = paraLines[0];
                LayoutElement LastLineOfPara = paraLines[paraLines.Count - 1];

                // Get the section that the paragraph belongs to and the index of the paragraph within the section
                Section owner = (Section)para.Owner.Owner;
                int secIndex = document.Sections.IndexOf(owner);
                int paraIndex = para.Owner.ChildObjects.IndexOf(para);
                // Get the rectangle area of the first and last lines
                System.Drawing.RectangleF FirstRectangle = FirstLineOfPara.Rectangle;
                System.Drawing.RectangleF LastRectangle = LastLineOfPara.Rectangle;

                // Output the content of the paragraph, starting position, and ending position
                Console.WriteLine("Section {0} Paragraph{1} conten: {2} \r\n start postion is:{3},{4}\r\n", secIndex, paraIndex, para.Text, FirstRectangle.X, FirstRectangle.Y);
                Console.WriteLine("Section {0} Paragraph{1} conten: {2} \r\n end postion is:{3},{4}\r\n", secIndex, paraIndex, para.Text, LastRectangle.X, LastRectangle.Y);
            }

            // Wait for user to press a key
            Console.ReadKey();


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Dec 15, 2025 11:02 pm

Hello again! I have been away from this project for about a year and a half and would like to get back into it. Would you have any idea why I get the error: IBM437 is not a supported encoding name.

bcamp011
 
Posts: 6
Joined: Wed Feb 07, 2024 9:10 pm

Tue Dec 16, 2025 1:40 am

Hello,

Thank you for your inquiry.
Based on the information currently provided, it is difficult for me to determine the root cause of the issue you are experiencing. Could you please provide a minimal demo to reproduce the problem, along with details of the environment where the error occurred (OS version, .NET version, etc.), the version of Spire.Doc you are using (e.g., Spire.Doc for .NET Version: 13.12.2, .NET 6.0 dll), and your regional settings (e.g., China/Chinese)? This information will help us effectively investigate the cause of the issue. You may upload the files as attachments or send them directly to my email: [email protected]. Thank you in advance.
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 341
Joined: Mon Apr 14, 2025 3:33 am

Return to Spire.Doc

cron