Hi,
I have a bookmark in the document that contains a table.
When i trying to get bookmark’s content, the table is nor extracted but only cell’s content. How it can be fixed?
Bookmark Bookmark = mDocument.Range.Bookmarks[“START184634325815091826172F”];
BookmarkStart bookmarkStart = Bookmark.BookmarkStart;
BookmarkEnd bookmarkEnd = Bookmark.BookmarkEnd;ArrayList extractedNodes = ExtractContent(bookmarkStart, bookmarkEnd, false);
Aspose.Words.Document srcBookmarkContent = GenerateDocument(mDocument, extractedNodes);
srcBookmarkContent.Save(“output.doc”);
Here the ExtractContent method that was fix by Aspose after one of my posts.
private ArrayList ExtractContent(Node startNode, Node endNode, bool isInclusive)
{
// First check that the nodes passed to this method are valid for use.
VerifyParameterNodes(startNode, endNode);while (startNode.NodeType != NodeType.Paragraph && startNode.NodeType != NodeType.Table)</span><span style="background-color: rgb(159, 209, 251); color: green; ">// Create a list to store the extracted nodes.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">ArrayList</span><span style="background-color: rgb(159, 209, 251);"> nodes = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">new</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">ArrayList</span><span style="background-color: rgb(159, 209, 251);">(); </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Keep a record of the original nodes passed to this method so we can split marker nodes if needed.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">Node</span><span style="background-color: rgb(159, 209, 251);"> originalStartNode = startNode; </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">Node</span><span style="background-color: rgb(159, 209, 251);"> originalEndNode = endNode; </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Extract content based on block level nodes (paragraphs and tables). Traverse through parent nodes to find them.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: green; ">// We will split the content of first and last nodes depending if the marker nodes are inline</span><span style="background-color: rgb(159, 209, 251);">
startNode = startNode.ParentNode;currNode = currNode.NextPreOrder(currNode.Document);<span style="color: blue; ">while</span> (endNode.NodeType != <span style="color: rgb(43, 145, 175); ">NodeType</span>.Paragraph && endNode.NodeType != <span style="color: rgb(43, 145, 175); ">NodeType</span>.Table) endNode = endNode.ParentNode;</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">bool</span><span style="background-color: rgb(159, 209, 251);"> isExtracting = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">true</span><span style="background-color: rgb(159, 209, 251);">; </span><span style="background-color: rgb(159, 209, 251); color: blue; ">bool</span><span style="background-color: rgb(159, 209, 251);"> isStartingNode = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">true</span><span style="background-color: rgb(159, 209, 251);">; </span><span style="background-color: rgb(159, 209, 251); color: blue; ">bool</span><span style="background-color: rgb(159, 209, 251);"> isEndingNode = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">false</span><span style="background-color: rgb(159, 209, 251);">; </span><span style="background-color: rgb(159, 209, 251); color: green; ">// The current node we are extracting from the document.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">Node</span><span style="background-color: rgb(159, 209, 251);"> currNode = startNode; </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Begin extracting content. Process all block level nodes and specifically split the first and last nodes when needed so paragraph formatting is retained.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Method is little more complex than a regular extractor as we need to factor in extracting using inline nodes, fields, bookmarks etc as to make it really useful.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">while</span><span style="background-color: rgb(159, 209, 251);"> (isExtracting) { </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Clone the current node and its children to obtain a copy.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">CompositeNode</span><span style="background-color: rgb(159, 209, 251);"> cloneNode = (</span><span style="background-color: rgb(159, 209, 251); color: rgb(43, 145, 175); ">CompositeNode</span><span style="background-color: rgb(159, 209, 251);">)currNode.Clone(</span><span style="background-color: rgb(159, 209, 251); color: blue; ">true</span><span style="background-color: rgb(159, 209, 251);">); isEndingNode = currNode.Equals(endNode); </span><span style="background-color: rgb(159, 209, 251); color: blue; ">if</span><span style="background-color: rgb(159, 209, 251);"> (isStartingNode || isEndingNode) { </span><span style="background-color: rgb(159, 209, 251); color: green; ">// We need to process each marker separately so pass it off to a separate method instead.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">if</span><span style="background-color: rgb(159, 209, 251);"> (isStartingNode) { ProcessMarker(cloneNode, nodes, originalStartNode, isInclusive, isStartingNode, isEndingNode); isStartingNode = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">false</span><span style="background-color: rgb(159, 209, 251);">; } </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Conditional needs to be separate as the block level start and end markers maybe the same node.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">if</span><span style="background-color: rgb(159, 209, 251);"> (isEndingNode) { ProcessMarker(cloneNode, nodes, originalEndNode, isInclusive, isStartingNode, isEndingNode); isExtracting = </span><span style="background-color: rgb(159, 209, 251); color: blue; ">false</span><span style="background-color: rgb(159, 209, 251);">; } } </span><span style="background-color: rgb(159, 209, 251); color: blue; ">else</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Node is not a start or end marker, simply add the copy to the list.</span><span style="background-color: rgb(159, 209, 251);"> nodes.Add(cloneNode); </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Move to the next node and extract it. If next node is null that means the rest of the content is found in a different section.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">if</span><span style="background-color: rgb(159, 209, 251);"> (currNode.NextSibling == </span><span style="background-color: rgb(159, 209, 251); color: blue; ">null</span><span style="background-color: rgb(159, 209, 251);"> && isExtracting) {
<span style="color: blue; ">while</span> (currNode.NodeType != <span style="color: rgb(43, 145, 175); ">NodeType</span>.Paragraph && currNode.NodeType != <span style="color: rgb(43, 145, 175); ">NodeType</span>.Table) currNode = currNode.NextPreOrder(currNode.Document);</b><span style="background-color: rgb(159, 209, 251);"> } </span><span style="background-color: rgb(159, 209, 251); color: blue; ">else</span><span style="background-color: rgb(159, 209, 251);"> { </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Move to the next node in the body.</span><span style="background-color: rgb(159, 209, 251);"> currNode = currNode.NextSibling; } }</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: green; ">// Return the nodes between the node markers.</span><span style="background-color: rgb(159, 209, 251);"> </span><span style="background-color: rgb(159, 209, 251); color: blue; ">return</span><span style="background-color: rgb(159, 209, 251);"> nodes; }</span></pre></div><div><pre style="font-family: 'Courier New'; font-size: 13px; background-color: rgb(159, 209, 251); background-position: initial initial; background-repeat: initial initial; "> <span style="color:blue;">private</span> Aspose.Words.<span style="color:#2b91af;">Document</span> GenerateDocument(Aspose.Words.<span style="color:#2b91af;">Document</span> srcDoc, <span style="color:#2b91af;">ArrayList</span> nodes) { <span style="color:green;">//Remove empty paragraphs from the end of document</span> <span style="color:blue;">if</span> (srcDoc.LastSection.Body.LastChild != <span style="color:blue;">null</span>) { <span style="color:blue;">while</span> (!((<span style="color:#2b91af;">CompositeNode</span>)srcDoc.LastSection.Body.LastChild).HasChildNodes) { srcDoc.LastSection.Body.LastParagraph.Remove(); <span style="color:blue;">if</span> (srcDoc.LastSection.Body.LastChild == <span style="color:blue;">null</span>) <span style="color:blue;">break</span>; } } <span style="color:green;">// Create a blank document.</span> Aspose.Words.<span style="color:#2b91af;">Document</span> dstDoc = <span style="color:blue;">new</span> Aspose.Words.<span style="color:#2b91af;">Document</span>(); <span style="color:green;">// Remove the first paragraph from the empty document.</span> dstDoc.FirstSection.Body.RemoveAllChildren(); <span style="color:green;">// Import each node from the list into the new document. Keep the original formatting of the node.</span> <span style="color:#2b91af;">NodeImporter</span> importer = <span style="color:blue;">new</span> <span style="color:#2b91af;">NodeImporter</span>(srcDoc, dstDoc, <span style="color:#2b91af;">ImportFormatMode</span>.KeepSourceFormatting); <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Node</span> node <span style="color:blue;">in</span> nodes) { <span style="color:#2b91af;">Node</span> importNode = importer.ImportNode(node, <span style="color:blue;">true</span>); dstDoc.FirstSection.Body.AppendChild(importNode); } <span style="color:green;">// Return the generated document.</span> <span style="color:blue;">return</span> dstDoc; }
I am attaching the document.
Thanks,
Stanislav.