Exemple #1
0
        public void RemoveNodes()
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            //ExStart
            //ExFor:Node
            //ExFor:Node.NodeType
            //ExFor:Node.Remove
            //ExSummary:Shows how to remove all nodes of a specific type from a composite node. In this example we remove tables from a section body.
            // Get the section that we want to work on.
            Aspose.Words.Section section = doc.Sections[0];
            Body body = section.Body;

            // Select the first child node in the body.
            Aspose.Words.Node curNode = body.FirstChild;

            while (curNode != null)
            {
                // Save the pointer to the next sibling node because if the current
                // node is removed from the parent in the next step, we will have
                // no way of finding the next node to continue the loop.
                Aspose.Words.Node nextNode = curNode.NextSibling;

                // A section body can contain Paragraph and Table nodes.
                // If the node is a Table, remove it from the parent.
                if (curNode.NodeType.Equals(NodeType.Table))
                {
                    curNode.Remove();
                }

                // Continue going through child nodes until null (no more siblings) is reached.
                curNode = nextNode;
            }
            //ExEnd
        }
        public void AddRemove()
        {
            //ExStart
            //ExFor:Document.Sections
            //ExFor:Section.Clone
            //ExFor:SectionCollection
            //ExFor:NodeCollection.RemoveAt(Int32)
            //ExSummary:Shows how to add/remove sections in a document.
            // Open the document.
            Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Section.AddRemove.doc");

            // This shows what is in the document originally. The document has two sections.
            Console.WriteLine(doc.GetText());

            // Delete the first section from the document
            doc.Sections.RemoveAt(0);

            // Duplicate the last section and append the copy to the end of the document.
            int lastSectionIdx = doc.Sections.Count - 1;

            Aspose.Words.Section newSection = doc.Sections[lastSectionIdx].Clone();
            doc.Sections.Add(newSection);

            // Check what the document contains after we changed it.
            Console.WriteLine(doc.GetText());
            //ExEnd

            Assert.AreEqual("Hello2\x000cHello2\x000c", doc.GetText());
        }
 public void SectionsCloneSection()
 {
     //ExStart
     //ExId:SectionsCloneSection
     //ExSummary:Shows how to create a duplicate of a particular section.
     Aspose.Words.Document doc          = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  cloneSection = doc.Sections[0].Clone();
     //ExEnd
 }
 public void SectionDeleteHeaderFooterShapes()
 {
     //ExStart
     //ExFor:Section.DeleteHeaderFooterShapes
     //ExSummary:Removes all images and shapes from all headers footers in a section.
     Aspose.Words.Document doc     = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  section = doc.Sections[0];
     section.DeleteHeaderFooterShapes();
     //ExEnd
 }
 public void SectionsAccessByIndex()
 {
     //ExStart
     //ExFor:SectionCollection.Item(Int32)
     //ExId:SectionsAccessByIndex
     //ExSummary:Shows how to access a section at the specified index.
     Aspose.Words.Document doc     = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  section = doc.Sections[0];
     //ExEnd
 }
