Wordは、私たちの日常生活における学習や仕事に不可欠な文書処理ツールです。 絶妙で美しい文書は、読むときに人々に視覚的な美学を与えることができる。 この記事では、無料のSpire.Doc for .NET(Community Edition)コンポーネントを使用してWordのドキュメントの背景を設定する方法を学習します。 次の例では、Wordに背景を追加することは3つの状況に分けられます。

  • 無垢の背景
  • グラデーションの背景
  • 画像の背景

 

ツールの使用方法

Spire.Docコントロールをダウンロードしてインストールした後、Spire.Doc.dllをプロジェクトプログラムに追加します(このDLLはインストールファイルのBinフォルダにあります)

1. 単色の背景色を追加する

C#

using Spire.Doc;

using System.Drawing;

 

namespace AddBackground

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create an object of Document class and load file from system            

            Document document = new Document();

            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

 

            //set the fill type of background as Color           

            document.Background.Type = Spire.Doc.Documents.BackgroundType.Color;

 

            //choose the background color

            document.Background.Color = Color.MistyRose;

 

            //save and open the document

            document.SaveToFile("PureBackground.docx", FileFormat.Docx2013);

            System.Diagnostics.Process.Start("PureBackground.docx");

        }

    }

}

実行中のプログラムをデバッグした後、ドキュメントを生成する

 

2. グラデーションの背景色を追加する

C#

using Spire.Doc;

using System.Drawing;

using Spire.Doc.Documents;

 

namespace AddGradientBackground

{

    class Program

    {

        static void Main(string[] args)

        {

            //Initialize an instance of Document class and load file            

            Document document = new Document();

            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

 

            //set the fill type of background as Gradient

            document.Background.Type = Spire.Doc.Documents.BackgroundType.Gradient;

 

            //choose the color types

            BackgroundGradient gradient = document.Background.Gradient;

            gradient.Color1 = Color.LightSkyBlue;

            gradient.Color2 = Color.PaleGreen;

 

            //apply the type of gradient

            gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle;

            gradient.ShadingStyle = GradientShadingStyle.FromCenter;

 

            //save and open the document

            document.SaveToFile("GradientColor.docx", FileFormat.Docx2013);

            System.Diagnostics.Process.Start("GradientColor.docx");

        }

    }

}

3. 画像の背景色を追加する

C#

using System.Drawing;

using Spire.Doc;

 

namespace ImageBackground

{

    class Program

    {

        static void Main(string[] args)

        {

            //create an object of Document class and load file

            Document document = new Document();

            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

 

            //set the type of background as Picture

            document.Background.Type = Spire.Doc.Documents.BackgroundType.Picture;

 

            //load picture and set as background

            document.Background.Picture = Image.FromFile(@"C:\Users\Administrator\Desktop\1.jpg");

 

            //save and open the document

            document.SaveToFile("ImageBackground.docx", FileFormat.Docx2013);

            System.Diagnostics.Process.Start("ImageBackground.docx");

        }

    }

}

上記のすべては、Word文書の背景を追加する3つの方法です。この記事が好きな場合は、転載を歓迎します(再現、ソースを明記してください)