C#/VB.NET: How to set Word paragraph shading ?

       As everybody knows, paragraph shading plays a significant role in article. It can effectively attract readers' attention and achieve the effect of highlighting the key points of the article.
       Spire.Doc for .NET , an easy-to-use component to operate Word document for developers' .NET applications, enables users to set Word shading for paragraphs in document. This guide will focus on how to realize this function by using C#, VB.NET via Spire.Doc for .NET. This article will show you how to set Word paragraph shading in C#/VB.NET.  

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet .

PM> Install-Package Spire.Doc

Here are the steps:

  • Create Word document and using the document.LoadFromFile()method to  load  the file from disk.
  • Get a paragraph and set background color for the paragraph through paragaph.Format.BackColor 
  • Using paragaph.Find()method to find the text of paragraph. Though CharacterFormat.TextBackgroundColor property to set background color for the selected text of paragraph.
  • Using document.SaveToFile() to save the file.
Full code:

[C#]

using System;

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

namespace setwordparagraphshading

{

    class program

    {

        static void Main( string [] args)

        {

            //Create Word document.

            Document document = new Document();

            //Load the file from disk.

            document.LoadFromFile( "Template_Docx_1.docx" );

            //Get a paragraph.

            Paragraph paragaph = document.Sections[0].Paragraphs[0];

            //Set background color for the paragraph.

            paragaph.Format.BackColor = Color.Yellow;

            //Set background color for the selected text of paragraph.

            paragaph = document.Sections[0].Paragraphs[2];

            TextSelection selection = paragaph.Find( "Christmas" , true , false );

            Spire.Doc.Fields.TextRange range = selection.GetAsOneRange();

            range.CharacterFormat.TextBackgroundColor = Color.Yellow;

            String result = "Result-SetParagraphShading.docx" ;

            //Save to file.

            document.SaveToFile(result, FileFormat.Docx2013);


[VB.NET]

Imports  System
Imports  System.Drawing
Imports  Spire.Doc
Imports  Spire.Doc.Documents

Namespace  setwordparagraphshading
    
    
Class  Program
        
        
Private Shared Sub  Main( ByVal  args()  As String )
            
'Create Word document.
            
Dim  document  As  Document  = New  Document
            
'Load the file from disk.
            
document.LoadFromFile( "Template_Docx_1.docx" )
            
'Get a paragraph.
            
Dim  paragaph  As  Paragraph  document.Sections( 0 ).Paragraphs( 0 )
            
'Set background color for the paragraph.
            
paragaph. Format .BackColor  Color.Yellow
            
'Set background color for the selected text of paragraph.
            
paragaph  document.Sections( 0 ).Paragraphs( 2 )
            
Dim  selection  As  TextSelection  paragaph.Find( "Christmas" true false )
            
Dim  range  As  Spire.Doc.Fields.TextRange  selection.GetAsOneRange
            range.CharacterFormat.TextBackgroundColor 
Color.Yellow
            
Dim  result  As String =  "Result-SetParagraphShading.docx"
            
'Save to file.
            
document.SaveToFile(result, FileFormat.Docx2013)
        
End Sub
    End Class
End Namespace

Effective shot:


ATTENTION:

The code  path is for reference only, and you can customize the path as you want.

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

If you'd like to learn more, please click on the link blow:

Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application