Exemple #6
0
        public static void CreateAndAddParagraphNode()
        {
            //ExStart:CreateAndAddParagraphNode
            Document  doc  = new Document();
            Paragraph para = new Paragraph(doc);

            Aspose.Words.Section section = doc.LastSection;
            section.Body.AppendChild(para);
            //ExEnd:CreateAndAddParagraphNode
        }
 public void SectionsDeleteHeaderFooter()
 {
     //ExStart
     //ExFor:Section.ClearHeadersFooters
     //ExId:SectionsDeleteHeaderFooter
     //ExSummary:Clears content of all headers and footers in a section.
     Aspose.Words.Document doc     = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  section = doc.Sections[0];
     section.ClearHeadersFooters();
     //ExEnd
 }
 public void SectionsDeleteSectionContent()
 {
     //ExStart
     //ExFor:Section.ClearContent
     //ExId:SectionsDeleteSectionContent
     //ExSummary:Shows how to delete main content of a section.
     Aspose.Words.Document doc     = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  section = doc.Sections[0];
     section.ClearContent();
     //ExEnd
 }
 public void SectionsAddSection()
 {
     //ExStart
     //ExFor:NodeCollection.Add
     //ExId:SectionsAddSection
     //ExSummary:Shows how to add a section to the end of the document.
     Aspose.Words.Document doc          = new Aspose.Words.Document(MyDir + "Document.doc");
     Aspose.Words.Section  sectionToAdd = new Aspose.Words.Section(doc);
     doc.Sections.Add(sectionToAdd);
     //ExEnd
 }
        public void BodyNodeType()
        {
            //ExStart
            //ExFor:Body.NodeType
            //ExFor:HeaderFooter.NodeType
            //ExFor:Document.FirstSection
            //ExSummary:Shows how you can enumerate through children of a composite node and detect types of the children nodes.

            // Open a document.
            Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Section.BodyNodeType.doc");

            // Get the first section in the document.
            Aspose.Words.Section section = doc.FirstSection;

            // A Section is a composite node and therefore can contain child nodes.
            // Section can contain only Body and HeaderFooter nodes.
            foreach (Aspose.Words.Node node in section)
            {
                // Every node has the NodeType property.
                switch (node.NodeType)
                {
                case NodeType.Body:
                {
                    // If the node type is Body, we can cast the node to the Body class.
                    Body body = (Body)node;

                    // Write the content of the main story of the section to the console.
                    Console.WriteLine("*** Body ***");
                    Console.WriteLine(body.GetText());
                    break;
                }

                case NodeType.HeaderFooter:
                {
                    // If the node type is HeaderFooter, we can cast the node to the HeaderFooter class.
                    Aspose.Words.HeaderFooter headerFooter = (Aspose.Words.HeaderFooter)node;

                    // Write the content of the header footer to the console.
                    Console.WriteLine("*** HeaderFooter ***");
                    Console.WriteLine(headerFooter.HeaderFooterType);
                    Console.WriteLine(headerFooter.GetText());
                    break;
                }

                default:
                {
                    // Other types of nodes never occur inside a Section node.
                    throw new Exception("Unexpected node type in a section.");
                }
                }
            }
            //ExEnd
        }
        public void SectionsImportSection()
        {
            //ExStart
            //ExId:SectionsImportSection
            //ExSummary:Shows how to copy sections between documents.
            Aspose.Words.Document srcDoc = new Aspose.Words.Document(MyDir + "Document.doc");
            Aspose.Words.Document dstDoc = new Aspose.Words.Document();

            Aspose.Words.Section sourceSection = srcDoc.Sections[0];
            Aspose.Words.Section newSection    = (Aspose.Words.Section)dstDoc.ImportNode(sourceSection, true);
            dstDoc.Sections.Add(newSection);
            //ExEnd
        }
        public void MigrateFrom2XImportSection()
        {
            Aspose.Words.Document srcDoc = new Aspose.Words.Document();
            Aspose.Words.Document dstDoc = new Aspose.Words.Document();

            //ExStart
            //ExId:MigrateFrom2XImportSection
            //ExSummary:This fragment shows how to insert a section from another document in Aspose.Words 3.0 or higher.
            Aspose.Words.Section sourceSection = srcDoc.Sections[0];
            Aspose.Words.Section newSection    = (Aspose.Words.Section)dstDoc.ImportNode(sourceSection, true);
            dstDoc.Sections.Add(newSection);
            //ExEnd
        }
