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

Free Support Forum - aspose.com

Adding content controls around text within bookmark

Hi,

I want to add content controls around existing text within my word document. I have bookmarks around the text to get the range of the text around which I want to add a bookmark.

Below is a code snippet I tried and somewhat helped me achieve the desired output.

    private void createContentControlAroundBookmarks(Word._Application WordApp)
    {
        Word.Document WordDoc = WordApp.ActiveDocument;
        Word.Bookmarks allBookMarks = null;
        Word.Range tempRange = null;
        try
        {
            allBookMarks = WordDoc.Bookmarks;
            allBookMarks.ShowHidden = true;
            if (allBookMarks != null)
            {
                clauseIds = new List<string>();
                foreach (Word.Bookmark bookMark in allBookMarks)
                {                      
                    if (bookMark.Name.EndsWith("_B"))
                    {
                        tempRange = bookMark.Range;
                        tempRange.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText);                        
                    }
                }
            }
        }
        finally
        {
            CommonUtils.releaseComObject(allBookMarks);
            CommonUtils.releaseComObject(tempRange);
        }
    }

This works fine in most cases except for a case where range contains ‘\r’.

If my bookmark range constitutes text like “Sentence1. \v\v\r \v Sentence2”. In this case, my content control is added only around “Sentence1” but “Sentence2” is out of my content control.

How can I add content controls properly around my existing text if it contains ‘\r’?

Thanks,

@pri_verd

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query along with code.

Hi,
I have attached a zip containing the input document. The document contains many bookmarks. What is expected is to create content controls around the range within bookmarks whose name ends with “_H”.

With the help of the above code, I am able to create a content control around the text below 1.1 subsection in the doc properly, but in case of the text below 1.2 subsection, the values "USD’ and “Random text” end up being out side my content control boundary. i think it has something to do with the paragraphs or \r.

Could you please help me with the same?

ContentControl Test.zip (11.8 KB)

@pri_verd

Thanks for sharing the document. The code example you shared is not Aspose.Words code. You have only shared the input document. Please share the expected output Word document. We will then write the code example according to your requirement and share it with you. Thanks for your cooperation.

Attached is an output document of what I am currently getting. I have 2 content controls around the subsection texts. But in case of the 2nd subsection texts as u can see, some part of the text is outside the content control.

ContentControl Test Output.zip (17.0 KB)

@pri_verd

Thanks for sharing the document. Please use the following code example to get the desired output.

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

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    if (bookmark.Name.EndsWith("_H"))
    {
        StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
        sdt.RemoveAllChildren();
        Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;
        if (para.NextSibling != null)
        {
            sdt.AppendChild(para.NextSibling.Clone(true));
            para.NextSibling.Remove();
            para.ParentStory.InsertAfter(sdt, para);
        }
                        
    }
}

doc.Save(MyDir + "18.9.docx");