Not getting all the 4 comments

Bank Danamon Indonesia_MarkUp Doc.docx (62.2 KB)

code.docx (16.9 KB)

I have attached the sample document and code

@Rubal As I can see Aspose.Words properly gets all 4 comments on my side. The following code returns 4:

Document doc = new Document(@"C:\Temp\in.docx");
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
Console.WriteLine(comments.Count);

Also, I see all the 4 comments in the document structure:

getting blank comments for OCC_8de0a26618b34681b19de2798611948a and OCC_30e6c0d1f5f54daebb1516a71d449909

@Rubal Unfortunately, I cannot reproduce the problem. I have simplified your code for testing and all 4 comments have been found:

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

// Get SDTs
List<StructuredDocumentTag> sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)
    .Cast<StructuredDocumentTag>()
    .Where(x => x.Title.Contains("OCC_")).ToList();

foreach (StructuredDocumentTag sdt in sdts)
{
    Console.WriteLine(sdt.Title);

    Comment comment = sdt.GetChildNodes(NodeType.Comment, true)
        .Cast<Comment>()
        .Where(x => x.GetText().Contains(sdt.Title)).FirstOrDefault();

    if (comment != null)
        Console.WriteLine(comment.GetText().Trim());
    else
        Console.WriteLine("Comment not found");

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

Bank Danamon Indonesia_MarkUp Doc (1).docx (42.7 KB) – Output file

Bank Danamon Indonesia_MarkUp Doc.docx (62.2 KB) – input file

code.docx (16.9 KB) – code

For first two bookmarks I ma getting the blank text which is in correct … Thr boundary of sdt exists

Can you please help on urgent basis|?

@Rubal Do you need to wrap each comment in SDT into the bookmark? If so you can simplify your code like this:

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

// Get SDTs
List<StructuredDocumentTag> sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)
    .Cast<StructuredDocumentTag>()
    .Where(x => x.Title.Contains("OCC_")).ToList();

foreach (StructuredDocumentTag sdt in sdts)
{
    Comment comment = sdt.GetChildNodes(NodeType.Comment, true)
        .Cast<Comment>()
        .Where(x => x.GetText().Contains(sdt.Title)).FirstOrDefault();

    if (comment != null)
    {
        comment.ParentNode.InsertBefore(new BookmarkStart(doc, sdt.Title), comment);
        comment.ParentNode.InsertAfter(new BookmarkEnd(doc, sdt.Title), comment);
    }
    else
    {
        Console.WriteLine($"Comment not found {sdt.Title}");
    }

    // Remove SDT.
    sdt.RemoveSelfOnly();
}

doc.Save(@"C:\Temp\out.docx");

It is not quite clear what is the expected output. Could you please elaborate your requirements in more details.

The test of bookmark we are getting is blank…our purpose is small remove sdk and insert bookmark in that range

the range of contentcontrol to be replaced with bookmark

@Rubal If it is required wrap whole SDT content into a bookmark and then remove SDT, you can use the following code:

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

// Get SDTs
List<StructuredDocumentTag> sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)
    .Cast<StructuredDocumentTag>()
    .Where(x => x.Title.Contains("OCC_")).ToList();

foreach (StructuredDocumentTag sdt in sdts)
{
    // Wrap content of the SDT into a bookmark
    sdt.PrependChild(new BookmarkStart(doc, sdt.Title));
    sdt.AppendChild(new BookmarkEnd(doc, sdt.Title));

    // Remove SDT.
    sdt.RemoveSelfOnly();
}

doc.Save(@"C:\Temp\out.docx");

Bank Danamon Indonesia_MarkUp Doc.docx (62.2 KB)

I basically wants to move to the end of the sdt … But with the above code it is taking me the end of one paragraph …

@Rubal You can use DocumentBuilder.MoveToStructuredDocumentTag method to achieve this. For example see the following code:

builder.MoveToStructuredDocumentTag(sdt, sdt.Level == MarkupLevel.Block ? -2 : -1);