背景颜色或图片可以使文档更加美观和引人注目。如果您正在创建一个用于营销、教育或演示的文档,添加一个具有吸引力的背景色或图片将非常有用。在本文中,我们将演示如何使用 Spire.Doc for C++ 以编程方式给 Word 文档添加背景色或背景图。
安装 Spire.Doc for C++
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
给 Word 添加背景色
使用 Spire.Doc for C++,向 Word 文档添加背景颜色非常简单。您只需要将文档的背景类型设置为颜色,然后指定一种颜色作为背景即可。详细步骤如下。
- 初始化 Document 类的一个实例。
- 使用 Document->LoadFromFile() 方法加载 Word 文档。
- 使用 Document->GetBackground() 方法获取文档的背景。
- 使用 Background->SetType(BackgroundType::Color) 方法将背景类型设置为颜色。
- 使用 Background->SetColor() 方法设置背景颜色。
- 使用 Document->SaveToFile() 方法保存结果文档。
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace Spire::Common;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//将背景类型设置为颜色
background->SetType(BackgroundType::Color);
//设置背景颜色
background->SetColor(Color::GetAliceBlue());
//保存结果文档
document->SaveToFile(L"添加背景颜色.docx", FileFormat::Docx2013);
document->Close();
}
给 Word 添加渐变背景
要添加渐变背景,您需要将背景类型设置为渐变,指定渐变颜色,然后设置渐变着色变体和样式。详细步骤如下。
- 初始化 Document 类的一个实例。
- 使用 Document->LoadFromFile() 方法加载 Word 文档。
- 使用 Document->GetBackground() 方法获取文档的背景。
- 使用 Background->SetType(BackgroundType::Gradient) 方法将背景类型设置为渐变。
- 使用 Background->GetGradient()->SetColor1() 和 Background->GetGradient()->SetColor2() 方法指定两种渐变颜色。
- 使用 Background->GetGradient()->SetShadingVariant() 和 Background->GetGradient()->SetShadingStyle() 方法设置渐变着色变体和样式。
- 使用 Document->SaveToFile() 方法保存结果文档。
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace Spire::Common;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//设置背景类型为渐变
background->SetType(BackgroundType::Gradient);
//指定两种渐变颜色
background->GetGradient()->SetColor1(Color::GetWhite());
background->GetGradient()->SetColor2(Color::GetLightBlue());
//设置渐变着色变体和样式
background->GetGradient()->SetShadingVariant(GradientShadingVariant::ShadingDown);
background->GetGradient()->SetShadingStyle(GradientShadingStyle::Horizontal);
//保存结果文档
document->SaveToFile(L"添加渐变背景.docx", FileFormat::Docx2013);
document->Close();
}
给 Word 添加背景图片
为 Word 文档添加背景图片,需要设置背景类型为图片,然后插入图片作为背景。详细步骤如下。
- 初始化 Document 类的一个实例。
- 使用 Document->LoadFromFile() 方法加载 Word 文档。
- 使用 Document->GetBackground() 方法获取文档的背景。
- 使用 Background->SetType(BackgroundType::Picture) 方法将背景类型设置为图片。
- 使用 Background->SetPicture() 方法设置背景图片。
- 使用 Document->SaveToFile() 方法保存结果文档。
- C++
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//设置背景类型为图片
background->SetType(BackgroundType::Picture);
//设置背景图片
background->SetPicture(L"背景图.jpeg");
//保存结果文档
document->SaveToFile(L"添加背景图.docx", FileFormat::Docx2013);
document->Close();
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。