hi all,
The word content like:
=================================================
〖start〗xxxxxxx
xxxxxxxx
〖end〗
〖start〗yyyyyyyyyyyyyyy
yyyyyyyyy
〖end〗
〖start〗xxxxxxx
xxxxxxxx
〖end〗
…
===================================================
now, the content of〖start〗to〖end〗 i want to save to different word like 1.docx 2.docx 3.docx …
how to do?
thanks!
Hi there,
Thanks for your inquiry. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
https://docs.aspose.com/words/net/logical-levels-of-nodes-in-a-document/
In your case, I suggest you please implementing IReplacingCallback interface to find all instances of particular word in the document. Please use the following code example to achieve your requirements. This code example does the followings:
- Find all instances of particular word in the document
- Insert bookmark to each resulting match found
- Extract contents based on inserted bookmarks
Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/
https://docs.aspose.com/words/java/find-and-replace/
Hope this helps you. Please let us know if you have any more queries.
// Load in the document
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Find text and insert bookmark
doc.Range.Replace(new Regex("\\[Start\\]"), new FindAndInsertBookmark(), false);
// Add the inserted bookmarks starts with BM_ in an ArrayList
ArrayList bookmarks = new ArrayList();
foreach (Bookmark bm in doc.Range.Bookmarks)
{
if (bm.Name.StartsWith("BM_"))
bookmarks.Add(bm);
}
// Move cursor to document end and insert bookmark
builder.MoveToDocumentEnd();
builder.StartBookmark("BM_" + bookmarks.Count);
builder.EndBookmark("BM_" + bookmarks.Count);
// Extract contents between bookmarks and split the document
for (int i = 0; i < bookmarks.Count - 1; i++)
{
BookmarkStart bStart = ((Bookmark)bookmarks[i]).BookmarkStart;
BookmarkEnd bEnd = ((Bookmark)bookmarks[i]).BookmarkEnd;
ArrayList nodes = ExtractContent(bStart, bEnd, true);
Document newdoc = GenerateDocument(doc, nodes);
newdoc.Save(MyDir + "Out_" + i + ".docx");
}
private class FindAndInsertBookmark : IReplacingCallback
{
int i = 0;
// /
// / This method is called by the Aspose.Words find and insert bookmark
// /
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs 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 removing.
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);
}
// Create Document Builder
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
builder.StartBookmark("BM_" + i);
builder.EndBookmark("BM_" + i);
i++;
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
}
it’s work, thanks
Hi there,
Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.