C#/VB.NET: How to set Word paragraph shading ?
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.
PM> Install-Package Spire.Doc
- 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.
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
The code path is for reference only, and you can customize the path as you want.
Comments
Post a Comment