Word控件Spire.Doc 【页面背景】教程(5) ;如如何在 C# 中为 Word 设置渐变背景

渐变可用于 Word 背景以吸引眼球。在 MS Word 中,我们可以为渐变选择一种颜色或两种颜色,设置渐变的底纹样式和底纹变体。值得一提的是,Spire.Doc 支持在 C# 中使用上述所有选项设置渐变背景。本文将介绍使用 Spire.Doc 在 C# 中实现该功能的简单代码。

Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

注意:开始之前,请下载最新版本的Spire.Doc,并将Spire.Doc.dll添加到bin文件夹中作为Visual Studio的参考。

第 1 步:加载只有文本的示例文档。

Document doc = new Document("Sample.docx");

第 2 步:将背景类型设置为渐变。

doc.Background.Type = BackgroundType.Gradient;
BackgroundGradient Test =doc.Background.Gradient;

第 3 步:设置渐变的第一种颜色和第二种颜色。

Test.Color1 = Color.White;
Test.Color2 = Color.Green;

第 4 步:设置渐变的阴影样式和变体。

Test.ShadingVariant = GradientShadingVariant.ShadingDown;
Test.ShadingStyle = GradientShadingStyle.Horizontal;

第 5 步:保存文件并启动查看效果。

doc.SaveToFile("Result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Result.docx");

样品

效果

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document("Sample.docx");

doc.Background.Type = BackgroundType.Gradient;
BackgroundGradient Test =doc.Background.Gradient;
Test.Color1 = Color.White;
Test.Color2 = Color.Green;
Test.ShadingVariant = GradientShadingVariant.ShadingDown;
Test.ShadingStyle = GradientShadingStyle.Horizontal;

doc.SaveToFile("Result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Result.docx");

}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!感谢您的提问。针对您的问题,使用Spire.Doc实现复制doc指定页面数据存放到另一个word的大致步骤如下: 1. 加载原始Word文档,使用Document类的LoadFromFile方法可以实现。 ``` Document doc = new Document(); doc.LoadFromFile("Original.docx"); ``` 2. 获取原始Word文档指定页面的数据,可以通过Document类的Sections属性获取所有的Section对象,然后使用Section类的PageNumber属性获取指定页码的Section对象,最后使用Section类的ToText方法获取该页的文本数据。 ``` Section section = doc.Sections[1]; // 获取第一页的Section对象 string pageText = section.ToText(); // 获取第一页的文本数据 ``` 3. 创建新的Word文档,使用Document类的构造函数可以创建一个新的空白Word文档。 ``` Document newDoc = new Document(); ``` 4. 将指定页面的数据添加到新的Word文档中,可以通过Document类的AddSection方法添加一个新的Section对象,然后使用Section类的AddParagraph方法添加一个新的Paragraph对象,最后使用Paragraph类的AppendText方法添加文本数据。 ``` Section newSection = newDoc.AddSection(); Paragraph newParagraph = newSection.AddParagraph(); newParagraph.AppendText(pageText); ``` 5. 保存新的Word文档,使用Document类的SaveToFile方法可以将新的Word文档保存到指定的文件路径。 ``` newDoc.SaveToFile("New.docx", FileFormat.Docx); ``` 以上就是使用Spire.Doc实现复制doc指定页面数据存放到另一个word的大致步骤。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值