Spire.Doc

If you're working with a request for proposal, a standard operating procedure file, or another document type that dictates your format, a Word document may be page after page of just text. Your document would be cumbersome and unclear. So your document need a table to record your information which you need show. A table not only provides a visual grouping of information, but also makes writer more convenient to modify and query data in table. In particular when you have a table with colorful and diverse borders, your document would be stunning.

Spire.Doc for .NET, the professional .NET Word component to operate Word document, provides a Table class to perform tasks on table, for example, including rows, columns, cells, borders and table layout operation. This guide introduces a solution to create Word table with data and set its border in C#.NET via Spire.Doc for .NET. To finish this guide, you should Download and install Spire.Doc for .NET at first.

The following will show a specified example to create Word table and set its border step by step. The screenshot below presents result of this example.

Create a table and set its Border in Doc Document

We suppose that you have finished the tutorial Spire.Doc Quick Start before this example.

  • 1. Firstly, create a new Document instance. Invoke AddTable method to create a table into the Section, passing a parameter value true to indicate the new table having border. Invoke ResetCells method to set the numbers of rows and columns of table.
  • 2. Secondly, set the table's header and cells format.
  • 3. Thirdly, set table's border format.int this step, you can set the bottom, top, left, right border's format. For example, you can set the Color, the LineWidth, the border whether exist and BorderType which you have many types to choice such as Dot, Double, Hairline and so on.
  • 4. Finally, you can set Vertical and Horizontal border such as step 3. And you can also set special rows as above.

The full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Table
{
Public class table
 {    
   static void Main(string[] args)
       {           
 //Open a blank word document as template
      Document document  = new Document(Blank.doc");
      addTable(document.Sections[0]);
           
      //Save doc file.
      document.SaveToFile("Sample.doc",FileFormat.Doc);

      //Launching the MS Word file.
      System.Diagnostics.Process.Start ("Sample.doc");
}
   private void addTable(Section section)
        {
//create a table with border
Spire.Doc.Table table = section.AddTable(true);
    String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
    String[][] data =
        {   new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
            new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
            new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
            new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
            new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
            new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
                           };
    
    table.ResetCells(data.Length + 1, header.Length);

    // ***************** First Row *************************
     TableRow Frow = table.Rows[0];
     Frow.IsHeader = true;
     Frow.Height = 20;    //unit: point, 1point = 0.3528 mm
     Frow.HeightType = TableRowHeightType.Exactly;
     Frow.RowFormat.BackColor = Color.Pink;
     for (int i = 0; i < header.Length; i++)
       {
       Frow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
       Paragraph p = Frow.Cells[i].AddParagraph(); 
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
        TextRange txtRange = p.AppendText(header[i]);
        txtRange.CharacterFormat.Bold = true;
       }
   for (int r = 0; r < data.Length; r++)
      {
       TableRow dataRow = table.Rows[r + 1];
       dataRow.Height = 20;
       dataRow.HeightType = TableRowHeightType.Exactly;
       dataRow.RowFormat.BackColor = Color.Empty;
       for (int c = 0; c < data[r].Length; c++)
          {
          dataRow.Cells[c].CellFormat.VerticalAlignment =VerticalAlignment.Middle;
dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
            }

    //set right border of table
    table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Right. LineWidth = 2.0F;
table.TableFormat.Borders.Right.Color = Color.GreenYellow;
//set top border of table
    table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Top.LineWidth = 4.0F;
table.TableFormat.Borders.Top.Color = Color.GreenYellow;
//set left border of table
    table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline;
    table.TableFormat.Borders.Left.LineWidth = 2.0F;
table.TableFormat.Borders.Left.Color = Color.GreenYellow;
//set bottom border is none
    table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.None;
//set vertical and horizontal border 
    table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Dot;
table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.None;
table.TableFormat.Borders.Vertical.Color = Color.Orange;
//set special row's border
    table.Rows[0].RowFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Double;
    table.Rows[0].RowFormat.Borders.Horizontal.Color = Color.HotPink;
 table.Rows[3].RowFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Thick;
table.Rows[3].RowFormat.Borders.Horizontal.Color = Color.HotPink;

   }               
}
}
Monday, 31 March 2014 01:47

How to insert section break in a document

Written by support iceblue

In Microsoft Word, you can easily split the document into sections by insert a section break in the place where you want, thus allowing you to apply different formatting or layout options to those sections. Section breaks in Microsoft Word can be a godsend if you need to perform any of the following tasks:

  • Use page numbering that starts at 1 for each section of your document (for example, different chapters in the same document).
  • Display text in two columns for only a portion of your document and then return to the default one column afterwards.
  • Display different header and footer information for different parts of your document.

This article demonstrates an easy solution to insert section break to split document into sections in C#, VB.NET via Spire.Doc for .NET. The following screenshot shows result after inserting Word page break.

Insert section break in a document

Spire.Doc for .NET provides Paragraph.InsertSectionBreak method for customers to insert break easily in Word. The overload, which should be passed to this method, is SectionBreakType (enum-type). There are five section break options offered by Spire.Doc for .NET such as EvenPage, NewColumnNewPage, NoBreak, OddPage. Download and install Spire.Doc for .NET and follow the code blow to insert section break behind the specified paragraph.

Document doc = new Document();
doc.LoadFromFile("E-iceblue Technology_new.docx");
doc.Sections[0].Paragraphs[0].InsertSectionBreak(SectionBreakType.NoBreak);
doc.SaveToFile("SectionBreak.docx",FileFormat.Docx);

Paragraph is a very important element in word document. It is powerful and has many properties. Spire.Doc, a .NET component specially designed for developers enables you to manipulate paragraphs easily and flexibly.

There are documents on our website introducing how to set Alignment, Indent and Bullets. And this article introduces you how to set Spacing of paragraphs in word document.

Step 1: Create a word document.

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

Step 2: Add the first paragraph to the document.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Thank you for requesting a trial version of Spire.Doc. The component will work normally except for an evaluation warning message. Once you purchase a license file, the evaluation warning will disappeare.");

Step 3: Set the Line spacing of the first paragraph.

paragraph.Format.LineSpacing = 20;

Step 4: Set the Spacing Before of the first paragraph.

paragraph.Format.BeforeSpacing = 30;

Step 5: Set the Spacing After of the first paragraph

paragraph.Format.AfterSpacing = 15;

Step 6: Save the document.

document.SaveToFile("result.doc", FileFormat.Doc);

Full code:

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

//add the first paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Thank you for requesting a trial version of Spire.Doc. The component will work normally except for an evaluation warning message. Once you purchase a license file, the evaluation warning will disappeare.");
paragraph.Format.FirstLineIndent = 20;

//set the Line spacing
paragraph.Format.LineSpacing = 20;

//set the Spacing Before
paragraph.Format.BeforeSpacing = 30;

//set the Spacing After
paragraph.Format.AfterSpacing = 15;

//add the second paragraph
Paragraph paragraph2 = section.AddParagraph();
paragraph2.AppendText("Spire.Doc (Pack) is a compilation of Spire.Doc for .NET, Spire.Doc for Silverlight and Spire.Doc for WPF. It is a professional and powerful Word component which enables you to generate, load, write, edit and save Word document on .NET, Silverlight and WPF. With Spire.Doc, you can create a large range of applications.");
paragraph2.Format.FirstLineIndent = 20;

//set the Line spacing
paragraph2.Format.LineSpacing = 25;

//save the document
document.SaveToFile("result.doc", FileFormat.Doc);

System.Diagnostics.Process.Start("result.doc");

Screenshot:

Set Spacing of Paragraphs in word document

Page 1 of 23