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