Hi,
I’m currently using the following method to insert a document into a bookmark:
public static void InsertDocumentAtBookmark(String bookmarkName, Document dstDoc, Document srcDoc)
{
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(dstDoc);
//Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName);
builder.Writeln();
// If current paragraph is a list item, we should clear its formating.
if (builder.CurrentParagraph.IsListItem)
builder.ListFormat.RemoveNumbers();
//Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
//Content of first paragraph of srcDoc will be apended to this parafraph
Paragraph insertAfterParagraph = null;
if (insertAfterNode.NodeType == NodeType.Paragraph)
insertAfterParagraph = (Paragraph)insertAfterNode;
//Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = builder.CurrentParagraph;
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
//Remove empty paragraphs from the end of document
while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
{
srcDoc.LastSection.Body.LastParagraph.Remove();
if (srcDoc.LastSection.Body.LastChild == null)
break;
}
//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.
for (int nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
{
Node srcNode = srcSection.Body.ChildNodes[nodeIdx];
//Do not insert node if it is a last empty paragarph in the section.
Paragraph para = null;
if (srcNode.NodeType == NodeType.Paragraph)
para = (Paragraph)srcNode;
if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
break;
//If current paragraph is first paragraph of srcDoc
//then appent its content to insertAfterParagraph
bool nodeInserted = false;
if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
{
nodeInserted = true; // set this flag to know that we already processed this node.
for (int i = 0; i < para.ChildNodes.Count; i++)
{
Node node = para.ChildNodes[i];
Node dstNode = importer.ImportNode(node, true);
insertAfterParagraph.AppendChild(dstNode);
}
//If subdocument contains only one paragraph
//then copy content of insertBeforeParagraph to insertAfterParagraph
//and remove insertBeforeParagraph
if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
{
while (insertBeforeParagraph.HasChildNodes)
{
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
}
insertBeforeParagraph.Remove();
}
}
//If current paragraph is last paragraph of srcDoc
//then appent its content to insertBeforeParagraph
if (para != null && para.Equals(srcDoc.LastSection.Body.LastChild))
{
nodeInserted = true; // set this flag to know that we already processed this node.
Node previouseNode = null;
for (int i = 0; i < para.ChildNodes.Count; i++)
{
Node node = para.ChildNodes[i];
Node dstNode = importer.ImportNode(node, true);
if (previouseNode == null)
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
else
insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
previouseNode = dstNode;
}
}
if (!nodeInserted)
{
//This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true);
//Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
}
The issue I’m having is that for bullet lists, an extra tab is being inserted for each item thus causing each item to be indented to the right (See attachment). The source document was originally in Rich Text Format (RTF). Let me know if you need anymore information.