Exemple #13
0
        public void CreateAndAddParagraphNode()
        {
            //ExStart
            //ExId:CreateAndAddParagraphNode
            //ExSummary:Creates and adds a paragraph node.
            Aspose.Words.Document doc = new Aspose.Words.Document();

            Paragraph para = new Paragraph(doc);

            Aspose.Words.Section section = doc.LastSection;
            section.Body.AppendChild(para);
            //ExEnd
        }
        public void EnsureSectionMinimum()
        {
            //ExStart
            //ExFor:Section.EnsureMinimum
            //ExSummary:Ensures that a section is valid.
            // Create a blank document
            Aspose.Words.Document doc     = new Aspose.Words.Document();
            Aspose.Words.Section  section = doc.FirstSection;

            // Makes sure that the section contains a body with at least one paragraph.
            section.EnsureMinimum();
            //ExEnd
        }
Exemple #15
0
        /// <summary>
        /// Clones and copies headers/footers form the previous section to the specified section.
        /// </summary>
        private static void CopyHeadersFootersFromPreviousSection(Aspose.Words.Section section)
        {
            Aspose.Words.Section previousSection = (Aspose.Words.Section)section.PreviousSibling;

            if (previousSection == null)
            {
                return;
            }

            section.HeadersFooters.Clear();

            foreach (Aspose.Words.HeaderFooter headerFooter in previousSection.HeadersFooters)
            {
                section.HeadersFooters.Add(headerFooter.Clone(true));
            }
        }
Exemple #16
0
        public void PageNumbering()
        {
            //ExStart
            //ExFor:PageSetup.RestartPageNumbering
            //ExFor:PageSetup.PageStartingNumber
            //ExFor:PageSetup.PageNumberStyle
            //ExFor:DocumentBuilder.InsertField(string, string)
            //ExSummary:Shows how to control page numbering per section.
            // This document has two sections, but no page numbers yet.
            Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "PageSetup.PageNumbering.doc");

            // Use document builder to create a header with a page number field for the first section.
            // The page number will look like "Page V".
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.MoveToSection(0);
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.Write("Page ");
            builder.InsertField("PAGE", "");

            // Set first section page numbering.
            Aspose.Words.Section section = doc.Sections[0];
            section.PageSetup.RestartPageNumbering = true;
            section.PageSetup.PageStartingNumber   = 5;
            section.PageSetup.PageNumberStyle      = NumberStyle.UppercaseRoman;


            // Create a header for the section section.
            // The page number will look like " - 10 - ".
            builder.MoveToSection(1);
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Write(" - ");
            builder.InsertField("PAGE", "");
            builder.Write(" - ");

            // Set second section page numbering.
            section = doc.Sections[1];
            section.PageSetup.PageStartingNumber   = 10;
            section.PageSetup.RestartPageNumbering = true;
            section.PageSetup.PageNumberStyle      = NumberStyle.Arabic;

            doc.Save(MyDir + "PageSetup.PageNumbering Out.doc");
            //ExEnd
        }
        public void SectionsAppendSectionContent()
        {
            //ExStart
            //ExFor:Section.AppendContent
            //ExFor:Section.PrependContent
            //ExId:SectionsAppendSectionContent
            //ExSummary:Shows how to append content of an existing section. The number of sections in the document remains the same.
            Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Section.AppendContent.doc");

            // This is the section that we will append and prepend to.
            Aspose.Words.Section section = doc.Sections[2];

            // This copies content of the 1st section and inserts it at the beginning of the specified section.
            Aspose.Words.Section sectionToPrepend = doc.Sections[0];
            section.PrependContent(sectionToPrepend);

            // This copies content of the 2nd section and inserts it at the end of the specified section.
            Aspose.Words.Section sectionToAppend = doc.Sections[1];
            section.AppendContent(sectionToAppend);
            //ExEnd
        }
Exemple #18
0
        /// <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;
                }
            }
        }
