Free Support Forum - aspose.com

Need to get locations of paragraph in pixels with custom rendering

Hi,

I want to get locations of each paragraph and table that passing through whole document and get their locations in pixels relative to top left of document page i.e. top left, top right, bottom left and bottom right.
I also need the rendering set to custom pixel size for e.g. 750 pixels of document before parsing through document.

Thanks in advance

@sonu26

Thanks for your inquiry. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

The LayoutCollector.GetEntity method returns an opaque position of the LayoutEnumerator which corresponds to the specified node. You can use returned value as an argument to Current given the document being enumerated and the document of the node are the same.

Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (paragraph.GetAncestor(NodeType.Table) == null)
    {
        enumerator.Current = collector.GetEntity(paragraph);
        Console.WriteLine(enumerator.Rectangle);
    }
}

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    enumerator.Current = collector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
    Console.WriteLine(enumerator.Rectangle);
}

Thanks Tahir for the response, but it doesn’t serves my requirement, as I have to get bounds of whole paragraph i.e.:

This is a sample paragraph section:

2.4. Supplier is appointed to provide particular services to Customer10 related to facilities management (hard & soft) indicatively maintenance services, helpdesk service, environmental & energy management, security services, cleaning services, waste removal & pest control, mail, messenger & portering services, business centre management, reception & customer services, meeting & conference room management, audio visual services, catering –hospitality services and 3rd parties contracts management for Customer10 Premises & Assets, as described in Schedule 5.

194.86300659179688
199.6970067024231
591.2639770507812
601.6129770278931

I want to get position of start character i.e. ‘2’ and end character i.e. ‘.’ of paragraph.

Or maybe I am not getting correct. Please help.

@sonu26

Thanks for your inquiry. All text of the document is stored in runs of text. If you need to navigate to a Run of text then you can insert bookmark right before it and then navigate to the bookmark instead.

In your case, we suggest you please insert the bookmark at the end of paragraph and get the position of “.”. You can use the same approach shared in my previous post to get the position of “.”. You need to pass BookmarkStart node in LayoutCollector.GetEntity method. Hope this helps you.

@Tahir thanks for earlier idea. I tried to implement the same but I am getting following issue, please help me through the same:

Code:

            Integer bookmarkIndex = 1;

        for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {

            String paragraphText = paragraph.toString(SaveFormat.TEXT).trim();

            if (paragraphText == null || paragraphText.isEmpty()) {
                continue;
            }

            if (paragraph.getParagraphFormat().getStyle().isHeading() || paragraph.getAncestor(NodeType.TABLE) != null) {
                continue;
            }

            builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
            builder.startBookmark("Bookmark" + bookmarkIndex);

            Integer paragraphListLevelNumber = paragraph.getListFormat().getListLevelNumber();
            Node sibling = paragraph.getNextSibling();
            Paragraph endSiblingPara = paragraph;
            while (sibling != null && sibling.getNodeType() == NodeType.PARAGRAPH && ((Paragraph) sibling).
                    getListFormat().getListLevelNumber() > paragraphListLevelNumber) {
                endSiblingPara = (Paragraph)sibling;
                sibling = sibling.getNextSibling();
            }
            builder.moveToParagraph(paragraphs.indexOf(endSiblingPara), paragraphText.length() - 1);
            builder.endBookmark("Bookmark" + bookmarkIndex);
            bookmarkIndex++;
        }

Error:
java.lang.IllegalArgumentException: Parameter name: paraIdx
at com.aspose.words.DocumentBuilder.zzZ(Unknown Source)
at com.aspose.words.DocumentBuilder.moveToParagraph(Unknown Source)

@sonu26

Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.