LoginSignup
0
0

More than 5 years have passed since last update.

C#Word文書の背景色と背景画像を設定する

Last updated at Posted at 2018-09-07

Spire.Docにより、開発者は簡単に背景色を設定し、背景画像をWord文書に追加できます。 次の例では、Spire.Doc for .NETを使用して単色の背景色、グラデーションの背景色を設定し、既存のWord文書に背景画像を追加する方法を詳しく説明します。
1.単色の背景色を設定する
【C#】

Program.cs

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;

namespace PureColor_Background_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //ドキュメントインスタンスを作成する
            Document document = new Document();
            //Word文書をロードする
            document.LoadFromFile("テスト.docx");
            //ドキュメントの背景塗りつぶしモードをカラー塗りつぶしに設定する
            document.Background.Type = BackgroundType.Color;
            //背景色を設定する
            document.Background.Color = Color.Beige;
            //ドキュメントを保存
            document.SaveToFile("単色の背景色.docx", FileFormat.Docx2013);

        }
    }
}

1.jpg

  1. グラデーションの背景色を設定する 【C#】
Program.cs

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;

namespace GradientColor_Background_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //ドキュメントインスタンスを作成する
            Document document = new Document();
            //Word文書をロードする
            document.LoadFromFile("テスト.docx");
            //ドキュメントの背景塗りつぶしモードをグラデーション塗りつぶしに設定する
            document.Background.Type = BackgroundType.Gradient;
            //グラデーションの色を設定する
            BackgroundGradient gradient = document.Background.Gradient;
            gradient.Color1 = Color.White;
            gradient.Color2 = Color.MediumSeaGreen;
            //グラデーションモードを設定する
            gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle;
            gradient.ShadingStyle = GradientShadingStyle.Horizontal;
            //保存文档
            document.SaveToFile("グラデーションの背景色.docx", FileFormat.Docx2013);
        }
    }
}

エフェクトチャートは以下の通りです:
2.jpg

3.背景画像を設定する
【C#】

Program.cs

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;

namespace Image_Background
{
    class Program
    {
        static void Main(string[] args)
        {
            //ドキュメントインスタンスを作成する
            Document document = new Document();
            //Word文書をロードする
            document.LoadFromFile("テスト.docx");
            //ドキュメントの背景塗りつぶしモードを設定して画像を塗りつぶす
            document.Background.Type = BackgroundType.Picture;
            //背景画像を設定する
            document.Background.Picture = Image.FromFile("背景.jpg");
            //ドキュメントを保存
            document.SaveToFile("画像の背景.docx", FileFormat.Docx2013);
        }
    }
}

エフェクトチャートは以下の通りです:
3.jpg

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