Aspose.Words.Examples.CSharp.Programming_Documents.Working_With_Document.Common.GenerateDocument C# (CSharp) Method

GenerateDocument() public static method

public static GenerateDocument ( Document srcDoc, ArrayList nodes ) : Document
srcDoc Document
nodes System.Collections.ArrayList
return Document
        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;
        }
        // ExEnd:CommonGenerateDocument  

Usage Example

        public static void Run()
        {
            // ExStart:ExtractContentBetweenParagraphStyles
            // The path to the documents directory.
            string   dataDir  = RunExamples.GetDataDir_WorkingWithDocument();
            string   fileName = "TestFile.doc";
            Document doc      = new Document(dataDir + fileName);

            // Gather a list of the paragraphs using the respective heading styles.
            ArrayList parasStyleHeading1 = Common.ParagraphsByStyleName(doc, "Heading 1");
            ArrayList parasStyleHeading3 = Common.ParagraphsByStyleName(doc, "Heading 3");

            // Use the first instance of the paragraphs with those styles.
            Node startPara1 = (Node)parasStyleHeading1[0];
            Node endPara1   = (Node)parasStyleHeading3[0];

            // Extract the content between these nodes in the document. Don't include these markers in the extraction.
            ArrayList extractedNodes = Common.ExtractContent(startPara1, endPara1, false);

            // Insert the content into a new separate document and save it to disk.
            Document dstDoc = Common.GenerateDocument(doc, extractedNodes);

            dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
            dstDoc.Save(dataDir);
            // ExEnd:ExtractContentBetweenParagraphStyles
            Console.WriteLine("\nExtracted content betweenn the paragraph styles successfully.\nFile saved at " + dataDir);
        }
All Usage Examples Of Aspose.Words.Examples.CSharp.Programming_Documents.Working_With_Document.Common::GenerateDocument