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

Free Support Forum - aspose.com

Check If Page Content is Out of Page Valid Rectangle using C# .NET | Table Text Image Positions in Word Document's Section

hi team,

i have a problem how to check the content is out of page valid rectangle, e.g.image.png (42.2 KB) .

for some reason, we put all the content of the question into one table, the table set preferred width 100% percentage, not set auto fit contents, window or fixed column width property, which case when i insert too many space, text or big picture, graph will enlarge the table width out of the page valid rectangle, it’s acceptable to us.

but i want to know how to detect which page is out of rectangle using Aspose API, like LayoutEnumerator or some method, could you provide a demo code.

Aspose word 19.8

The follow is test paper: paper a.zip (56.5 KB)

Looking forward your reply, thks!

@TommyZhou,

You can build logic on the following code that determines if any character is rendering beyond its Section’s width or not. If yes, then it will print it along with its page number:

Document doc = new Document("E:\\Temp\\paper a\\paper a.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Section sec in doc.Sections)
{
    Node[] runs = sec.Body.GetChildNodes(NodeType.Run, true).ToArray();
    for (int i = 0; i < runs.Length; i++)
    {
        Run run = (Run)runs[i];
        int length = run.Text.Length;

        Run currentNode = run;
        for (int x = 1; x < length; x++)
        {
            currentNode = SplitRun(currentNode, 1);
        }
    }
}

int j = 0;
foreach (Section sec in doc.Sections)
{
    NodeCollection oneCharacterRunNodes = sec.Body.GetChildNodes(NodeType.Run, true);
    for (int i = 0; i < oneCharacterRunNodes.Count; i++)
    {
        Run run = (Run)oneCharacterRunNodes[i];
        builder.MoveTo(run);
        builder.StartBookmark("bm_" + j);
        BookmarkEnd end = builder.EndBookmark("bm_" + j);
        run.ParentNode.InsertAfter(end, run);
        j++;
    }
}

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

float testMe = 0;
foreach (Bookmark bm in doc.Range.Bookmarks)
{
    if (bm.Name.StartsWith("bm_"))
    {
        enumerator.Current = collector.GetEntity(bm.BookmarkStart);
        enumerator.MoveNext();

        float left = enumerator.Rectangle.Left;
        float width = enumerator.Rectangle.Width;

        if ((left + width) > testMe)
            testMe = (left + width);

        Section sec = (Section)bm.BookmarkStart.NextSibling.GetAncestor(NodeType.Section);
        if ((left + width) > (sec.PageSetup.PageWidth - sec.PageSetup.RightMargin))
            Console.WriteLine("Outside at Page # " + collector.GetStartPageIndex(bm.BookmarkStart.NextSibling) + " Text:" + bm.Text);
    }
}

@TommyZhou,

Also, there is a Table on second page that extends beyond the Page’s valid rectangle. But, the code in my previous post does not detect it. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-20718. We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

Thanks for your reply, looking forward your good news.

@TommyZhou,

Sure, we will inform you via this thread as soon as this issue will get resolved in future or any more updates may be available.