Exemple #19
0
        public void EnumNextSibling()
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            //ExStart
            //ExFor:CompositeNode.FirstChild
            //ExFor:Node.NextSibling
            //ExFor:Node.NodeTypeToString
            //ExFor:Node.NodeType
            //ExSummary:Shows how to enumerate immediate child nodes of a composite node using NextSibling. In this example we enumerate all paragraphs of a section body.
            // Get the section that we want to work on.
            Aspose.Words.Section section = doc.Sections[0];
            Body body = section.Body;

            // Loop starting from the first child until we reach null.
            for (Aspose.Words.Node node = body.FirstChild; node != null; node = node.NextSibling)
            {
                // Output the types of the nodes that we come across.
                Console.WriteLine(Aspose.Words.Node.NodeTypeToString(node.NodeType));
            }
            //ExEnd
        }
Exemple #20
0
        public void TypedAccess()
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            //ExStart
            //ExFor:Story.Tables
            //ExFor:Table.FirstRow
            //ExFor:Table.LastRow
            //ExFor:TableCollection
            //ExId:TypedPropertiesAccess
            //ExSummary:Demonstrates how to use typed properties to access nodes of the document tree.
            // Quick typed access to the first child Section node of the Document.
            Aspose.Words.Section section = doc.FirstSection;

            // Quick typed access to the Body child node of the Section.
            Body body = section.Body;

            // Quick typed access to all Table child nodes contained in the Body.
            TableCollection tables = body.Tables;

            foreach (Table table in tables)
            {
                // Quick typed access to the first row of the table.
                if (table.FirstRow != null)
                {
                    table.FirstRow.Remove();
                }

                // Quick typed access to the last row of the table.
                if (table.LastRow != null)
                {
                    table.LastRow.Remove();
                }
            }
            //ExEnd
        }
Exemple #21
0
 public void SectionsAddSection()
 {
     //ExStart
     //ExFor:NodeCollection.Add
     //ExId:SectionsAddSection
     //ExSummary:Shows how to add a section to the end of the document.
     Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Document.doc");
     Aspose.Words.Section sectionToAdd = new Aspose.Words.Section(doc);
     doc.Sections.Add(sectionToAdd);
     //ExEnd
 }
