Bookmark Help

Hi,
We use aspose documentbuilder’s StartBookMark and EndBookMark methods for inserting bookmark into a document. It works fine in all the situations except when writting RTF content to the document. We write RTF content to the document using the method given by Aspose Tech support.
Please see the below code to find how we write RTF content. Please advice how to insert a bookmark when we write RTF to document.
Code to insert Book Mark in normal scenario*

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.Write(counter.ToString());
builder.StartBookmark(bookmarkID);
builder.EndBookmark(bookmarkID);
builder.EndRow();
builder.EndTable();

Code to insert RTF string**

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
// stream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
Document rtfDoc = new Document(rtfStream);
rtfStream.Close();
rtfStream.Dispose();
// Insert rtd document into destination document
InsertDocument(builder.CurrentParagraph, rtfDoc);
rtfDoc = null;
builder.CurrentParagraph.Remove();
/// insert document method
private void InsertDocument(Node insertAfterNode, Document srcDoc)
{
    try
    {
        // We need to make sure that the specified node is either pargraph or table.
        if (!((insertAfterNode.NodeType == NodeType.Paragraph) ||
        (insertAfterNode.NodeType == NodeType.Table)))
            throw new Exception();
        // We will be inserting into the parent of the destination paragraph.
        CompositeNode dstStory = insertAfterNode.ParentNode;
        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
        ImportFormatMode.KeepSourceFormatting);
        // Loop through all sections in the source document.
        foreach (Section srcSection in srcDoc.Sections)
        {
            // Loop through all block level nodes (paragraphs and tables) in the body of the section.
            foreach (Node srcNode in srcSection.Body)
            {
                // Do not insert node if it is a last empty paragarph in the section.
                Paragraph para = srcNode as Paragraph;
                if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                    break;
                // This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = importer.ImportNode(srcNode, true);
                // Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
    catch
    {
        throw;
    }
}

Please advice where can use the code to insert bookmark with RTF string.
Thank you,
Ali

Hi
Thanks for your inquiry. I think the problem occurs because you remove builder.CurrentParagraph. You can resolve this problem using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Cell rtfCell = builder.InsertCell();
byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
// stream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
Document rtfDoc = new Document(rtfStream);
rtfStream.Close();
rtfStream.Dispose();
// Insert rtd document into destination document
InsertDocument(builder.CurrentParagraph, rtfDoc);
rtfDoc = null;
builder.CurrentParagraph.Remove();
// Move DocumentBuilderCursor to the last paragraph of the cell
builder.MoveTo(rtfCell.LastParagraph);
// Insert bookmark
builder.StartBookmark("myBk");
builder.EndBookmark("myBk");
builder.EndRow();
// Insert next cell
builder.InsertCell();
builder.Write("Test");
builder.EndRow();
builder.EndTable();
doc.Save(@"Test211\out.doc");

I highlighted my changes.
Hope this helps.
Best regards.

Hi,
Thanks for the solution. But what if I traverse through the cells by using MoveToCell() method, is there any way to get the reference of the present cell. Please refer the below code

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int row = 0; row < 5; row++)
{
    builder.InsertCell();
    builder.EndRow();
}
for (int row = 0; row < 5; row++)
{
    builder.MoveToCell(0, row, 0, 0);
    if (dataString is OrdinaryText)
    {
        builder.Write(dataString);
    }
    else if (dataString is RTFText)
    {
        byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
        // stream
        MemoryStream rtfStream = new MemoryStream(rtfBytes);
        Document rtfDoc = new Document(rtfStream);
        rtfStream.Close();
        rtfStream.Dispose();
        // Insert rtd document into destination document
        InsertDocument(builder.CurrentParagraph, rtfDoc);
        rtfDoc = null;
        builder.CurrentParagraph.Remove();
    }
}

Is there any way to implement the bookmark in this case? This is very much similar to our business requirement.
Thanks in advance,
Ali

Hi
Thanks for your request. You can get reference to cell using GetAncestor method. See the following code example:

else if (dataString is RTFText)
{
    byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
    // stream
    MemoryStream rtfStream = new MemoryStream(rtfBytes);
    Document rtfDoc = new Document(rtfStream);
    rtfStream.Close();
    rtfStream.Dispose();
    // Insert rtd document into destination document
    InsertDocument(builder.CurrentParagraph, rtfDoc);
    rtfDoc = null;
    Cell currentCell = builder.CurrentParagraph.GetAncestor(NodeType.Cell) as Cell;
    builder.CurrentParagraph.Remove();
    if (currentCell != null)
    {
        builder.MoveTo(currentCell.LastParagraph);
        // Insert bookmark
        builder.StartBookmark("myBk");
        builder.EndBookmark("myBk");
    }
}

Hope this helps.
Best regards.

Hi,
Thanks for the solution it works fine for our business needs.
Thanks in advance,
Ali