News Category

Spire.Doc for .NET

Spire.Doc supports to add new shape and shape group to the word document, it also supports to remove the shapes from the word. This article will show you how to reset the size of shape on an existing word document via Spire.Doc.

Firstly, view the original shape on the word document:

C# reset the size of shape on the word

Step 1: Create a new instance of word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

Step 2: Get the first section and the third paragraph that contains the shape.

Section section = doc.Sections[0]; 
Paragraph para = section.Paragraphs[2];

Step 3: Get the shape and reset the width and height for the shape.

ShapeObject shape = para.ChildObjects[0] as ShapeObject;

shape.Width = 400;
shape.Height = 300;

Step 4: Save the document to file.

doc.SaveToFile("result.docx",FileFormat.Docx2010);

Effective screenshot of the shape after reset the size.

C# reset the size of shape on the word

Full codes:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Reset
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

            Section section = doc.Sections[0];
            Paragraph para = section.Paragraphs[2];

            ShapeObject shape = para.ChildObjects[0] as ShapeObject;

            shape.Width = 400;
            shape.Height = 300;

            doc.SaveToFile("result.docx", FileFormat.Docx2010);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace Reset
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New Document()
			doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)

			Dim section As Section = doc.Sections(0)
			Dim para As Paragraph = section.Paragraphs(2)

			Dim shape As ShapeObject = TryCast(para.ChildObjects(0), ShapeObject)

			shape.Width = 400
			shape.Height = 300

			doc.SaveToFile("result.docx", FileFormat.Docx2010)
		End Sub
	End Class
End Namespace

Tab stops are markers placed on the ruler that define how text or numbers are aligned on a line. To add tab stops to a paragraph in Microsoft Word, we need to open the Tabs dialog box and then set the tab stop position, alignment and leader as shown below.

Add Tab Stops to Word Paragraphs in C#

This article elaborates how to add tab stops to paragraphs in word document programmatically using Spire.Doc.

Detail steps:

Step 1: Instantiate a Document object and add a section to it.

Document document = new Document();
Section section = document.AddSection(); 

Step 2: Add paragraph 1 to the section.

Paragraph paragraph1 = section.AddParagraph();

Step 3: Add tab stops to paragraph 1.

//Add tab and set its position (in points)
Tab tab = paragraph1.Format.Tabs.AddTab(28);
//Set tab alignment
tab.Justification = TabJustification.Left;
//move to next tab and append text
paragraph1.AppendText("\tWashing Machine");

//Add another tab and set its position (in points)
tab = paragraph1.Format.Tabs.AddTab(280);
//Set tab alignment
tab.Justification = TabJustification.Left;
//Specify tab leader type
tab.TabLeader = TabLeader.Dotted;
//move to next tab and append text
paragraph1.AppendText("\t$650"); 

Step 4: Add paragraph 2 to the section.

Paragraph paragraph2 = section.AddParagraph();

Step 5: Add tab stops to paragraph 2.

//Add tab and set its position (in points)
tab = paragraph2.Format.Tabs.AddTab(28);
//Set tab alignment
tab.Justification = TabJustification.Left;
//move to next tab and append text
paragraph2.AppendText("\tRefrigerator");

//Add another tab and set its position (in points)
tab = paragraph2.Format.Tabs.AddTab(280);
//Set tab alignment
tab.Justification = TabJustification.Left;
//Specify tab leader type
tab.TabLeader = TabLeader.NoLeader;
//move to next tab and append text
paragraph2.AppendText("\t$800"); 

Step 6: Save and close the document object.

document.SaveToFile("Tab.docx", FileFormat.Docx2013);
document.Close();

Screenshot:

Add Tab Stops to Word Paragraphs in C#

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
namespace AddTapStops
{
 class Program
    {

     static void Main(string[] args)
     {
         //Instantiate a Document object
         Document document = new Document();
         //Add a section
         Section section = document.AddSection();

         //Add paragraph 1
         Paragraph paragraph1 = section.AddParagraph();

         //Add tab and set its position (in points)
         Tab tab = paragraph1.Format.Tabs.AddTab(28);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //move to next tab and append text
         paragraph1.AppendText("\tWashing Machine");

         //Add another tab and set its position (in points)
         tab = paragraph1.Format.Tabs.AddTab(280);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //Specify tab leader type
         tab.TabLeader = TabLeader.Dotted;
         //move to next tab and append text
         paragraph1.AppendText("\t$650");

         //Add paragraph 2
         Paragraph paragraph2 = section.AddParagraph();

         //Add tab and set its position (in points)
         tab = paragraph2.Format.Tabs.AddTab(28);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //move to next tab and append text
         paragraph2.AppendText("\tRefrigerator"); //move to next tab and append text

         //Add another tab and set its position (in points)
         tab = paragraph2.Format.Tabs.AddTab(280);
         //Set tab alignment
         tab.Justification = TabJustification.Left;
         //Specify tab leader type
         tab.TabLeader = TabLeader.NoLeader;
         //move to next tab and append text
         paragraph2.AppendText("\t$800");

         //Save and close the document object            
         document.SaveToFile("Tab.docx", FileFormat.Docx2013);
         document.Close();

     }

    }
}

In Word, textbox can contain multiple elements such as text, image and table. This article demonstrates how to insert table into word textbox, and read and delete existing table from word textbox using Spire.Doc.

Insert table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace InsertTable
{
    class Program
    {

        static void Main(string[] args)
        {

            //Create a Document instance
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();
            //Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            //Add textbox to the paragraph
            TextBox textbox = paragraph.AppendTextBox(300, 100);

            //Add text to textbox
            Paragraph textboxParagraph = textbox.Body.AddParagraph();
            TextRange textboxRange = textboxParagraph.AppendText("Table 1");
            textboxRange.CharacterFormat.FontName = "Arial";

            //Insert table to textbox
            Table table = textbox.Body.AddTable(true);
            //Specify the number of rows and columns of the table
            table.ResetCells(4, 4);

            string[,] data = new string[,]  
{  
{"Name","Age","Gender","ID" },  
{"John","28","Male","0023" },  
{"Steve","30","Male","0024" },  
{"Lucy","26","female","0025" }  
};

            //Add data to table 
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]);
                    tableRange.CharacterFormat.FontName = "Arial";
                }
            }

            //Apply style to table
            table.ApplyStyle(DefaultTableStyle.LightGridAccent3);

            //Save the document
            document.SaveToFile("Output.docx", FileFormat.Docx2013);

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Read table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;
namespace ReadTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Get the first table in the textbox
            Table table = textbox.Body.Tables[0] as Table;

            StringBuilder sb = new StringBuilder();

            //Loop through the paragraphs of the table and extract text to a .txt file
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteAllText("text.txt", sb.ToString());

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Delete table

using Spire.Doc;
using Spire.Doc.Fields;
namespace DeleteTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Remove the first table from the textbox
            textbox.Body.Tables.RemoveAt(0);

            //Save the document
            document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);


        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Page 10 of 56