Exemple #22
0
        //ExStart
        //ExId:HeaderFooterPrimer
        //ExSummary:Maybe a bit complicated example, but demonstrates many things that can be done with headers/footers.
        public void Primer()
        {
            Aspose.Words.Document doc     = new Aspose.Words.Document();
            DocumentBuilder       builder = new DocumentBuilder(doc);

            Aspose.Words.Section   currentSection = builder.CurrentSection;
            Aspose.Words.PageSetup pageSetup      = currentSection.PageSetup;

            // Specify if we want headers/footers of the first page to be different from other pages.
            // You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
            // different headers/footers for odd and even pages.
            pageSetup.DifferentFirstPageHeaderFooter = true;

            // --- Create header for the first page. ---
            pageSetup.HeaderDistance = 20;
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

            // Set font properties for header text.
            builder.Font.Name = "Arial";
            builder.Font.Bold = true;
            builder.Font.Size = 14;
            // Specify header title for the first page.
            builder.Write("Aspose.Words Header/Footer Creation Primer - Title Page.");

            // --- Create header for pages other than first. ---
            pageSetup.HeaderDistance = 20;
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

            // Insert absolutely positioned image into the top/left corner of the header.
            // Distance from the top/left edges of the page is set to 10 points.
            string imageFileName = MyDir + "Aspose.Words.gif";

            builder.InsertImage(imageFileName, RelativeHorizontalPosition.Page, 10, RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);

            builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
            // Specify another header title for other pages.
            builder.Write("Aspose.Words Header/Footer Creation Primer.");

            // --- Create footer for pages other than first. ---
            builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

            // We use table with two cells to make one part of the text on the line (with page numbering)
            // to be aligned left, and the other part of the text (with copyright) to be aligned right.
            builder.StartTable();

            // Clear table borders.
            builder.CellFormat.ClearFormatting();

            builder.InsertCell();

            // Set first cell to 1/3 of the page width.
            builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3);

            // Insert page numbering text here.
            // It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
            builder.Write("Page ");
            builder.InsertField("PAGE", "");
            builder.Write(" of ");
            builder.InsertField("NUMPAGES", "");

            // Align this text to the left.
            builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;

            builder.InsertCell();
            // Set the second cell to 2/3 of the page width.
            builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 * 2 / 3);

            builder.Write("(C) 2001 Aspose Pty Ltd. All rights reserved.");

            // Align this text to the right.
            builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;

            builder.EndRow();
            builder.EndTable();

            builder.MoveToDocumentEnd();
            // Make page break to create a second page on which the primary headers/footers will be seen.
            builder.InsertBreak(BreakType.PageBreak);

            // Make section break to create a third page with different page orientation.
            builder.InsertBreak(BreakType.SectionBreakNewPage);

            // Get the new section and its page setup.
            currentSection = builder.CurrentSection;
            pageSetup      = currentSection.PageSetup;

            // Set page orientation of the new section to landscape.
            pageSetup.Orientation = Orientation.Landscape;

            // This section does not need different first page header/footer.
            // We need only one title page in the document and the header/footer for this page
            // has already been defined in the previous section
            pageSetup.DifferentFirstPageHeaderFooter = false;

            // This section displays headers/footers from the previous section by default.
            // Call currentSection.HeadersFooters.LinkToPrevious(false) to cancel this.
            // Page width is different for the new section and therefore we need to set
            // a different cell widths for a footer table.
            currentSection.HeadersFooters.LinkToPrevious(false);

            // If we want to use the already existing header/footer set for this section
            // but with some minor modifications then it may be expedient to copy headers/footers
            // from the previous section and apply the necessary modifications where we want them.
            CopyHeadersFootersFromPreviousSection(currentSection);

            // Find the footer that we want to change.
            Aspose.Words.HeaderFooter primaryFooter = currentSection.HeadersFooters[HeaderFooterType.FooterPrimary];

            Row row = primaryFooter.Tables[0].FirstRow;

            row.FirstCell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100 / 3);
            row.LastCell.CellFormat.PreferredWidth  = PreferredWidth.FromPercent(100 * 2 / 3);

            // Save the resulting document.
            doc.Save(MyDir + "HeaderFooter.Primer Out.doc");
        }
Exemple #23
0
        private static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, HeaderFooterType headerType)
        {
            Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

            if (header == null)
            {
                header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
                sect.HeadersFooters.Add(header);
            }
            header.AppendChild(watermarkPara.Clone(true));
        }
        public void CreateFromScratch()
        {
            //ExStart
            //ExFor:Node.GetText
            //ExFor:CompositeNode.RemoveAllChildren
            //ExFor:CompositeNode.AppendChild
            //ExFor:Section
            //ExFor:Section.#ctor
            //ExFor:Section.PageSetup
            //ExFor:PageSetup.SectionStart
            //ExFor:PageSetup.PaperSize
            //ExFor:SectionStart
            //ExFor:PaperSize
            //ExFor:Body
            //ExFor:Body.#ctor
            //ExFor:Paragraph
            //ExFor:Paragraph.#ctor
            //ExFor:Paragraph.ParagraphFormat
            //ExFor:ParagraphFormat
            //ExFor:ParagraphFormat.StyleName
            //ExFor:ParagraphFormat.Alignment
            //ExFor:ParagraphAlignment
            //ExFor:Run
            //ExFor:Run.#ctor(DocumentBase)
            //ExFor:Run.Text
            //ExFor:Inline.Font
            //ExSummary:Creates a simple document from scratch using the Aspose.Words object model.

            // Create an "empty" document. Note that like in Microsoft Word,
            // the empty document has one section, body and one paragraph in it.
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // This truly makes the document empty. No sections (not possible in Microsoft Word).
            doc.RemoveAllChildren();

            // Create a new section node.
            // Note that the section has not yet been added to the document,
            // but we have to specify the parent document.
            Aspose.Words.Section section = new Aspose.Words.Section(doc);

            // Append the section to the document.
            doc.AppendChild(section);

            // Lets set some properties for the section.
            section.PageSetup.SectionStart = SectionStart.NewPage;
            section.PageSetup.PaperSize    = PaperSize.Letter;


            // The section that we created is empty, lets populate it. The section needs at least the Body node.
            Body body = new Body(doc);

            section.AppendChild(body);


            // The body needs to have at least one paragraph.
            // Note that the paragraph has not yet been added to the document,
            // but we have to specify the parent document.
            // The parent document is needed so the paragraph can correctly work
            // with styles and other document-wide information.
            Paragraph para = new Paragraph(doc);

            body.AppendChild(para);

            // We can set some formatting for the paragraph
            para.ParagraphFormat.StyleName = "Heading 1";
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;


            // So far we have one empty paragraph in the document.
            // The document is valid and can be saved, but lets add some text before saving.
            // Create a new run of text and add it to our paragraph.
            Run run = new Run(doc);

            run.Text       = "Hello World!";
            run.Font.Color = System.Drawing.Color.Red;
            para.AppendChild(run);


            // As a matter of interest, you can retrieve text of the whole document and
            // see that \x000c is automatically appended. \x000c is the end of section character.
            Console.WriteLine("Hello World!\x000c", doc.GetText());

            // Save the document.
            doc.Save(MyDir + "Section.CreateFromScratch Out.doc");
            //ExEnd

            Assert.AreEqual("Hello World!\x000c", doc.GetText());
        }
