Spire.Doc Knowledgebase - Page11
Spire.Doc for .NET

With Spire.Doc, we can set the formats for paragraph in C#. This article will focus on demonstrate how to set the spacing before and after the paragraph in C#.

Set the spacing before and after the paragraph for a newly added paragraph added by the method of paragraph.AppendHTML() to a new blank word document.

//create a new word document and add a section and paragraph to it.
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();

//Add the text strings to the paragraph and set the style
para.AppendHTML("

Add a new paragraph to the word and set the spacing

"); para.ApplyStyle(BuiltinStyle.Heading1); //set the spacing before and after para.Format.BeforeAutoSpacing = false; para.Format.BeforeSpacing = 20; para.Format.AfterAutoSpacing = false; para.Format.AfterSpacing = 20; //save the document to file doc.SaveToFile("Result1.docx");

How to set the spacing before and after the paragraph in C#

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

     static void Main(string[] args)
     {
         //create a new word document and load the sample from file
         Document document = new Document();
         document.LoadFromFile("sample.docx", FileFormat.Docx);

         //Add the text strings to the paragraph and set the style
         Paragraph para = new Paragraph(document);
         TextRange textRange1 = para.AppendText("This is a inserted paragraph.");
         textRange1.CharacterFormat.TextColor = Color.Blue;
         textRange1.CharacterFormat.FontSize = 15;

         //set the spacing before and after
         para.Format.BeforeAutoSpacing = false;
         para.Format.BeforeSpacing = 10;
         para.Format.AfterAutoSpacing = false;
         para.Format.AfterSpacing = 10;

         //insert the added paragraph to the word document
         document.Sections[0].Paragraphs.Insert(1, para);

         //save the document to file
         document.SaveToFile("Result2.docx", FileFormat.Docx2010);
     }

    }
}

How to set the spacing before and after the paragraph in C#

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

Add Tab Stops to Word Paragraphs in C#

2017-09-18 09:28:27 Written by Koohji

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();

     }

    }
}
page 12