I am having a problem with moving the contents of one document to a final document.
The problem I am experiencing is when the source document has bullets at the very beginning of the file.
Attached are the two documents.
Here is my code to do the insert of one doc into the other.
Document srcDoc = new Document("Bullet.doc");
Document destDoc = new Document("DestDoc.doc");
Node currentPosition = destDoc.Range.Bookmarks["AgenSum_TEXTAREA_CS"].BookmarkStart;
if (srcDoc.Sections.Count = 1)
{
// If there is only one section in the rendered component, insert the section's contents
// immediately after the bookmark
InsertNodes(currentPosition, srcDoc.FirstSection);
}
public static void InsertNodes(Node currentPosition, Section renderedSection)
{
// Add the rendered section after the bookmark
BookmarkStart bs = (BookmarkStart)currentPosition;
BookmarkEnd be = (BookmarkEnd)bs.Bookmark.BookmarkEnd;
NodeCollection existingNodes = be.ParentNode.ParentNode.ChildNodes;
Node lastInsertedParagraph = null;
int index = existingNodes.IndexOf(be.ParentNode);
// If there is more than one paragraph to import and if the paragraph where we will be inserting
// has any (residual) children after the bookmark end, then those residual children will have
// to be added as siblings at the end of the childnode collection of the last imported paragraph.
List residuals = new List();
NodeCollection childrenToImport = renderedSection.Body.ChildNodes;
int totChildrenToImport = childrenToImport.Count;
int indexOfBe = be.ParentNode.IndexOf(be);
int indexOfSiblingAfterBe = indexOfBe + 1;
int totBeSiblings = be.ParentNode.ChildNodes.Count;
if (totChildrenToImport > 1 && totBeSiblings > 1 && indexOfSiblingAfterBe < totBeSiblings)
{
// Collect the residual children
for (int i = indexOfSiblingAfterBe; i < totBeSiblings; i++)
{
residuals.Add(be.ParentNode.ChildNodes[i]);
}
// Remove the residuals from the template
foreach (Node node in residuals)
{
be.ParentNode.ChildNodes.Remove(node);
}
}
Aspose.Words.Paragraph paraOfBookmarkEnd = null;
foreach (Node sourceNode in renderedSection.Body.ChildNodes)
{
// Only Aspose.Words.Paragraph or Table nodes can be inserted into Cell or Shape
if (sourceNode.NodeType == NodeType.Paragraph || sourceNode.NodeType == NodeType.Table)
{
// Do not insert node if it is a last empty paragraph in the section.
if (sourceNode.NodeType == NodeType.Paragraph && sourceNode == renderedSection.Body.LastChild && sourceNode.GetText() == string.Empty)
break;
// if the node is the first node, add its children to the existing paragraph
if (sourceNode == renderedSection.Body.FirstChild)
{
if (sourceNode.NodeType == NodeType.Paragraph)
{
paraOfBookmarkEnd = (Aspose.Words.Paragraph)existingNodes[index];
int beIndex = paraOfBookmarkEnd.IndexOf(be);
foreach (Node run in ((CompositeNode)sourceNode))
{
if (run.NodeType == NodeType.Shape)
{
//// If we're dealing with a Shape, assume its an embedded grid and set the paragraphs
//// right indent to the far right. This should keep the paragraph from creating an unwanted line.
Aspose.Words.Paragraph paraOfBookmarkStart = (Aspose.Words.Paragraph)bs.GetAncestor(NodeType.Paragraph);
if (paraOfBookmarkStart.ParagraphFormat.Alignment != ParagraphAlignment.Center)
{
// Only if not center aligned (otherwise will offset from center)
PageSetup setup = ((Section)be.GetAncestor(NodeType.Section)).PageSetup;
paraOfBookmarkStart.ParagraphFormat.RightIndent = -setup.RightMargin;
}
}
Node insertedNode = currentPosition.Document.ImportNode(run, true, ImportFormatMode.UseDestinationStyles);
paraOfBookmarkEnd.ChildNodes.Insert(++beIndex, insertedNode);
}
}
else
{
// Here we assume its a Table
lastInsertedParagraph = currentPosition.Document.ImportNode(sourceNode, true, ImportFormatMode.UseDestinationStyles);
existingNodes.Insert(++index, lastInsertedParagraph);
}
}
else
{
lastInsertedParagraph = currentPosition.Document.ImportNode(sourceNode, true, ImportFormatMode.UseDestinationStyles);
// TODO: Do we need this for OSD? If it breaks Army/AF may want to try removing it.
if (sourceNode.NodeType == NodeType.Paragraph) //Only do CopyFont if is paragraph
{
if (paraOfBookmarkEnd != null)
{
((Aspose.Words.Paragraph)lastInsertedParagraph).ParagraphFormat.Style = paraOfBookmarkEnd.ParagraphFormat.Style;
}
}
existingNodes.Insert(++index, lastInsertedParagraph);
}
}
}
// Add back the residuals
if (residuals.Count > 0)
{
foreach (Node run in residuals)
{
// Always keep the source formatting when adding back the residuals
Node insertedRun = currentPosition.Document.ImportNode(run, true, ImportFormatMode.KeepSourceFormatting);
((CompositeNode)lastInsertedParagraph).ChildNodes.Insert(++index, insertedRun);
}
}
}
then I call this method to remove the book mark.
RemoveBookmark(currentPosition, "AgenSum_TEXTAREA_CS")
private void RemoveBookmark(Node currentPosition, string bookMark)
{
Table newTable = null;
DocumentBuilder docBuilder = new DocumentBuilder(_template);
BookmarkStart bs = (BookmarkStart)currentPosition;
docBuilder.MoveToBookmark(bs.Bookmark.Name);
// If a table was the first thing added to the rendered component, get a reference to the table
// that was merged into the template.
if ((AsposeUtil.IsEmptyParagraph(_renderedComponent.FirstSection.Body.FirstChild) &&
_renderedComponent.Sections.Count > 1 &&
_renderedComponent.Sections[1].Body.FirstChild.NodeType == NodeType.Table))
{
if (docBuilder.CurrentNode.ParentNode.NextSibling.NextSibling != null &&
docBuilder.CurrentNode.ParentNode.NextSibling.NextSibling.NodeType == NodeType.Table)
{
newTable = (Table)docBuilder.CurrentNode.ParentNode.NextSibling.NextSibling;
}
else if (docBuilder.CurrentNode.ParentNode.NextSibling != null &&
docBuilder.CurrentNode.ParentNode.NextSibling.NodeType == NodeType.Table)
{
newTable = (Table)docBuilder.CurrentNode.ParentNode.NextSibling;
}
}
else if (_renderedComponent.FirstSection.Body.FirstChild.NodeType == NodeType.Table)
{
newTable = (Table)docBuilder.CurrentNode.ParentNode.NextSibling.NextSibling;
}
// Remove the bookmark we just processed.
Bookmark bookmark = _template.Range.Bookmarks[bookMark];
bookmark.Text = "";
bookmark.Remove();
if (newTable != null)
{
// If a table was the first thing added to the built component, we
// must remove the paragraph break that preceeds the table. Normally
// removing the bookmark will remove the embedded paragraph breaks but Aspose
// leaves one if a table is added.
if (newTable.PreviousSibling.NodeType == NodeType.Paragraph &&
AsposeUtil.IsEmptyParagraph(newTable.PreviousSibling))
{
newTable.PreviousSibling.Remove();
}
}
}
After all this, the first bullet is removed but it’s text is still there, and only the second two bullets are shown as bullets. I think it has something to do with the fact that I am adding bullets on the same line as bookmark, but don’t understand why it happens or how to fix it. When I print document mid insertdoc, it see book mark and then the #1 next to it without the bullet, so I know it has nothing to do with the RemoveBookmark method.
Thanks for your help in advance.