Set text style in word document

  • NPOI
  • Spire.Doc
  • Download Sample Code

using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            XWPFDocument doc = new XWPFDocument();

            //Create paragraph
            XWPFParagraph para1 = doc.CreateParagraph();
            para1.Alignment = ParagraphAlignment.CENTER;

            //Set style of the paragraph text
            XWPFRun run1 = para1.CreateRun();
            run1.SetColor("Blue");
            run1.FontSize = 18;
            run1.SetText("This is the first paragraph");
            run1.SetBold(true);

            XWPFParagraph para2 = doc.CreateParagraph();
            para2.Alignment = ParagraphAlignment.LEFT;
            para2.BorderTop = Borders.WAVE;   

            XWPFRun run2 = para2.CreateRun();
            run2.Subscript = VerticalAlign.BASELINE;            
            run2.SetColor("Red");
            run2.IsItalic = true;
            run2.SetUnderline(UnderlinePatterns.Dash);
            run2.SetText("This is the second paragraph");

            //Save the file
            using (FileStream file = File.Create("TextStyle.docx"))
            {
                doc.Write(file);
            }

            //Launch
            System.Diagnostics.Process.Start("TextStyle.docx");
        }
    }
}


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

namespace Spire.Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            Document doc = new Document();
            Section section = doc.AddSection();

            //Declare a paragraph style
            ParagraphStyle style1 = new ParagraphStyle(doc);
            style1.Name = "Style1";
            style1.CharacterFormat.Bold = true;
            style1.CharacterFormat.TextColor = Color.Gray;
            style1.CharacterFormat.FontSize = 18;
            doc.Styles.Add(style1);

            //Add paragraph and apply the declared style
            Paragraph para1 = section.AddParagraph();
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            para1.AppendText("This is the first paragraph");
            para1.ApplyStyle(style1.Name);


            //Add paragraph and set its style
            Paragraph para2 = section.AddParagraph();
            para2.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TextRange range=para2.AppendText("This is the second paragraph");
            range.CharacterFormat.Italic = true;
            range.CharacterFormat.TextColor = Color.Red;
            range.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
            range.CharacterFormat.SubSuperScript = SubSuperScript.BaseLine;
            range.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
            range.CharacterFormat.Border.Color = Color.Green;

            //Save and Launch
            doc.SaveToFile("TextStyle.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("TextStyle.docx");


        }
    }
}