News Category

Apply a border around characters or sentence in word document

2015-05-15 02:20:38 Written by  support iceblue
Rate this item
(0 votes)

To emphasize and beautify a set of characters or sentence, applying a border around the characters or sentence is a good option. Spire.Doc enables developers to achieve this feature in C#. And there are plenty of built-in border styles available, such as: Wave, Hairline, DotDash, DashSmallGap, DashLargeGap, DoubleWave, DashDotStroker, Emboss3D, Engrave3D, TwistedLines1 and so on. The following codes show how to achieve character border with some border styles mentioned above:

Note: Before start, please make sure that Visual Studio and Spire.Doc have been installed properly. We will use Spire.Doc .dll as reference.

Step 1: Load word document

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

Step 2: Add the characters needed to apply a border and set the border style

//DashSmallGap Border
Paragraph para = section.AddParagraph();
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
TextRange tr = para.AppendText("Spire.Doc for .Net");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap;
tr.CharacterFormat.Border.Color = Color.Green;
tr.CharacterFormat.FontSize = 24;
tr.CharacterFormat.TextColor = Color.DarkKhaki; 
para.AppendBreak(BreakType.LineBreak);

//Wave Border
para = section.AddParagraph();
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
tr = para.AppendText("Spire.PDF for .Net");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
tr.CharacterFormat.Border.Color = Color.Aqua;
tr.CharacterFormat.FontSize = 24;
tr.CharacterFormat.TextColor = Color.BurlyWood;
para.AppendBreak(BreakType.LineBreak);

//Emboss3D Border
para = section.AddParagraph();
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
tr = para.AppendText("Spire.XLS for .Net");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D;
tr.CharacterFormat.FontSize = 24;
para.AppendBreak(BreakType.LineBreak);

//DashDotStroker Border
para = section.AddParagraph();
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
tr = para.AppendText("Spire.Office for .Net");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker;
tr.CharacterFormat.Border.Color = Color.Olive;
tr.CharacterFormat.FontSize = 24;
tr.CharacterFormat.TextColor = Color.Olive;
para.AppendBreak(BreakType.LineBreak);

//DoubleWave Border
para = section.AddParagraph();
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
tr = para.AppendText("Spire.Presentation for .Net");
tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave;
tr.CharacterFormat.Border.Color = Color.Blue;
tr.CharacterFormat.FontSize = 24;
tr.CharacterFormat.TextColor = Color.Blue;
para.AppendBreak(BreakType.LineBreak);

Step 3: Save and launch word document

doc.SaveToFile("S1.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("S1.docx");

Effect of screenshot:

How to apply a border around characters or sentence in word document

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            Section section = doc.AddSection();

            //DashSmallGap Border
            Paragraph para = section.AddParagraph();
            para.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TextRange tr = para.AppendText("Spire.Doc for .Net");
            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap;
            tr.CharacterFormat.Border.Color = Color.Green;
            tr.CharacterFormat.FontSize = 24;
            tr.CharacterFormat.TextColor = Color.DarkKhaki; 
            para.AppendBreak(BreakType.LineBreak);

            //Wave Border
            para = section.AddParagraph();
            para.Format.HorizontalAlignment = HorizontalAlignment.Left;
            tr = para.AppendText("Spire.PDF for .Net");
            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
            tr.CharacterFormat.Border.Color = Color.Aqua;
            tr.CharacterFormat.FontSize = 24;
            tr.CharacterFormat.TextColor = Color.BurlyWood;
            para.AppendBreak(BreakType.LineBreak);

            //Emboss3D Border
            para = section.AddParagraph();
            para.Format.HorizontalAlignment = HorizontalAlignment.Left;
            tr = para.AppendText("Spire.XLS for .Net");
            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D;
            tr.CharacterFormat.FontSize = 24;
            para.AppendBreak(BreakType.LineBreak);

            //DashDotStroker Border
            para = section.AddParagraph();
            para.Format.HorizontalAlignment = HorizontalAlignment.Left;
            tr = para.AppendText("Spire.Office for .Net");
            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker;
            tr.CharacterFormat.Border.Color = Color.Olive;
            tr.CharacterFormat.FontSize = 24;
            tr.CharacterFormat.TextColor = Color.Olive;
            para.AppendBreak(BreakType.LineBreak);

            //DoubleWave Border
            para = section.AddParagraph();
            para.Format.HorizontalAlignment = HorizontalAlignment.Left;
            tr = para.AppendText("Spire.Presentation for .Net");
            tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave;
            tr.CharacterFormat.Border.Color = Color.Blue;
            tr.CharacterFormat.FontSize = 24;
            tr.CharacterFormat.TextColor = Color.Blue;
            para.AppendBreak(BreakType.LineBreak);

            doc.SaveToFile("S1.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("S1.docx");

        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Thursday, 11 April 2024 01:15