当前位置:网站首页>C#/VB. Net to set solid color / gradient / picture background in word

C#/VB. Net to set solid color / gradient / picture background in word

2022-06-09 03:13:00 InfoQ

In daily work and study , We often use Word Documents to help us complete the work . However, the simple and generous background can make the document look more delicate and beautiful , So as to achieve a different visual effect . This article will be divided into three cases to introduce Word Add background , A solid background is added 、 Gradient background and picture background . attach C#/VB.NET Code , There is a need for reference .
Program environment
In this test , Introduce... Into the program
Free Spire.Docfor .NET
. Can be referenced by  Spire.Doc.dll file :
Method 1
: take Free Spire.Doc for .NET Download to local , decompression , install . After installation , Find the installation path BIN In folder  Spire.Doc.dll. And then in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ Add reference ”, The local path BIN Under folder dll Add a reference to the program .
Method 2:
adopt
NuGet
install . This can be done by 2 Methods of installation :
(1) Can be in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ management NuGet package ”, And then the search “Free Spire.Doc”, Click on “ install ”. Wait for the program installation to complete .
(2) Copy the following to PM Console installation .
Install-Package FreeSpire.Doc -Version 10.2.0
One 、 Set a solid background
Specific steps and codes
  •    Create a Document Class object , Concurrent loading Word file .
  •    adopt Document.Background.Type Property to set the background fill mode of the document to color fill .
  •   adopt Document.Background.Color Property to set the background color .
  •    use Document.SaveToFile() Method to save a document .
【C#】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Spire.Doc;
using System.Drawing;

namespace AddBackground
{
 class Program
 {
 static void Main(string[] args)
 {
 // Create a Document Class object , And load Word file
 Document document = new Document();
 document.LoadFromFile(@"NewSample1.docx");

 // Set the background fill mode of the document to color fill
 document.Background.Type = Spire.Doc.Documents.BackgroundType.Color;

 // Set 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");
 }
 }
}
【VB.NET】
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports Spire.Doc
Imports System.Drawing

Namespace AddBackground
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
  Create a Document Class object , And load Word file  
 Dim document As Document = New Document
 document.LoadFromFile("NewSample1.docx")
 ' Set the background fill mode of the document to color fill
 document.Background.Type = Spire.Doc.Documents.BackgroundType.Color
 ' Set 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")
 End Sub
 End Class
End Namespace
design sketch
null
Two 、 Set gradient background
Specific steps and codes
  •   Create a new instance document and use
    Document.LoadFromFile() Method to load the instance document .
  • adopt Document.Background.Type Property to set the gradient background type .
  • adopt BackgroundGradient.Color1 Property to set the first gradient color .
  • adopt BackgroundGradient.Color2 Property to set the second gradient color .
  • adopt BackgroundGradient.ShadingVariant Property to set the shadow style and gradient effect
  •   use
    Document.SaveToFile() Method to save a document .
[C#]
using System.Drawing;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;

namespace SetGradientBackground
{
 public partial class Form1 : Form
 {
 public Form1()
 {
 InitializeComponent();
 }

 private void button1_Click(object sender, EventArgs e)
 {
 // establish Word file .
 Document document = new Document();

 // Load instance document .
 document.LoadFromFile("NewSample1.docx");

 // Set the gradient background type .
 document.Background.Type = BackgroundType.Gradient;
 BackgroundGradient Test = document.Background.Gradient;

 // Set gradient color .
 Test.Color1 = Color.White;
 Test.Color2 = Color.LightBlue;

 //  Set shadow style and gradient effect .
 Test.ShadingVariant = GradientShadingVariant.ShadingDown;
 Test.ShadingStyle = GradientShadingStyle.Horizontal;

 String result = "Result-SetGradientBackground.docx";

 // Save the document .
 document.SaveToFile(result, FileFormat.Docx2013);
 }
 }
}

[VB.NET]
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace SetGradientBackground
 
 Public Class Form1
 Inherits Form
 
 Public Sub New()
 MyBase.New
 InitializeComponent
 End Sub
 
 Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
 ' establish Word file .
 Dim document As Document = New Document
 ' Load instance document .
 document.LoadFromFile("..\..\..\..\..\..\Data\Template_Docx_2.docx")
 ' Set the gradient background type .
 document.Background.Type = BackgroundType.Gradient
 Dim Test As BackgroundGradient = document.Background.Gradient
 ' Set the color 1 For the background , Color 2 For gradient .
 Test.Color1 = Color.White
 Test.Color2 = Color.LightBlue
 '  Set shadow style and gradient effect .
 Test.ShadingVariant = GradientShadingVariant.ShadingDown
 Test.ShadingStyle = GradientShadingStyle.Horizontal
 Dim result As String = "Result-SetGradientBackground.docx"
 ' Save the document .
 document.SaveToFile(result, FileFormat.Docx2013)
 End Sub
 End Class
End Namespace

design sketch
null
3、 ... and 、 Set picture background
Specific steps and codes
  •    Create and load sample documents
  •    adopt Document.Background.Type Property to set the background picture type
  •   adopt Document.Background.Picture Property to set the background picture
  •    use Document.SaveToFile() Method to save a document

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

namespace SetImageBackground
{
 class Program
 {
 static void Main(string[] args)
 {
 // Load the document
 Document document = new Document("NewSample1.docx");

 // Set the background picture type .
 document.Background.Type = BackgroundType.Picture;

 // Set the background image
 document.Background.Picture = Image.FromFile("Background.png");

 // Save the document .
 document.SaveToFile("ImageBackground.docx", FileFormat.Docx);


 }
 }
}
[VB.NET]
Imports System.Drawing
Imports System.Text
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace setimagebackground
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
 ' load Word file
 Dim document As Document = New Document("Template.docx")
 ' Set the background picture type .
 document.Background.Type = BackgroundType.Picture
 ' Set the background image
 document.Background.Picture = Image.FromFile("Background.png")
 ' Save the document .
 document.SaveToFile("ImageBackground.docx", FileFormat.Docx)
 End Sub
 End Class
End Namespace
design sketch
null
matters needing attention :
 
The document path generated in the code is VS programmatic Debug route , File paths can also be customized to other paths .



原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081033161956.html