/// <summary> /// The main entry point for the application. /// </summary> public static void Main() { string dataDir = Path.GetFullPath("../../../Data/"); // Load the source document. Document srcDoc = new Document(dataDir + "Template.doc"); // This is the bookmark whose content we want to copy. Bookmark srcBookmark = srcDoc.Range.Bookmarks["ntf010145060"]; // We will be adding to this document. Document dstDoc = new Document(); // Let's say we will be appending to the end of the body of the last section. CompositeNode dstNode = dstDoc.LastSection.Body; // It is a good idea to use this import context object because multiple nodes are being imported. // If you import multiple times without a single context, it will result in many styles created. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); // Do it once. AppendBookmarkedText(importer, srcBookmark, dstNode); // Do it one more time for fun. AppendBookmarkedText(importer, srcBookmark, dstNode); // Save the finished document. dstDoc.Save(dataDir + "Template Out.doc"); }
public void KeepSourceNumbering() { //ExStart:KeepSourceNumbering Document srcDoc = new Document(MyDir + "Document source.docx"); Document dstDoc = new Document(MyDir + "Northwind traders.docx"); // Keep source list formatting when importing numbered paragraphs. ImportFormatOptions importFormatOptions = new ImportFormatOptions { KeepSourceNumbering = true }; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, false); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(ArtifactsDir + "JoinAndAppendDocuments.KeepSourceNumbering.docx"); //ExEnd:KeepSourceNumbering }
public void KeepSourceNumbering() { //ExStart //ExFor:ImportFormatOptions.KeepSourceNumbering //ExFor:NodeImporter.#ctor(DocumentBase, DocumentBase, ImportFormatMode, ImportFormatOptions) //ExSummary:Shows how the numbering will be imported when it clashes in source and destination documents. // Open a document with a custom list numbering scheme and clone it // Since both have the same numbering format, the formats will clash if we import one document into the other Document srcDoc = new Document(MyDir + "Custom list numbering.docx"); Document dstDoc = srcDoc.Clone(); // Both documents have the same numbering in their lists, but if we set this flag to false and then import one document into the other // the numbering of the imported source document will continue from where it ends in the destination document ImportFormatOptions importFormatOptions = new ImportFormatOptions(); importFormatOptions.KeepSourceNumbering = false; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepDifferentStyles, importFormatOptions); foreach (Paragraph paragraph in srcDoc.FirstSection.Body.Paragraphs) { Node importedNode = importer.ImportNode(paragraph, true); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.UpdateListLabels(); dstDoc.Save(ArtifactsDir + "NodeImporter.KeepSourceNumbering.docx"); //ExEnd }
public void IgnoreTextBoxes() { //ExStart:IgnoreTextBoxes Document srcDoc = new Document(MyDir + "Document source.docx"); Document dstDoc = new Document(MyDir + "Northwind traders.docx"); // Keep the source text boxes formatting when importing. ImportFormatOptions importFormatOptions = new ImportFormatOptions { IgnoreTextBoxes = false }; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, true); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(ArtifactsDir + "JoinAndAppendDocuments.IgnoreTextBoxes.docx"); //ExEnd:IgnoreTextBoxes }
// Token: 0x0600000C RID: 12 RVA: 0x0000235C File Offset: 0x0000055C public static void InsertDocument(Node insertAfterNode, Document mainDoc, Document srcDoc) { if (insertAfterNode.NodeType != NodeType.Paragraph & insertAfterNode.NodeType != NodeType.Table) { throw new Exception("The destination node should be either a paragraph or table."); } CompositeNode parentNode = insertAfterNode.ParentNode; while (srcDoc.LastSection.Body.LastParagraph != null && !srcDoc.LastSection.Body.LastParagraph.HasChildNodes) { srcDoc.LastSection.Body.LastParagraph.Remove(); } NodeImporter nodeImporter = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting); int count = srcDoc.Sections.Count; for (int i = 0; i < count; i++) { Section section = srcDoc.Sections[i]; int count2 = section.Body.ChildNodes.Count; for (int j = 0; j < count2; j++) { Node srcNode = section.Body.ChildNodes[j]; Node node = nodeImporter.ImportNode(srcNode, true); parentNode.InsertAfter(node, insertAfterNode); insertAfterNode = node; } } }
/// <summary> /// Copies content of the bookmark and adds it to the end of the specified node. /// The destination node can be in a different document. /// </summary> /// <param name="importer">Maintains the import context </param> /// <param name="srcBookmark">The input bookmark</param> /// <param name="dstNode">Must be a node that can contain paragraphs (such as a Story).</param> private static void AppendBookmarkedText(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode) { // This is the paragraph that contains the beginning of the bookmark. Paragraph startPara = (Paragraph)srcBookmark.BookmarkStart.ParentNode; // This is the paragraph that contains the end of the bookmark. Paragraph endPara = (Paragraph)srcBookmark.BookmarkEnd.ParentNode; if ((startPara == null) || (endPara == null)) throw new InvalidOperationException("Parent of the bookmark start or end is not a paragraph, cannot handle this scenario yet."); // Limit ourselves to a reasonably simple scenario. if (startPara.ParentNode != endPara.ParentNode) throw new InvalidOperationException("Start and end paragraphs have different parents, cannot handle this scenario yet."); // We want to copy all paragraphs from the start paragraph up to (and including) the end paragraph, // therefore the node at which we stop is one after the end paragraph. Node endNode = endPara.NextSibling; // This is the loop to go through all paragraph-level nodes in the bookmark. for (Node curNode = startPara; curNode != endNode; curNode = curNode.NextSibling) { // This creates a copy of the current node and imports it (makes it valid) in the context // of the destination document. Importing means adjusting styles and list identifiers correctly. Node newNode = importer.ImportNode(curNode, true); // Now we simply append the new node to the destination. dstNode.AppendChild(newNode); } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); string fileName = "Template.doc"; // Load the source document. Document srcDoc = new Document(dataDir + fileName); // This is the bookmark whose content we want to copy. Bookmark srcBookmark = srcDoc.Range.Bookmarks["ntf010145060"]; // We will be adding to this document. Document dstDoc = new Document(); // Let's say we will be appending to the end of the body of the last section. CompositeNode dstNode = dstDoc.LastSection.Body; // It is a good idea to use this import context object because multiple nodes are being imported. // If you import multiple times without a single context, it will result in many styles created. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); // Do it once. AppendBookmarkedText(importer, srcBookmark, dstNode); // Do it one more time for fun. AppendBookmarkedText(importer, srcBookmark, dstNode); dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); // Save the finished document. dstDoc.Save(dataDir); Console.WriteLine("\nBookmark copied successfully.\nFile saved at " + dataDir); }
/// <summary> /// Inserts all the nodes of another document after a paragraph or table. /// </summary> private static void InsertDocument(Node insertionDestination, Document docToInsert) { if (insertionDestination.NodeType == NodeType.Paragraph || insertionDestination.NodeType == NodeType.Table) { CompositeNode dstStory = insertionDestination.ParentNode; NodeImporter importer = new NodeImporter(docToInsert, insertionDestination.Document, ImportFormatMode.KeepSourceFormatting); foreach (Section srcSection in docToInsert.Sections.OfType <Section>()) { foreach (Node srcNode in srcSection.Body) { // Skip the node if it is the last empty paragraph in a section. if (srcNode.NodeType == NodeType.Paragraph) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) { continue; } } Node newNode = importer.ImportNode(srcNode, true); dstStory.InsertAfter(newNode, insertionDestination); insertionDestination = newNode; } } } else { throw new ArgumentException("The destination node must be either a paragraph or table."); } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); // Load the source document. Document srcDoc = new Document(dataDir + "Template.doc"); // This is the bookmark whose content we want to copy. Bookmark srcBookmark = srcDoc.Range.Bookmarks["ntf010145060"]; // We will be adding to this document. Document dstDoc = new Document(); // Let's say we will be appending to the end of the body of the last section. CompositeNode dstNode = dstDoc.LastSection.Body; // It is a good idea to use this import context object because multiple nodes are being imported. // If you import multiple times without a single context, it will result in many styles created. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); // Do it once. AppendBookmarkedText(importer, srcBookmark, dstNode); // Do it one more time for fun. AppendBookmarkedText(importer, srcBookmark, dstNode); // Save the finished document. dstDoc.Save(dataDir + "Template Out.doc"); Console.WriteLine("\nBookmark copied successfully.\nFile saved at " + dataDir + "Template Out.doc"); }
// Token: 0x0600000D RID: 13 RVA: 0x00002480 File Offset: 0x00000680 private static void InsertDocument(Node insertAfterNode, Document srcDoc) { if (!insertAfterNode.NodeType.Equals(NodeType.Paragraph) & !insertAfterNode.NodeType.Equals(NodeType.Table)) { throw new ArgumentException("The destination node should be either a paragraph or table."); } CompositeNode parentNode = insertAfterNode.ParentNode; NodeImporter nodeImporter = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); foreach (object obj in srcDoc.Sections) { Section section = (Section)obj; foreach (object obj2 in section.Body) { Node node = (Node)obj2; if (node.NodeType.Equals(NodeType.Paragraph)) { Paragraph paragraph = (Paragraph)node; if (paragraph.IsEndOfSection && !paragraph.HasChildNodes) { continue; } } Node node2 = nodeImporter.ImportNode(node, true); parentNode.InsertAfter(node2, insertAfterNode); insertAfterNode = node2; } } }
/// <summary> /// Copies content of the bookmark and adds it to the end of the specified node. /// The destination node can be in a different document. /// </summary> /// <param name="importer">Maintains the import context </param> /// <param name="srcBookmark">The input bookmark</param> /// <param name="dstNode">Must be a node that can contain paragraphs (such as a Story).</param> private static void AppendBookmarkedText(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode) { // This is the paragraph that contains the beginning of the bookmark. Paragraph startPara = (Paragraph)srcBookmark.BookmarkStart.ParentNode; // This is the paragraph that contains the end of the bookmark. Paragraph endPara = (Paragraph)srcBookmark.BookmarkEnd.ParentNode; if ((startPara == null) || (endPara == null)) { throw new InvalidOperationException("Parent of the bookmark start or end is not a paragraph, cannot handle this scenario yet."); } // Limit ourselves to a reasonably simple scenario. if (startPara.ParentNode != endPara.ParentNode) { throw new InvalidOperationException("Start and end paragraphs have different parents, cannot handle this scenario yet."); } // We want to copy all paragraphs from the start paragraph up to (and including) the end paragraph, // Therefore the node at which we stop is one after the end paragraph. Node endNode = endPara.NextSibling; // This is the loop to go through all paragraph-level nodes in the bookmark. for (Node curNode = startPara; curNode != endNode; curNode = curNode.NextSibling) { // This creates a copy of the current node and imports it (makes it valid) in the context // Of the destination document. Importing means adjusting styles and list identifiers correctly. Node newNode = importer.ImportNode(curNode, true); // Now we simply append the new node to the destination. dstNode.AppendChild(newNode); } }
/// <summary> /// 复制页眉页脚 /// </summary> /// <param name="importer">导入节点</param> /// <param name="srcSection">源分节</param> /// <param name="desSection">目标分节</param> private void CopyHeadersFooters(NodeImporter importer, Section srcSection, Section desSection) { desSection.HeadersFooters.Clear(); foreach (HeaderFooter headerFooter in srcSection.HeadersFooters) { desSection.HeadersFooters.Add(importer.ImportNode(headerFooter, true)); } }
public void CopyNodeToOtherDoc(Node node, Document docNew) { docNew.FirstSection.Body.RemoveAllChildren(); NodeImporter importer = new NodeImporter(mdoc, docNew, ImportFormatMode.KeepSourceFormatting); Node importNode = importer.ImportNode(node, true); docNew.FirstSection.Body.AppendChild(importNode); }
public void KeepSourceNumbering(bool keepSourceNumbering) { //ExStart //ExFor:ImportFormatOptions.KeepSourceNumbering //ExFor:NodeImporter.#ctor(DocumentBase, DocumentBase, ImportFormatMode, ImportFormatOptions) //ExSummary:Shows how to resolve list numbering clashes in source and destination documents. // Open a document with a custom list numbering scheme, and then clone it. // Since both have the same numbering format, the formats will clash if we import one document into the other. Document srcDoc = new Document(MyDir + "Custom list numbering.docx"); Document dstDoc = srcDoc.Clone(); // When we import the document's clone into the original and then append it, // then the two lists with the same list format will join. // If we set the "KeepSourceNumbering" flag to "false", then the list from the document clone // that we append to the original will carry on the numbering of the list we append it to. // This will effectively merge the two lists into one. // If we set the "KeepSourceNumbering" flag to "true", then the document clone // list will preserve its original numbering, making the two lists appear as separate lists. ImportFormatOptions importFormatOptions = new ImportFormatOptions(); importFormatOptions.KeepSourceNumbering = keepSourceNumbering; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepDifferentStyles, importFormatOptions); foreach (Paragraph paragraph in srcDoc.FirstSection.Body.Paragraphs) { Node importedNode = importer.ImportNode(paragraph, true); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.UpdateListLabels(); if (keepSourceNumbering) { Assert.AreEqual( "6. Item 1\r\n" + "7. Item 2 \r\n" + "8. Item 3\r\n" + "9. Item 4\r\n" + "6. Item 1\r\n" + "7. Item 2 \r\n" + "8. Item 3\r\n" + "9. Item 4", dstDoc.FirstSection.Body.ToString(SaveFormat.Text).Trim()); } else { Assert.AreEqual( "6. Item 1\r\n" + "7. Item 2 \r\n" + "8. Item 3\r\n" + "9. Item 4\r\n" + "10. Item 1\r\n" + "11. Item 2 \r\n" + "12. Item 3\r\n" + "13. Item 4", dstDoc.FirstSection.Body.ToString(SaveFormat.Text).Trim()); } //ExEnd }
public Document CopyNodeToNewDoc(Node node) { Document docNew = new Document(); docNew.FirstSection.Body.RemoveAllChildren(); NodeImporter importer = new NodeImporter(mdoc, docNew, ImportFormatMode.KeepSourceFormatting); Node importNode = importer.ImportNode(node, true); docNew.FirstSection.Body.AppendChild(importNode); return(docNew); }
//ExEnd:InsertDocument //ExStart:InsertDocumentWithSectionFormatting /// <summary> /// Inserts content of the external document after the specified node. /// </summary> /// <param name="insertAfterNode">Node in the destination document after which the content /// should be inserted. This node should be a block level node (paragraph or table).</param> /// <param name="srcDoc">The document to insert.</param> static void InsertDocumentWithSectionFormatting(Node insertAfterNode, Document srcDoc) { // Make sure that the node is either a pargraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) { throw new ArgumentException("The destination node should be either a paragraph or table."); } // Document to insert srcDoc into. Document dstDoc = (Document)insertAfterNode.Document; // To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections. // The section of the node which the insert marker node belongs to Section currentSection = (Section)insertAfterNode.GetAncestor(NodeType.Section); // Don't clone the content inside the section, we just want the properties of the section retained. Section cloneSection = (Section)currentSection.Clone(false); // However make sure the clone section has a body, but no empty first paragraph. cloneSection.EnsureMinimum(); cloneSection.Body.FirstParagraph.Remove(); // Insert the cloned section into the document after the original section. insertAfterNode.Document.InsertAfter(cloneSection, currentSection); // Append all nodes after the marker node to the new section. This will split the content at the section level at // the marker so the sections from the other document can be inserted directly. Node currentNode = insertAfterNode.NextSibling; while (currentNode != null) { Node nextNode = currentNode.NextSibling; cloneSection.Body.AppendChild(currentNode); currentNode = nextNode; } // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) { Node newNode = importer.ImportNode(srcSection, true); // Append each section to the destination document. Start by inserting it after the split section. dstDoc.InsertAfter(newNode, currentSection); currentSection = (Section)newNode; } }
//ExStart:CommonGenerateDocument public static Document GenerateDocument(Document srcDoc, ArrayList nodes) { Document dstDoc = new Document(); // Remove the first paragraph from the empty document. dstDoc.FirstSection.Body.RemoveAllChildren(); // Import each node from the list into the new document. Keep the original formatting of the node. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); foreach (Node node in nodes) { Node importNode = importer.ImportNode(node, true); dstDoc.FirstSection.Body.AppendChild(importNode); } return(dstDoc); }
public void CopyBookmarkedText() { Document srcDoc = new Document(MyDir + "Bookmarks.docx"); // This is the bookmark whose content we want to copy. Bookmark srcBookmark = srcDoc.Range.Bookmarks["MyBookmark1"]; // We will be adding to this document. Document dstDoc = new Document(); // Let's say we will be appended to the end of the body of the last section. CompositeNode dstNode = dstDoc.LastSection.Body; // If you import multiple times without a single context, it will result in many styles created. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); AppendBookmarkedText(importer, srcBookmark, dstNode); dstDoc.Save(ArtifactsDir + "WorkingWithBookmarks.CopyBookmarkedText.docx"); }
//ExStart //ExFor:Paragraph.IsEndOfSection //ExFor:NodeImporter //ExFor:NodeImporter.#ctor(DocumentBase, DocumentBase, ImportFormatMode) //ExFor:NodeImporter.ImportNode(Node, Boolean) //ExId:InsertDocumentMain //ExSummary:This is a method that inserts contents of one document at a specified location in another document. /// <summary> /// Inserts content of the external document after the specified node. /// Section breaks and section formatting of the inserted document are ignored. /// </summary> /// <param name="insertAfterNode">Node in the destination document after which the content /// should be inserted. This node should be a block level node (paragraph or table).</param> /// <param name="srcDoc">The document to insert.</param> static void InsertDocument(Node insertAfterNode, Document srcDoc) { // Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) { throw new ArgumentException("The destination node should be either a paragraph or table."); } // We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections.OfType <Section>()) { // Loop through all block level nodes (paragraphs and tables) in the body of the section. foreach (Node srcNode in srcSection.Body) { // Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) { continue; } } // This creates a clone of the node, suitable for insertion into the destination document. Node newNode = importer.ImportNode(srcNode, true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } }
static void InsertDocument(Node insertAfterNode, Document sourceDoc) { //Check if the node is a paragraph or a table if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) { throw new ArgumentException("The destination node should either be a paragraph or a table"); } //Insert into the parent of the destination paragraph CompositeNode destinationStory = insertAfterNode.ParentNode; //Translating styles and lists during the import NodeImporter importer = new NodeImporter(sourceDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); //Loop through all sections of the source document foreach (Section sourceSection in sourceDoc.Sections) { //Loop through all block level nodes such as paragraphs and tables in the body section foreach (Node sourceNode in sourceSection.Body) { //skip the node if its the last empty pargraph of the section if (sourceNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)sourceNode; if (para.IsEndOfSection && !para.HasChildNodes) { continue; } } //creating a clone of the node to be inserted in the destination document Node newNode = importer.ImportNode(sourceNode, true); //Insert the clone(new node) after the reference node destinationStory.InsertAfter(newNode, insertAfterNode); //move on to the next node by changing reference node insertAfterNode = newNode; } } }
/// <summary> /// 插入word内容 /// </summary> /// <param name="insertAfterNode"></param> /// <param name="mainDoc"></param> /// <param name="srcDoc"></param> public static void InsertDocument(Node insertAfterNode, Aspose.Words.Document mainDoc, Aspose.Words.Document srcDoc) { // Make sure that the node is either a pargraph or table. if ((insertAfterNode.NodeType != NodeType.Paragraph) & (insertAfterNode.NodeType != NodeType.Table)) { throw new Exception("The destination node should be either a paragraph or table."); } //We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; //Remove empty paragraphs from the end of document while (null != srcDoc.LastSection.Body.LastParagraph && !srcDoc.LastSection.Body.LastParagraph.HasChildNodes) { srcDoc.LastSection.Body.LastParagraph.Remove(); } NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting); //Loop through all sections in the source document. int sectCount = srcDoc.Sections.Count; for (int sectIndex = 0; sectIndex < sectCount; sectIndex++) { Aspose.Words.Section srcSection = srcDoc.Sections[sectIndex]; //Loop through all block level nodes (paragraphs and tables) in the body of the section. int nodeCount = srcSection.Body.ChildNodes.Count; for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++) { Node srcNode = srcSection.Body.ChildNodes[nodeIndex]; Node newNode = importer.ImportNode(srcNode, true); dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } }
public static void IgnoreTextBoxes(string dataDir) { // ExStart:IgnoreTextBoxes Document srcDoc = new Document(dataDir + "source.docx"); Document dstDoc = new Document(dataDir + "destination.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); // Keep the source text boxes formatting when importing. importFormatOptions.IgnoreTextBoxes = false; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, true); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(dataDir + "output.docx"); // ExEnd:IgnoreTextBoxes }
static void KeepSourceNumbering(string dataDir) { // ExStart:KeepSourceNumbering Document srcDoc = new Document(dataDir + "source.docx"); Document dstDoc = new Document(dataDir + "destination.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); // Keep source list formatting when importing numbered paragraphs. importFormatOptions.KeepSourceNumbering = true; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, false); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(dataDir + "output.docx"); // ExEnd:KeepSourceNumbering }
//ExStart //ExFor:Paragraph.IsEndOfSection //ExId:InsertDocumentMain //ExSummary:This is a method that inserts contents of one document at a specified location in another document. /// <summary> /// Inserts content of the external document after the specified node. /// Section breaks and section formatting of the inserted document are ignored. /// </summary> /// <param name="insertAfterNode">Node in the destination document after which the content /// should be inserted. This node should be a block level node (paragraph or table).</param> /// <param name="srcDoc">The document to insert.</param> static void InsertDocument(Node insertAfterNode, Document srcDoc) { // Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) throw new ArgumentException("The destination node should be either a paragraph or table."); // We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) { // Loop through all block level nodes (paragraphs and tables) in the body of the section. foreach (Node srcNode in srcSection.Body) { // Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) continue; } // This creates a clone of the node, suitable for insertion into the destination document. Node newNode = importer.ImportNode(srcNode, true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } }
/// <summary> /// Inserts the contents of a document after the specified node. /// </summary> static void InsertDocument(Node insertionDestination, Document docToInsert) { if (insertionDestination.NodeType.Equals(NodeType.Paragraph) || insertionDestination.NodeType.Equals(NodeType.Table)) { CompositeNode destinationParent = insertionDestination.ParentNode; NodeImporter importer = new NodeImporter(docToInsert, insertionDestination.Document, ImportFormatMode.KeepSourceFormatting); // Loop through all block-level nodes in the section's body, // then clone and insert every node that is not the last empty paragraph of a section. foreach (Section srcSection in docToInsert.Sections.OfType <Section>()) { foreach (Node srcNode in srcSection.Body) { if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) { continue; } } Node newNode = importer.ImportNode(srcNode, true); destinationParent.InsertAfter(newNode, insertionDestination); insertionDestination = newNode; } } } else { throw new ArgumentException("The destination node should be either a paragraph or table."); } }
public static Document GenerateDocument(Document srcDoc, ArrayList nodes) { // Create a blank document. Document dstDoc = new Document(); // Remove the first paragraph from the empty document. dstDoc.FirstSection.Body.RemoveAllChildren(); // Import each node from the list into the new document. Keep the original formatting of the node. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); foreach (Node node in nodes) { Node importNode = importer.ImportNode(node, true); dstDoc.FirstSection.Body.AppendChild(importNode); } // Return the generated document. return dstDoc; }
public override void PostHook(NodeImporter impoter, Transform trans, VGltf.Types.Node gltfNode) { if (!GE.ContainsExtensionUsed(impoter.Container.Gltf, AvatarType.ExtensionName)) { return; } AvatarType extAvatar; if (!gltfNode.GetExtension(AvatarType.ExtensionName, out extAvatar)) { return; } var extHD = extAvatar.HumanDescription; var hd = new HumanDescription(); hd.upperArmTwist = extHD.UpperArmTwist; hd.lowerArmTwist = extHD.LowerArmTwist; hd.upperLegTwist = extHD.UpperLegTwist; hd.lowerLegTwist = extHD.LowerLegTwist; hd.armStretch = extHD.ArmStretch; hd.legStretch = extHD.LegStretch; hd.feetSpacing = extHD.FeetSpacing; hd.skeleton = extHD.Skeleton.Select(s => { // TODO: Coord return(new SkeletonBone { name = s.Name, position = PrimitiveImporter.AsVector3(s.Position), rotation = PrimitiveImporter.AsQuaternion(s.Rotation), scale = PrimitiveImporter.AsVector3(s.Scale), }); }).ToArray(); hd.human = extHD.Human.Select(h => { var extLimit = h.Limit; var limit = new HumanLimit { useDefaultValues = extLimit.UseDefaultValues, min = PrimitiveImporter.AsVector3(extLimit.Min), max = PrimitiveImporter.AsVector3(extLimit.Max), center = PrimitiveImporter.AsVector3(extLimit.Center), axisLength = extLimit.AxisLength, }; return(new HumanBone { boneName = h.BoneName, humanName = h.HumanName, limit = limit, }); }).ToArray(); var go = trans.gameObject; var anim = go.AddComponent <Animator>(); anim.avatar = AvatarBuilder.BuildHumanAvatar(go, hd); }
// ExEnd:InsertDocument // ExStart:InsertDocumentWithSectionFormatting /// <summary> /// Inserts content of the external document after the specified node. /// </summary> /// <param name="insertAfterNode">Node in the destination document after which the content /// Should be inserted. This node should be a block level node (paragraph or table).</param> /// <param name="srcDoc">The document to insert.</param> static void InsertDocumentWithSectionFormatting(Node insertAfterNode, Document srcDoc) { // Make sure that the node is either a pargraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) throw new ArgumentException("The destination node should be either a paragraph or table."); // Document to insert srcDoc into. Document dstDoc = (Document)insertAfterNode.Document; // To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections. // The section of the node which the insert marker node belongs to Section currentSection = (Section)insertAfterNode.GetAncestor(NodeType.Section); // Don't clone the content inside the section, we just want the properties of the section retained. Section cloneSection = (Section)currentSection.Clone(false); // However make sure the clone section has a body, but no empty first paragraph. cloneSection.EnsureMinimum(); cloneSection.Body.FirstParagraph.Remove(); // Insert the cloned section into the document after the original section. insertAfterNode.Document.InsertAfter(cloneSection, currentSection); // Append all nodes after the marker node to the new section. This will split the content at the section level at // The marker so the sections from the other document can be inserted directly. Node currentNode = insertAfterNode.NextSibling; while (currentNode != null) { Node nextNode = currentNode.NextSibling; cloneSection.Body.AppendChild(currentNode); currentNode = nextNode; } // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) { Node newNode = importer.ImportNode(srcSection, true); // Append each section to the destination document. Start by inserting it after the split section. dstDoc.InsertAfter(newNode, currentSection); currentSection = (Section)newNode; } }
/// <summary> /// 插入文档 /// </summary> /// <param name="insertAfterNode">插入节点</param> /// <param name="srcDoc">目标文档</param> /// <param name="newSection">是否新节</param> private void InsertDocument(Node insertAfterNode, Document srcDoc, bool newSection) { // Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table))) { throw new ArgumentException("目标节点应该是一个段落或表"); // The destination node should be either a paragraph or table. } // We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode; // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.UseDestinationStyles); DocumentBuilder builder = new DocumentBuilder(this.doc); List <TopicStartPara> topicStartParas = new List <TopicStartPara>(); // Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) { // Loop through all block level nodes (paragraphs and tables) in the body of the section. foreach (Node srcNode in srcSection.Body) { // Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) { Paragraph para = (Paragraph)srcNode; if (para.IsEndOfSection && !para.HasChildNodes) { continue; } } // This creates a clone of the node, suitable for insertion into the destination document. Node newNode = importer.ImportNode(srcNode, true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; if ((newSection || !srcSection.Equals(srcDoc.FirstSection)) && srcNode.Equals(srcSection.Body.FirstChild) && insertAfterNode.PreviousSibling != null) { bool isRemove = false; Node firstChildWithSection = insertAfterNode.PreviousSibling; if (firstChildWithSection.NodeType != NodeType.Paragraph) { firstChildWithSection = new Paragraph(insertAfterNode.Document); dstStory.InsertBefore(firstChildWithSection, insertAfterNode); isRemove = true; } BreakType bt = BreakType.SectionBreakNewPage; if (!srcSection.Equals(srcDoc.FirstSection)) { switch (srcSection.PageSetup.SectionStart) { case SectionStart.Continuous: { bt = BreakType.SectionBreakContinuous; break; } } } topicStartParas.Add(new TopicStartPara() { TopicStartNode = firstChildWithSection, SourceSection = srcSection, TargerBreakType = bt, IsRemove = isRemove }); } } } // 插入分节符 foreach (var item in topicStartParas) { if (item.TopicStartNode != null) { var moveNode = item.TopicStartNode; if (!moveNode.NodeType.Equals(NodeType.Paragraph)) { throw new ArgumentException("目标节点应该是一个段落"); } var nextNode = moveNode.NextSibling; builder.MoveTo(moveNode); builder.InsertBreak(item.TargerBreakType); builder.CurrentSection.HeadersFooters.LinkToPrevious(false); PageSetup ps = builder.PageSetup; ps.DifferentFirstPageHeaderFooter = false; ps.OddAndEvenPagesHeaderFooter = false; this.CopyHeadersFooters(importer, item.SourceSection, builder.CurrentSection); // 删除添加分隔符后多加的空行 if (nextNode != null && nextNode.PreviousSibling != null && nextNode.PreviousSibling.NodeType == NodeType.Paragraph && ((Paragraph)nextNode.PreviousSibling).Runs.Count == 0) { nextNode.PreviousSibling.Remove(); } if (item.IsRemove) { moveNode.Remove(); } } } }