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

Free Support Forum - aspose.com

How to check current SDT Content is Splited in two pages?

Hii team,
How can i check the current sdt content is splited in two or more pages ?

Snippet :

Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\\SDT_Check.docx");
var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)
    .Where(x => ((Aspose.Words.Markup.StructuredDocumentTag)x).Tag.StartsWith("Rangescop_"));

Attachments :
SDT_Check.docx (26.7 KB)

And I want to add bookmark to the before sdt first child of first page and next page.

@AlpeshChaudhari12345 You can use LayoutCollector to detect page index where some node starts or ends. In your case you can use code like the following to insert a bookmark into SDT at the beginning of each page of SDT content:

Document doc = new Document(@"C:\Temp\in.docx");
LayoutCollector collector = new LayoutCollector(doc);

StructuredDocumentTag sdt = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(x => x.Tag.StartsWith("Rangescop_")).FirstOrDefault();

// Check page number wherre SDT starts and ends.
int sdtPageStart = collector.GetStartPageIndex(sdt);
int sdtPageEnd = collector.GetEndPageIndex(sdt);

if (sdtPageStart != sdtPageEnd)
{
    // Split all Run nodes in the SDT to make them not more than one word.
    List<Run> runs = sdt.GetChildNodes(NodeType.Run, true).Cast<Run>().ToList();
    foreach (Run r in runs)
    {
        Run current = r;
        while (current.Text.IndexOf(' ') >= 0)
            current = SplitRun(current, current.Text.IndexOf(' ') + 1);
    }

    // Now update page layout and reset LayoutCollector to work with the updated document model.
    collector.Clear();
    doc.UpdatePageLayout();

    NodeCollection updatedRuns = sdt.GetChildNodes(NodeType.Run, true);
    int currentPageIndex = -1;
    // Loop through the runs and detect where page index changes.
    for (int i = 0; i < updatedRuns.Count; i++)
    {
        // Insert  bookmark before the Run where page index changes.
        Node currnetRun = updatedRuns[i];
        int runPageIndex = collector.GetStartPageIndex(currnetRun);
        if (runPageIndex != currentPageIndex)
        {
            BookmarkStart start = new BookmarkStart(doc, string.Format("sdt_start_{0}", runPageIndex));
            BookmarkEnd end = new BookmarkEnd(doc, start.Name);
            currnetRun.ParentNode.InsertBefore(start, currnetRun);
            currnetRun.ParentNode.InsertBefore(end, currnetRun);
            currentPageIndex = runPageIndex;
        }
    }
}

doc.Save(@"C:\Temp\out.docx");
private static Run SplitRun(Run run, int position)
{
    Run afterRun = (Run)run.Clone(true);
    run.ParentNode.InsertAfter(afterRun, run);
    afterRun.Text = run.Text.Substring(position);
    run.Text = run.Text.Substring(0, position);
    return afterRun;
}

out.docx (21.9 KB)

@alexey.noskov thanks…

1 Like