public void ExtractContentBetweenBookmark() { //ExStart:ExtractContentBetweenBookmark Document doc = new Document(MyDir + "Extract content.docx"); Section section = doc.Sections[0]; section.PageSetup.LeftMargin = 70.85; // Retrieve the bookmark from the document. Bookmark bookmark = doc.Range.Bookmarks["Bookmark1"]; // We use the BookmarkStart and BookmarkEnd nodes as markers. BookmarkStart bookmarkStart = bookmark.BookmarkStart; BookmarkEnd bookmarkEnd = bookmark.BookmarkEnd; // Firstly, extract the content between these nodes, including the bookmark. List <Node> extractedNodesInclusive = ExtractContentHelper.ExtractContent(bookmarkStart, bookmarkEnd, true); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesInclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenBookmark.IncludingBookmark.docx"); // Secondly, extract the content between these nodes this time without including the bookmark. List <Node> extractedNodesExclusive = ExtractContentHelper.ExtractContent(bookmarkStart, bookmarkEnd, false); dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesExclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenBookmark.WithoutBookmark.docx"); //ExEnd:ExtractContentBetweenBookmark }
public void ExtractContentBetweenBlockLevelNodes() { //ExStart:ExtractContentBetweenBlockLevelNodes Document doc = new Document(MyDir + "Extract content.docx"); Paragraph startPara = (Paragraph)doc.LastSection.GetChild(NodeType.Paragraph, 2, true); Table endTable = (Table)doc.LastSection.GetChild(NodeType.Table, 0, true); // Extract the content between these nodes in the document. Include these markers in the extraction. List <Node> extractedNodes = ExtractContentHelper.ExtractContent(startPara, endTable, true); // Let's reverse the array to make inserting the content back into the document easier. extractedNodes.Reverse(); while (extractedNodes.Count > 0) { // Insert the last node from the reversed list. endTable.ParentNode.InsertAfter((Node)extractedNodes[0], endTable); // Remove this node from the list after insertion. extractedNodes.RemoveAt(0); } doc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenBlockLevelNodes.docx"); //ExEnd:ExtractContentBetweenBlockLevelNodes }
public void ExtractContentBetweenParagraphs() { //ExStart:ExtractContentBetweenParagraphs Document doc = new Document(MyDir + "Extract content.docx"); Paragraph startPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 6, true); Paragraph endPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 10, true); // Extract the content between these nodes in the document. Include these markers in the extraction. List <Node> extractedNodes = ExtractContentHelper.ExtractContent(startPara, endPara, true); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodes); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenParagraphs.docx"); //ExEnd:ExtractContentBetweenParagraphs }
public void ExtractContentBetweenRuns() { //ExStart:ExtractContentBetweenRuns Document doc = new Document(MyDir + "Extract content.docx"); Paragraph para = (Paragraph)doc.GetChild(NodeType.Paragraph, 7, true); Run startRun = para.Runs[1]; Run endRun = para.Runs[4]; // Extract the content between these nodes in the document. Include these markers in the extraction. List <Node> extractedNodes = ExtractContentHelper.ExtractContent(startRun, endRun, true); Node node = (Node)extractedNodes[0]; Console.WriteLine(node.ToString(SaveFormat.Text)); //ExEnd:ExtractContentBetweenRuns }
public void ExtractContentBetweenParagraphStyles() { //ExStart:ExtractContentBetweenParagraphStyles Document doc = new Document(MyDir + "Extract content.docx"); // Gather a list of the paragraphs using the respective heading styles. List <Paragraph> parasStyleHeading1 = ExtractContentHelper.ParagraphsByStyleName(doc, "Heading 1"); List <Paragraph> parasStyleHeading3 = ExtractContentHelper.ParagraphsByStyleName(doc, "Heading 3"); // Use the first instance of the paragraphs with those styles. Node startPara1 = parasStyleHeading1[0]; Node endPara1 = parasStyleHeading3[0]; // Extract the content between these nodes in the document. Don't include these markers in the extraction. List <Node> extractedNodes = ExtractContentHelper.ExtractContent(startPara1, endPara1, false); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodes); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenParagraphStyles.docx"); //ExEnd:ExtractContentBetweenParagraphStyles }
public void ExtractContentUsingField() { //ExStart:ExtractContentUsingField Document doc = new Document(MyDir + "Extract content.docx"); DocumentBuilder builder = new DocumentBuilder(doc); // Pass the first boolean parameter to get the DocumentBuilder to move to the FieldStart of the field. // We could also get FieldStarts of a field using GetChildNode method as in the other examples. builder.MoveToMergeField("Fullname", false, false); // The builder cursor should be positioned at the start of the field. FieldStart startField = (FieldStart)builder.CurrentNode; Paragraph endPara = (Paragraph)doc.FirstSection.GetChild(NodeType.Paragraph, 5, true); // Extract the content between these nodes in the document. Don't include these markers in the extraction. List <Node> extractedNodes = ExtractContentHelper.ExtractContent(startField, endPara, false); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodes); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentUsingField.docx"); //ExEnd:ExtractContentUsingField }
public void ExtractContentBetweenCommentRange() { //ExStart:ExtractContentBetweenCommentRange Document doc = new Document(MyDir + "Extract content.docx"); // This is a quick way of getting both comment nodes. // Your code should have a proper method of retrieving each corresponding start and end node. CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); // Firstly, extract the content between these nodes including the comment as well. List <Node> extractedNodesInclusive = ExtractContentHelper.ExtractContent(commentStart, commentEnd, true); Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesInclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenCommentRange.IncludingComment.docx"); // Secondly, extract the content between these nodes without the comment. List <Node> extractedNodesExclusive = ExtractContentHelper.ExtractContent(commentStart, commentEnd, false); dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesExclusive); dstDoc.Save(ArtifactsDir + "ExtractContent.ExtractContentBetweenCommentRange.WithoutComment.docx"); //ExEnd:ExtractContentBetweenCommentRange }