LoginSignup
0
0

【C#/VB.NET】Word文書の段落やテキストの背景色を設定

Last updated at Posted at 2023-08-18

Word ドキュメントでは、段落やテキストの背景色は文書のデザインにおいて重要な要素です。適切な段落やテキストの背景色は、特定の段落やテキストを強調表示し、テキストのコントラストを高めて読みやすさを向上させる役割を果たすだけでなく、レイアウトの空白を埋めるための助けにもなります。この記事では、無料のFree Spire.Doc for .NETプログラムを使用して段落やテキストの背景色を設定する方法について説明します。

依存関係の追加

Free Spire.Doc for .NETの依存関係は、公式ウェブサイトから手動でダウンロードするか、NuGet Package Manageで検索するか、パッケージマネージャーコンソールで次のコードを使用して追加することができます。
PM> Install-Package FreeSpire.Doc

Wordドキュメントの段落に背景色を設定する

段落の背景色を設定するには、まず指定の段落を取得し、Paragraph.Format.BackColor プロパティを使用して背景色を設定します。具体的な手順は以下の通りです。

  • Document クラスのオブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントをロードします。
  • Document.Sections[] プロパティを使用してドキュメントの最初のセクションを取得します。
  • Section.Paragraphs[] プロパティを使用してそのセクションの4番目の段落を取得します。
  • Paragraph.Format.BackColor プロパティを使用してその段落の背景色を設定します。
  • Document.SaveToFile() メソッドを使用してドキュメントを保存します。

コードの例
C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace SetParagraphBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {
            // Documentクラスのオブジェクトを作成します
            Document document = new Document();

            // Wordドキュメントをロードします
            document.LoadFromFile("サンプル.docx");

            // ドキュメントの最初のセクションを取得します
            Section section = document.Sections[0];

            // セクション内の4番目の段落を取得します
            Paragraph paragraph = section.Paragraphs[3];

            // この段落の背景色をライトグレーに設定します
            paragraph.Format.BackColor = Color.LightGray;

            // ドキュメントを保存します
            document.SaveToFile("段落の背景色.docx", FileFormat.Docx_2013);

            // リソースを解放します
            document.Dispose();

            Console.WriteLine("段落の背景色の設定が完了しました");
            Console.ReadLine();
        }
    }
}

VB.NET

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

Namespace SetParagraphBackgroundColor
    Class Program
        Shared Sub Main(ByVal args() As String)
            ' Documentクラスのオブジェクトを作成します
            Dim document As New Document()

            ' Wordドキュメントをロードします
            document.LoadFromFile("サンプル.docx")

            ' ドキュメントの最初のセクションを取得します
            Dim section As Section = document.Sections(0)

            ' セクション内の4番目の段落を取得します
            Dim paragraph As Paragraph = section.Paragraphs(3)

            ' この段落の背景色をライトグレーに設定します
            paragraph.Format.BackColor = Color.LightGray

            ' ドキュメントを保存します
            document.SaveToFile("段落の背景色.docx", FileFormat.Docx_2013)

            ' リソースを解放します
            document.Dispose()

            Console.WriteLine("段落の背景色の設定が完了しました")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

Wordドキュメントの段落に背景色を設定する

Wordドキュメントのテキストに背景色を設定する

Free SpireDoc for Javaは、Word文書内の特定のテキストの出現箇所をすべて検索する Document.FindAllString() メソッドと、特定のテキストの背景を設定する TextRange.CharacterFormat.TextBackgroundColor プロパティを提供します。 既存のテキストの背景色を設定する詳しい手順は以下の通りです。

  • Document クラスのオブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.FindAllString() メソッドを使用して「砂糖摂取量」を検索し、すべての結果を取得します。
  • すべての結果をループします。
  • TextSelection.GetAsOneRange() メソッドを使用して、検索結果をテキスト範囲として取得します。
  • TextRange.CharacterFormat.TextBackgroundColor プロパティを使用して、テキスト範囲の背景色を設定します。
  • Document.SaveToFile() メソッドを使用してドキュメントを保存します。

コードの例
C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace SetTextBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {
            // Documentクラスのオブジェクトを作成します
            Document document = new Document();

            // Wordドキュメントをロードします
            document.LoadFromFile("サンプル.docx");

            // 背景色を設定するテキストを検索します
            TextSelection[] textSelections = document.FindAllString("糖分摂取", false, true);

            // 検索結果をループ処理します
            foreach (TextSelection selection in textSelections)
            {
                // テキスト範囲を取得します
                TextRange textRange = selection.GetAsOneRange();

                // 検索結果の背景色を設定します
                textRange.CharacterFormat.TextBackgroundColor = Color.Cyan;
            }

            // ドキュメントを保存します
            document.SaveToFile("テキストの背景色.docx", FileFormat.Docx_2013);

            // リソースを解放します
            document.Dispose();

            Console.WriteLine("テキストの背景色の設定が完了しました");
            Console.ReadLine();
        }
    }
}

VB.NET

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

Namespace SetTextBackgroundColor
    Class Program
        Shared Sub Main(ByVal args() As String)
            ' Documentクラスのオブジェクトを作成します
            Dim document As New Document()

            ' Wordドキュメントをロードします
            document.LoadFromFile("サンプル.docx")

            ' 背景色を設定するテキストを検索します
            Dim textSelections As TextSelection() = document.FindAllString("糖分摂取", False, True)

            ' 検索結果をループ処理します
            For Each selection As TextSelection In textSelections
                ' テキスト範囲を取得します
                Dim textRange As TextRange = selection.GetAsOneRange()

                ' 検索結果の背景色を設定します
                textRange.CharacterFormat.TextBackgroundColor = Color.Cyan
            Next

            ' ドキュメントを保存します
            document.SaveToFile("テキストの背景色.docx", FileFormat.Docx_2013)

            ' リソースを解放します
            document.Dispose()

            Console.WriteLine("テキストの背景色の設定が完了しました")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

Wordドキュメントのテキストに背景色を設定する

この記事では、Word文書で段落やテキストの背景色を設定する方法を紹介します。Free Spire.Doc for Javaには他にも多くのWord文書処理機能があります。詳しくはSpire.Doc for Javaチュートリアルをご覧下さい。このAPIを使用して何か問題がある場合、または他の人と議論したい場合は、Spire.Docフォーラムにアクセスしてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0