Word控件Spire.Doc 【页面背景】教程(6) ;如何设置页面边框的位置和自定义样式

MS 页面边框选项提供了通过从文本或页面边缘测量的边距设置页面边框位置的选项。此外,它还提供了自定义页面边框的选项,通过设置上、下、左、右边框的样式和颜色。我们已经介绍了设置页面边框的方法,并使用Spire.Doc设置页面边框是否围绕页眉/页脚。本文将介绍Spire.Doc提供的在C#中设置页面边框的位置和自定义样式的解决方案。

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

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

第 1 步:加载没有页面边框的示例文档。

Document document = new Document();
document.LoadFromFile("S.docx");
Section section = document.Sections[0];

第 2 步:设置页面边框的位置及其间距。效果将显示从文本测量的页面边框和具有相同间距的页面边缘之间的差异。

section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
section.PageSetup.Borders.Top.Space = 20;
section.PageSetup.Borders.Bottom.Space = 30;
section.PageSetup.Borders.Left.Space = 20;
section.PageSetup.Borders.Right.Space =25;

第 3 步:设置页面上、下、左、右边框的样式和颜色,即自定义页面边框。

section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

section.PageSetup.Borders.Top.Color = Color.YellowGreen;
section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

第 4 步:保存文档并启动以查看效果。

document.SaveToFile("result.docx", FileFormat.Docx2013);
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 Mirror_Margin
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("S.docx");
Section section = document.Sections[0];

section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
section.PageSetup.Borders.Top.Space = 20;
section.PageSetup.Borders.Bottom.Space = 30;
section.PageSetup.Borders.Left.Space = 20;
section.PageSetup.Borders.Right.Space =25;

section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

section.PageSetup.Borders.Top.Color = Color.YellowGreen;
section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值