Hi guys,
Do any of you know how to reproduce the behaviour of the MSWord function “Selection.InsertFile” ?
Thanks,
Elm
In Aspose.Word you should use the Document.ImportNode method to reproduce the behaviour of MS Word Selection.InsertFile.
For example, you can extract the first section from one document by:
Section section = (Section)destinationDocument.ImportNode(sourceDocument.Sections[0], true);
and insert it at the beginning of another document by:
destinationDocument.Sections.Insert(0, section);
Thanks, I’ll try that.
Elm
In word, whatever text is currently selected is replaced by the inserted document.
How can I do similar with Aspose.Word?
Do I need to create a new section divided around the selected text? if so how?
Thanks for your inquiry. You can achieve this using ReplaceEvaluator. Using Replace method you can find a text, which should be replaced and using ReplaceEvaluatior, you can replace this text with whatever you want.
Please see the following link to learn how to insert one document into another.
https://docs.aspose.com/words/net/insert-and-append-documents/
Also I think information provided in the following forum thread could be useful for you.
https://forum.aspose.com/t/105730
Please let me know in case of any issues, I will be glad to help you.
Best regards.
Hi,
Your sample had some bugs which I’ve fixed and included below.
I really think though that this is so simple to do with Microsoft Word API - why not take the code and simplify your own API by offering the functionality directly? I thought that was the point of Aspose.Word? It should make it at least as easy if not easier as it is using Microsoft API’s? At the moment this is far from trivial, and probably a common requirement. I think it shouldn’t be necessary to use replace either, just Selection.InsertFile() would do if you could overwrite the current selection with a file.
// This takes the found string and replaces it with the contents of a document
private ReplaceAction MyReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}
// Now remove all text from the runs
foreach (Run run in runs)
run.Text = "";
Document insertDoc = new Document(_fileNameToInsert);
_builder.MoveTo(e.MatchNode);
// Insert Paragraph break
_builder.InsertBreak(BreakType.ParagraphBreak);
// Remove previouse paragraph if it is empty
Paragraph para = (Paragraph)_builder.CurrentParagraph.PreviousSibling;
// Paragraph para = ((Run)runs[0]).ParentParagraph; //(Paragraph)e.MatchNode.ParentNode;
InsertDocument(para, insertDoc);
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
public void InsertDocument(Node insertAfterNode, Document docToInsert)
{
Document dstDoc = (Document)insertAfterNode.Document;
// Content of first paragraph of srcDoc will be apended to this parafraph
Paragraph insertAfterParagraph = insertAfterNode as Paragraph;
// Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = insertAfterNode.NextSibling as Paragraph;
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// Remove empty paragraphs from the end of document
while ((docToInsert.LastSection.Body.LastParagraph != null) && !docToInsert.LastSection.Body.LastParagraph.HasChildNodes)
{
docToInsert.LastSection.Body.LastParagraph.Remove();
}
// Loop through all sections in the source document.
foreach (Section srcSection in docToInsert.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;
}
// If current paragraph is first paragraph of srcDoc
// then appent its content to insertAfterParagraph
if (srcNode.Equals(docToInsert.FirstSection.Body.FirstParagraph))
{
foreach (Node node in para.ChildNodes)
{
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
insertAfterParagraph.AppendChild(dstNode);
}
// If subdocument contains only one paragraph
// then copy content of insertBeforeParagraph to insertAfterParagraph
// and remove insertBeforeParagraph
if (docToInsert.FirstSection.Body.FirstParagraph.Equals(docToInsert.LastSection.Body.LastParagraph))
{
while (insertBeforeParagraph.HasChildNodes)
{
insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
}
insertBeforeParagraph.Remove();
}
}
// If current paragraph is last paragraph of srcDoc
// then appent its content to insertBeforeParagraph
else if (srcNode.Equals(docToInsert.LastSection.Body.LastParagraph))
{
Node previousNode = null;
foreach (Node node in para.ChildNodes)
{
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
if (previousNode == null)
{
insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
}
else
{
insertBeforeParagraph.InsertAfter(dstNode, previousNode);
}
previousNode = dstNode;
}
}
else
{
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
}
///
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
///
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
Hi
Thanks for your inquiry. Actually, Aspose.Words does not use MS Word API at all. Also, there is no “Selection” concept in Aspose.Words. Please see Aspose.Words DOM for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
Best regards.