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

Free Support Forum - aspose.com

How to convert bookmark to content control?

Hi,
I want create content control with bookmark content. is there any option for converting or inserting content control for a selected bookmark?

It would be great if you could provide any sample?

Regards
Sathiyamurthy A
Merrill Corporation

Hi Sathiyamurthy,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference. We will then provide you more information about your query along with code.

Hi,
Thanks for your reply.
INPUT: DOCX file with word TOC. For each TOC items there is a content in the document.
So each content has is own bookmark.

OUTPUT: Same file with content control(Rich text) for each and every bookmark and unique content control name.

I attached expected input and output docx file for you reference.

In simple I want to create content control with bookmark content.

Regards
Sathiyamurthy A
Merrill Corporation

Hi Sathiyamurthy,

Thanks for sharing the detail. We are working over your query and will get back to you soon.

Hi,
Hope I will get sample code…

Regards
Sathiyamurthy A
Merrill Corporation

Hi Sathiyamurthy,

Thanks for your patience. In your case, we suggest you following solution.

  1. Extract the TOC contents and save them into separate document
  2. Remove all contents after TOC field from document
  3. Insert content control at the end of document and insert extracted contents

Please read following articles for your kind reference.
How to Extract Selected Content Between Nodes in a Document
Working with Content Control (SDT)

Please get the code of ExtractContent and GenerateDocument methods from above documentation link and use following code example. You may modify the code example according to your requirements. Hope this helps you.

Document doc = new Document(MyDir + "Input.docx");
doc.UpdateFields();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.StartBookmark("_TocEnd");
builder.EndBookmark("_TocEnd");
NodeCollection nodes = doc.GetChildNodes(NodeType.FieldStart, true);
ArrayList tocitems = new ArrayList();
foreach (FieldStart fstart in nodes)
{
    if (fstart.FieldType == Aspose.Words.Fields.FieldType.FieldPageRef)
    {
        String fieldText = fstart.GetField().GetFieldCode();
        if (fieldText.Contains("_Toc"))
        {
            fieldText = fieldText.Substring(fieldText.IndexOf("_Toc"), fieldText.Length - fieldText.IndexOf("_Toc")).Replace("\\h", "").Trim();
            tocitems.Add(fieldText);
        }
    }
    else if (fstart.FieldType == FieldType.FieldTOC)
    {
        if (fstart.GetAncestor(NodeType.StructuredDocumentTag) != null)
        {
            builder.MoveTo(fstart.GetAncestor(NodeType.StructuredDocumentTag).NextSibling);
            builder.StartBookmark("TOCField");
            builder.EndBookmark("TOCField");
        }
        else
        {
            builder.MoveTo(fstart.ParentParagraph.NextSibling);
            builder.StartBookmark("TOCField");
            builder.EndBookmark("TOCField");
        }
    }
}
tocitems.Add("_TocEnd");
ArrayList contents = new ArrayList();
for (int i = 0; i < tocitems.Count - 1; i++)
{
    BookmarkStart bookmarkStart = doc.Range.Bookmarks[tocitems[i].ToString()].BookmarkStart;
    BookmarkStart bookmarkEnd = doc.Range.Bookmarks[tocitems[i + 1].ToString()].BookmarkStart;
    // firstly extract the content between these nodes including the bookmark.
    ArrayList extractedNodes = ExtractContent(bookmarkStart, bookmarkEnd, false);
    Document doc2 = GenerateDocument(doc, extractedNodes);
    doc2.UpdatePageLayout();
    // Remove the empty paragraphs if necessary.
    while (!doc2.LastSection.Body.LastParagraph.HasChildNodes)
    {
        if (doc2.LastSection.Body.LastParagraph.PreviousSibling != null &&
        doc2.LastSection.Body.LastParagraph.PreviousSibling.NodeType != NodeType.Paragraph)
            break;
        doc2.LastSection.Body.LastParagraph.Remove();
        // If the current section becomes empty, we should remove it.
        if (!doc2.LastSection.Body.HasChildNodes)
            doc2.LastSection.Remove();
        if (doc2.LastSection.Body.LastParagraph.Runs.Count == 1 && doc2.LastSection.Body.LastParagraph.Runs[0].Text == ControlChar.PageBreak)
            doc2.LastSection.Body.LastParagraph.Remove();
        // We should exit the loop if the document becomes empty.
        if (!doc2.HasChildNodes)
            break;
    }
    MemoryStream stream = new MemoryStream();
    doc2.Save(stream, SaveFormat.Docx);
    stream.Position = 0;
    contents.Add(stream);
}
// Remove document's contents after TOC field.
builder.MoveToBookmark("TOCField");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.StartBookmark("removecontents");
builder.MoveToDocumentEnd();
builder.EndBookmark("removecontents");
doc.Range.Bookmarks["removecontents"].Text = "";
builder.MoveToDocumentEnd();
builder.Writeln();
foreach (MemoryStream item in contents)
{
    StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
    tag.RemoveAllChildren();
    tag.AppendChild(new Paragraph(doc));
    builder.MoveToDocumentEnd();
    doc.LastSection.Body.AppendChild(tag);
    builder.MoveTo(tag.FirstChild);
    builder.InsertDocument(new Document(item), ImportFormatMode.KeepSourceFormatting);
    doc.LastSection.Body.AppendChild(new Paragraph(doc));
    builder.MoveToDocumentEnd();
    builder.InsertBreak(BreakType.PageBreak);
}
doc.Range.Bookmarks["removecontents"].BookmarkStart.ParentNode.Remove();
doc.UpdateFields();
doc.Save(MyDir + "Out.docx");

Hi,
Thanks lot… its working.
I need to check in details…
If required i will get back to you.

But you are rocking…

Regards
Sathiyamurthy A
Merrill Corporation