public void ImportNode()
        {
            //ExStart
            //ExFor:DocumentBase.ImportNode(Node, Boolean)
            //ExSummary:Shows how to import a node from one document to another.
            Document srcDoc = new Document();
            Document dstDoc = new Document();

            srcDoc.FirstSection.Body.FirstParagraph.AppendChild(
                new Run(srcDoc, "Source document first paragraph text."));
            dstDoc.FirstSection.Body.FirstParagraph.AppendChild(
                new Run(dstDoc, "Destination document first paragraph text."));

            // Every node has a parent document, which is the document that contains the node.
            // Inserting a node into a document that the node does not belong to will throw an exception.
            Assert.AreNotEqual(dstDoc, srcDoc.FirstSection.Document);
            Assert.Throws <ArgumentException>(() => { dstDoc.AppendChild(srcDoc.FirstSection); });

            // Use the ImportNode method to create a copy of a node, which will have the document
            // that called the ImportNode method set as its new owner document.
            Section importedSection = (Section)dstDoc.ImportNode(srcDoc.FirstSection, true);

            Assert.AreEqual(dstDoc, importedSection.Document);

            // We can now insert the node into the document.
            dstDoc.AppendChild(importedSection);

            Assert.AreEqual("Destination document first paragraph text.\r\nSource document first paragraph text.\r\n",
                            dstDoc.ToString(SaveFormat.Text));
            //ExEnd

            Assert.AreNotEqual(importedSection, srcDoc.FirstSection);
            Assert.AreNotEqual(importedSection.Document, srcDoc.FirstSection.Document);
            Assert.AreEqual(importedSection.Body.FirstParagraph.GetText(),
                            srcDoc.FirstSection.Body.FirstParagraph.GetText());
        }
Esempio n. 2
0
        public void DocumentGetText_ToString()
        {
            //ExStart
            //ExFor:CompositeNode.GetText
            //ExFor:Node.ToString(SaveFormat)
            //ExId:NodeTxtExportDifferences
            //ExSummary:Shows the difference between calling the GetText and ToString methods on a node.
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // Enter a dummy field into the document.
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.InsertField("MERGEFIELD Field");

            // GetText will retrieve all field codes and special characters
            Console.WriteLine("GetText() Result: " + doc.GetText());

            // ToString will export the node to the specified format. When converted to text it will not retrieve fields code
            // or special characters, but will still contain some natural formatting characters such as paragraph markers etc.
            // This is the same as "viewing" the document as if it was opened in a text editor.
            Console.WriteLine("ToString() Result: " + doc.ToString(SaveFormat.Text));
            //ExEnd
        }