在 Word 文档页面中添加装订线,可以有效提升文档的专业性和整体美观度。装订线不仅能让文档看起来更加整洁、有条理,还能在打印时作为参考标记,方便读者快速浏览和查阅内容。通过在页面中设置装订线,可以模拟纸质文档中常见的装订边效果,使电子文档更具"印刷感"。本文将介绍如何在 C# 项目中使用 Spire.Doc for .NET 为 Word 文档页面添加装订线。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for .NET 软件包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过官方提供的下载链接获取,也可以直接通过 NuGet 进行安装。
cs
PM> Install-Package Spire.Doc
使用 C# 在 Word 文档页面顶部添加装订线
要在页面顶部启用装订线,可以将 section.PageSetup.IsTopGutter 设置为 true。默认情况下,装订线区域是空白的、不显示任何内容。本文示例还演示了如何在装订线区域中添加文本。
示例代码如下:
cs
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Drawing;
using System.Text;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 创建 Document 对象
Document document = new Document();
// 加载文档
document.LoadFromFile("Sample1.docx");
// 遍历文档中的所有节
for (int i = 0; i < document.Sections.Count; i++)
{
// 获取当前节
Section section = document.Sections[i];
// 设置在页面顶部启用装订线
section.PageSetup.IsTopGutter = true;
// 设置装订线宽度为 100f
section.PageSetup.Gutter = 100f;
// 调用方法,在顶部装订线区域添加文本
AddTopGutterText(section);
}
// 将修改后的文档保存为文件
document.SaveToFile("Add Gutter Line at the Top of the Page.docx", FileFormat.Docx2016);
// 释放文档资源
document.Dispose();
}
// 在顶部装订线区域添加文本的方法
static void AddTopGutterText(Section section)
{
// 获取当前节的页眉
HeaderFooter header = section.HeadersFooters.Header;
// 设置文本框宽度为页面宽度
float width = section.PageSetup.PageSize.Width;
// 设置文本框高度为 40
float height = 40;
// 在页眉中添加一个文本框
TextBox textBox = header.AddParagraph().AppendTextBox(width, height);
// 设置文本框无边框
textBox.Format.NoLine = true;
// 设置文本框的垂直起始位置为上边距区域
textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;
// 设置文本框的垂直位置
textBox.VerticalPosition = 140;
// 设置文本框的水平对齐方式为左对齐
textBox.HorizontalAlignment = ShapeHorizontalAlignment.Left;
// 设置文本框的水平起始位置为左边距区域
textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;
// 设置文本锚点为底部
textBox.Format.TextAnchor = ShapeVerticalAlignment.Bottom;
// 设置文字环绕方式为"浮于文字上方"
textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
// 设置文字环绕类型为两侧
textBox.Format.TextWrappingType = TextWrappingType.Both;
// 创建段落对象
Paragraph paragraph = new Paragraph(section.Document);
// 设置段落水平居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
// 创建字体对象
Font font = new Font("Times New Roman", 8);
// 创建绘图对象,用于测量文本宽度
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
string text1 = " - ";
SizeF size1 = graphics.MeasureString(text1, font);
float textWidth1 = size1.Width / 96 * 72;
int count = (int)(textBox.Width / textWidth1);
// 构建重复的文本字符串
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i < count; i++)
{
stringBuilder.Append(text1);
}
// 创建字符格式对象
CharacterFormat characterFormat = new CharacterFormat(section.Document);
characterFormat.FontName = font.Name;
characterFormat.FontSize = font.Size;
// 将文本添加到段落并应用字符格式
TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
textRange.ApplyCharacterFormat(characterFormat);
// 将段落添加到文本框中
textBox.ChildObjects.Add(paragraph);
}
}
}
使用 C# 在 Word 文档页面左侧添加装订线
要在页面左侧设置装订线,需要将 Section.PageSetup.IsTopGutter 属性设置为 false,以确保装订线显示在页面左侧。
示例代码如下:
cs
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Drawing;
using System.Text;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 创建 Document 对象
Document document = new Document();
// 加载文档
document.LoadFromFile("Sample1.docx");
// 遍历文档中的所有节
for (int i = 0; i < document.Sections.Count; i++)
{
// 获取当前节
Section section = document.Sections[i];
// 设置不在页面顶部添加装订线(装订线将显示在页面左侧)
section.PageSetup.IsTopGutter = false;
// 设置装订线宽度为 100f
section.PageSetup.Gutter = 100f;
// 调用方法,在左侧装订线区域添加文本
AddLeftGutterText(section);
}
// 将修改后的文档保存为文件
document.SaveToFile("Add Gutter Line on the Left Side of the Page.docx", FileFormat.Docx2016);
// 释放文档资源
document.Dispose();
}
// 在左侧装订线区域添加文本的方法
static void AddLeftGutterText(Section section)
{
// 获取当前节的页眉
HeaderFooter header = section.HeadersFooters.Header;
// 设置文本框宽度为 40
float width = 40;
// 获取页面高度
float height = section.PageSetup.PageSize.Height;
// 在页眉中添加一个文本框
TextBox textBox = header.AddParagraph().AppendTextBox(width, height);
// 设置文本框无边框
textBox.Format.NoLine = true;
// 设置文本框内文字方向为从右到左
textBox.Format.LayoutFlowAlt = TextDirection.RightToLeft;
// 设置文本框的水平起始位置
textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;
// 设置文本框的水平位置
textBox.HorizontalPosition = 140;
// 设置文本框垂直对齐方式为顶部对齐
textBox.VerticalAlignment = ShapeVerticalAlignment.Top;
// 设置文本框的垂直起始位置为上边距区域
textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;
// 设置文本锚点为顶部
textBox.Format.TextAnchor = ShapeVerticalAlignment.Top;
// 设置文字环绕方式为"浮于文字上方"
textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
// 设置文字环绕类型为两侧
textBox.Format.TextWrappingType = TextWrappingType.Both;
// 创建段落对象
Paragraph paragraph = new Paragraph(section.Document);
// 设置段落水平居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
// 创建字体对象
Font font = new Font("Times New Roman", 8);
// 创建绘图对象,用于测量文本尺寸
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
string text1 = " - ";
// 测量文本大小
SizeF size1 = graphics.MeasureString(text1, font);
float textWidth1 = size1.Width / 96 * 72;
int count = (int)(textBox.Height / textWidth1);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i < count; i++)
{
stringBuilder.Append(text1);
}
// 创建字符格式对象
CharacterFormat characterFormat = new CharacterFormat(section.Document);
characterFormat.FontName = font.Name;
characterFormat.FontSize = font.Size;
// 将文本添加到段落并应用字符格式
TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
textRange.ApplyCharacterFormat(characterFormat);
// 将段落添加到文本框中
textBox.ChildObjects.Add(paragraph);
}
}
}
申请临时许可证
如果您希望移除生成文档中的评估提示信息,或解除功能限制,请申请一份 有效期为 30 天的临时许可证。