Exemple #25
0
        /// <summary>
        /// Converts a textbox to a table by copying the same content and formatting.
        /// Currently export to HTML will render the textbox as an image which looses any text functionality.
        /// This is useful to convert textboxes in order to retain proper text.
        /// </summary>
        /// <param name="textbox">The textbox shape to convert to a table</param>
        private static void ConvertTextboxToTable(Shape textBox)
        {
            if (textBox.StoryType != StoryType.Textbox)
            {
                throw new ArgumentException("Can only convert a shape of type textbox");
            }

            Aspose.Words.Document doc     = (Aspose.Words.Document)textBox.Document;
            Aspose.Words.Section  section = (Aspose.Words.Section)textBox.GetAncestor(NodeType.Section);

            // Create a table to replace the textbox and transfer the same content and formatting.
            Table table = new Table(doc);

            // Ensure that the table contains a row and a cell.
            table.EnsureMinimum();
            // Use fixed column widths.
            table.AutoFit(AutoFitBehavior.FixedColumnWidths);

            // A shape is inline level (within a paragraph) where a table can only be block level so insert the table
            // after the paragraph which contains the shape.
            Aspose.Words.Node shapeParent = textBox.ParentNode;
            shapeParent.ParentNode.InsertAfter(table, shapeParent);

            // If the textbox is not inline then try to match the shape's left position using the table's left indent.
            if (!textBox.IsInline && textBox.Left < section.PageSetup.PageWidth)
            {
                table.LeftIndent = textBox.Left;
            }

            // We are only using one cell to replicate a textbox so we can make use of the FirstRow and FirstCell property.
            // Carry over borders and shading.
            Row  firstRow  = table.FirstRow;
            Cell firstCell = firstRow.FirstCell;

            firstCell.CellFormat.Borders.Color     = textBox.StrokeColor;
            firstCell.CellFormat.Borders.LineWidth = textBox.StrokeWeight;
            firstCell.CellFormat.Shading.BackgroundPatternColor = textBox.Fill.Color;

            // Transfer the same height and width of the textbox to the table.
            firstRow.RowFormat.HeightRule = HeightRule.Exactly;
            firstRow.RowFormat.Height     = textBox.Height;
            firstCell.CellFormat.Width    = textBox.Width;
            table.AllowAutoFit            = false;

            // Replicate the textbox's horizontal alignment.
            TableAlignment horizontalAlignment;

            switch (textBox.HorizontalAlignment)
            {
            case HorizontalAlignment.Left:
                horizontalAlignment = TableAlignment.Left;
                break;

            case HorizontalAlignment.Center:
                horizontalAlignment = TableAlignment.Center;
                break;

            case HorizontalAlignment.Right:
                horizontalAlignment = TableAlignment.Right;
                break;

            default:
                // Most other options are left by default.
                horizontalAlignment = TableAlignment.Left;
                break;
            }

            table.Alignment = horizontalAlignment;
            firstCell.RemoveAllChildren();

            // Append all content from the textbox to the new table
            foreach (Aspose.Words.Node node in textBox.GetChildNodes(NodeType.Any, false).ToArray())
            {
                table.FirstRow.FirstCell.AppendChild(node);
            }

            // Remove the empty textbox from the document.
            textBox.Remove();
        }
