在 Word 文档页面上添加装订线可以提高文档的专业性和美观度。装订线不仅可以使文档看起来更加整洁和有条理,还可以在打印文档时起到指导作用,帮助读者更容易翻阅和浏览文档内容。通过在文档页面上添加装订线,可以模拟出纸质文档中常见的装订线效果,让文档更具印刷品质。本文将介绍如何使用 Spire.Doc for .NET 在 C# 项目中给 Word 文档页面添加装订线。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.DocC# 在 Word 文档页面的上端添加装订线
设置装订线在页面上端的关键设置是 Section.PageSetup.IsTopGutter = true。默认的装订线区域是显示空白没有内容,此示例也包含了在装订线区域如何添加文本。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 for 循环遍历文档的所有节集合 Document.Sections。
- 设置 Section.PageSetup.IsTopGutter 为 true,让装订线在页面上端显示。
- 使用 Section.PageSetup.Gutter 属性设置装订线的宽度。
- 调用自定义的 AddTopGutterText() 方法将文本添加到装订线区域。
- 使用 Document.SaveToFile() 方法保存到文档。
- C#
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 = new Document();
			// 加载文档
			document.LoadFromFile("示例1.docx");
			// 遍历文档的所有节
			for (int i = 0; i < document.Sections.Count; i++)
			{
				// 获取当前节
				Section section = document.Sections[i];
				// 设置是否在页面的上端添加装订线为true
				section.PageSetup.IsTopGutter = true;
				// 设置装订线的宽度为100f
				section.PageSetup.Gutter = 100f;
				// 调用方法,在左侧装订线上添加文本
				AddTopGutterText(section);
			}
			// 将修改后的文档保存为文件
			document.SaveToFile("在页面的上端添加装订线.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;
			// 创建字体对象,宋体,大小8
			Font font = new Font("宋体", 8);
			// 创建绘图对象
			Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
			string text1 = " - ";
			string text2 = "装订线";
			SizeF size1 = graphics.MeasureString(text1, font);
			SizeF size2 = graphics.MeasureString(text2, font);
			float textWidth1 = size1.Width / 96 * 72;
			float textWidth2 = size2.Width / 96 * 72;
			int count = (int)((textBox.Width - textWidth2) / textWidth1);
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 2; i < count / 2; 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);
			textRange = paragraph.AppendText(text2);
			textRange.ApplyCharacterFormat(characterFormat);
			textRange = paragraph.AppendText(stringBuilder.ToString());
			textRange.ApplyCharacterFormat(characterFormat);
			// 将段落添加到文本框中
			textBox.ChildObjects.Add(paragraph);
		}
	}
}
C# 在 Word 文档页面的左侧添加装订线
在页面左侧设置装订线的关键是将 Section.PageSetup.IsTopGutter 属性设置为 false。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载一个文档。
- 使用 for 循环遍历文档的所有节集合 Document.Sections。
- 设置 Section.PageSetup.IsTopGutter 为 false,让装订线在页面左侧显示。
- 使用 Section.PageSetup.Gutter 属性设置装订线的宽度。
- 调用自定义的AddLeftGutterText() 方法将文本添加到装订线区域。
- 使用Document.SaveToFile() 方法保存到文档。
- C#
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 = new Document();
            // 加载文档
            document.LoadFromFile("示例1.docx");
            // 遍历文档的所有节
            for (int i = 0; i < document.Sections.Count; i++)
            {
                // 获取当前节
                Section section = document.Sections[i];
                // 设置是否在页面的上端添加装订线为false, 则会添加到页面的左侧
                section.PageSetup.IsTopGutter = false;
                // 设置装订线的宽度为100f
                section.PageSetup.Gutter = 100f;
                // 调用方法,在左侧装订线上添加文本
                AddLeftGutterText(section);
            }
            // 将修改后的文档保存为文件
            document.SaveToFile("在页面的左侧添加装订线.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;
            // 创建字体对象,宋体,大小8
            Font font = new Font("宋体", 8);
            // 创建绘图对象
            Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
            string text1 = " - ";
            string text2 = "装订线";
            // 测量文本的大小
            SizeF size1 = graphics.MeasureString(text1, font);
            SizeF size2 = graphics.MeasureString(text2, font);
            float textWidth1 = size1.Width / 96 * 72;
            float textWidth2 = size2.Width / 96 * 72;
            int count = (int)((textBox.Height - textWidth2) / textWidth1);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 5; i < count / 2; 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);
            textRange = paragraph.AppendText(text2);
            textRange.ApplyCharacterFormat(characterFormat);
            textRange = paragraph.AppendText(stringBuilder.ToString());
            textRange.ApplyCharacterFormat(characterFormat);
            // 将段落添加到文本框中
            textBox.ChildObjects.Add(paragraph);
        }
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