Exemple #26
-1
        public void CreateFromScratch()
        {
            //ExStart
            //ExFor:Node.GetText
            //ExFor:CompositeNode.RemoveAllChildren
            //ExFor:CompositeNode.AppendChild
            //ExFor:Section
            //ExFor:Section.#ctor
            //ExFor:Section.PageSetup
            //ExFor:PageSetup.SectionStart
            //ExFor:PageSetup.PaperSize
            //ExFor:SectionStart
            //ExFor:PaperSize
            //ExFor:Body
            //ExFor:Body.#ctor
            //ExFor:Paragraph
            //ExFor:Paragraph.#ctor
            //ExFor:Paragraph.ParagraphFormat
            //ExFor:ParagraphFormat
            //ExFor:ParagraphFormat.StyleName
            //ExFor:ParagraphFormat.Alignment
            //ExFor:ParagraphAlignment
            //ExFor:Run
            //ExFor:Run.#ctor(DocumentBase)
            //ExFor:Run.Text
            //ExFor:Inline.Font
            //ExSummary:Creates a simple document from scratch using the Aspose.Words object model.

            // Create an "empty" document. Note that like in Microsoft Word,
            // the empty document has one section, body and one paragraph in it.
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // This truly makes the document empty. No sections (not possible in Microsoft Word).
            doc.RemoveAllChildren();

            // Create a new section node.
            // Note that the section has not yet been added to the document,
            // but we have to specify the parent document.
            Aspose.Words.Section section = new Aspose.Words.Section(doc);

            // Append the section to the document.
            doc.AppendChild(section);

            // Lets set some properties for the section.
            section.PageSetup.SectionStart = SectionStart.NewPage;
            section.PageSetup.PaperSize = PaperSize.Letter;

            // The section that we created is empty, lets populate it. The section needs at least the Body node.
            Body body = new Body(doc);
            section.AppendChild(body);

            // The body needs to have at least one paragraph.
            // Note that the paragraph has not yet been added to the document,
            // but we have to specify the parent document.
            // The parent document is needed so the paragraph can correctly work
            // with styles and other document-wide information.
            Paragraph para = new Paragraph(doc);
            body.AppendChild(para);

            // We can set some formatting for the paragraph
            para.ParagraphFormat.StyleName = "Heading 1";
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;

            // So far we have one empty paragraph in the document.
            // The document is valid and can be saved, but lets add some text before saving.
            // Create a new run of text and add it to our paragraph.
            Run run = new Run(doc);
            run.Text = "Hello World!";
            run.Font.Color = System.Drawing.Color.Red;
            para.AppendChild(run);

            // As a matter of interest, you can retrieve text of the whole document and
            // see that \x000c is automatically appended. \x000c is the end of section character.
            Console.WriteLine("Hello World!\x000c", doc.GetText());

            // Save the document.
            doc.Save(ExDir + "Section.CreateFromScratch Out.doc");
            //ExEnd

            Assert.AreEqual("Hello World!\x000c", doc.GetText());
        }