C#代碼實(shí)現(xiàn)在Word文檔頁(yè)面中添加裝訂線
在 Word 文檔頁(yè)面中添加裝訂線,可以有效提升文檔的專業(yè)性和整體美觀度。裝訂線不僅能讓文檔看起來(lái)更加整潔、有條理,還能在打印時(shí)作為參考標(biāo)記,方便讀者快速瀏覽和查閱內(nèi)容。通過(guò)在頁(yè)面中設(shè)置裝訂線,可以模擬紙質(zhì)文檔中常見(jiàn)的裝訂邊效果,使電子文檔更具“印刷感”。本文將介紹如何在 C# 項(xiàng)目中使用 Spire.Doc for .NET 為 Word 文檔頁(yè)面添加裝訂線。
安裝 Spire.Doc for .NET
首先,您需要將 Spire.Doc for .NET 軟件包中包含的 DLL 文件添加為 .NET 項(xiàng)目的引用。這些 DLL 文件可以通過(guò)官方提供的下載鏈接獲取,也可以直接通過(guò) NuGet 進(jìn)行安裝。
PM> Install-Package Spire.Doc
使用 C# 在 Word 文檔頁(yè)面頂部添加裝訂線
要在頁(yè)面頂部啟用裝訂線,可以將 section.PageSetup.IsTopGutter 設(shè)置為 true。默認(rèn)情況下,裝訂線區(qū)域是空白的、不顯示任何內(nèi)容。本文示例還演示了如何在裝訂線區(qū)域中添加文本。
示例代碼如下:
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)
{
// 創(chuàng)建 Document 對(duì)象
Document document = new Document();
// 加載文檔
document.LoadFromFile("Sample1.docx");
// 遍歷文檔中的所有節(jié)
for (int i = 0; i < document.Sections.Count; i++)
{
// 獲取當(dāng)前節(jié)
Section section = document.Sections[i];
// 設(shè)置在頁(yè)面頂部啟用裝訂線
section.PageSetup.IsTopGutter = true;
// 設(shè)置裝訂線寬度為 100f
section.PageSetup.Gutter = 100f;
// 調(diào)用方法,在頂部裝訂線區(qū)域添加文本
AddTopGutterText(section);
}
// 將修改后的文檔保存為文件
document.SaveToFile("Add Gutter Line at the Top of the Page.docx", FileFormat.Docx2016);
// 釋放文檔資源
document.Dispose();
}
// 在頂部裝訂線區(qū)域添加文本的方法
static void AddTopGutterText(Section section)
{
// 獲取當(dāng)前節(jié)的頁(yè)眉
HeaderFooter header = section.HeadersFooters.Header;
// 設(shè)置文本框?qū)挾葹轫?yè)面寬度
float width = section.PageSetup.PageSize.Width;
// 設(shè)置文本框高度為 40
float height = 40;
// 在頁(yè)眉中添加一個(gè)文本框
TextBox textBox = header.AddParagraph().AppendTextBox(width, height);
// 設(shè)置文本框無(wú)邊框
textBox.Format.NoLine = true;
// 設(shè)置文本框的垂直起始位置為上邊距區(qū)域
textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;
// 設(shè)置文本框的垂直位置
textBox.VerticalPosition = 140;
// 設(shè)置文本框的水平對(duì)齊方式為左對(duì)齊
textBox.HorizontalAlignment = ShapeHorizontalAlignment.Left;
// 設(shè)置文本框的水平起始位置為左邊距區(qū)域
textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;
// 設(shè)置文本錨點(diǎn)為底部
textBox.Format.TextAnchor = ShapeVerticalAlignment.Bottom;
// 設(shè)置文字環(huán)繞方式為“浮于文字上方”
textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
// 設(shè)置文字環(huán)繞類型為兩側(cè)
textBox.Format.TextWrappingType = TextWrappingType.Both;
// 創(chuàng)建段落對(duì)象
Paragraph paragraph = new Paragraph(section.Document);
// 設(shè)置段落水平居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
// 創(chuàng)建字體對(duì)象
Font font = new Font("Times New Roman", 8);
// 創(chuàng)建繪圖對(duì)象,用于測(cè)量文本寬度
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);
// 構(gòu)建重復(fù)的文本字符串
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i < count; i++)
{
stringBuilder.Append(text1);
}
// 創(chuàng)建字符格式對(duì)象
CharacterFormat characterFormat = new CharacterFormat(section.Document);
characterFormat.FontName = font.Name;
characterFormat.FontSize = font.Size;
// 將文本添加到段落并應(yīng)用字符格式
TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
textRange.ApplyCharacterFormat(characterFormat);
// 將段落添加到文本框中
textBox.ChildObjects.Add(paragraph);
}
}
}使用 C# 在 Word 文檔頁(yè)面左側(cè)添加裝訂線
要在頁(yè)面左側(cè)設(shè)置裝訂線,需要將 Section.PageSetup.IsTopGutter 屬性設(shè)置為 false,以確保裝訂線顯示在頁(yè)面左側(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)
{
// 創(chuàng)建 Document 對(duì)象
Document document = new Document();
// 加載文檔
document.LoadFromFile("Sample1.docx");
// 遍歷文檔中的所有節(jié)
for (int i = 0; i < document.Sections.Count; i++)
{
// 獲取當(dāng)前節(jié)
Section section = document.Sections[i];
// 設(shè)置不在頁(yè)面頂部添加裝訂線(裝訂線將顯示在頁(yè)面左側(cè))
section.PageSetup.IsTopGutter = false;
// 設(shè)置裝訂線寬度為 100f
section.PageSetup.Gutter = 100f;
// 調(diào)用方法,在左側(cè)裝訂線區(qū)域添加文本
AddLeftGutterText(section);
}
// 將修改后的文檔保存為文件
document.SaveToFile("Add Gutter Line on the Left Side of the Page.docx", FileFormat.Docx2016);
// 釋放文檔資源
document.Dispose();
}
// 在左側(cè)裝訂線區(qū)域添加文本的方法
static void AddLeftGutterText(Section section)
{
// 獲取當(dāng)前節(jié)的頁(yè)眉
HeaderFooter header = section.HeadersFooters.Header;
// 設(shè)置文本框?qū)挾葹?40
float width = 40;
// 獲取頁(yè)面高度
float height = section.PageSetup.PageSize.Height;
// 在頁(yè)眉中添加一個(gè)文本框
TextBox textBox = header.AddParagraph().AppendTextBox(width, height);
// 設(shè)置文本框無(wú)邊框
textBox.Format.NoLine = true;
// 設(shè)置文本框內(nèi)文字方向?yàn)閺挠业阶?
textBox.Format.LayoutFlowAlt = TextDirection.RightToLeft;
// 設(shè)置文本框的水平起始位置
textBox.HorizontalOrigin = HorizontalOrigin.LeftMarginArea;
// 設(shè)置文本框的水平位置
textBox.HorizontalPosition = 140;
// 設(shè)置文本框垂直對(duì)齊方式為頂部對(duì)齊
textBox.VerticalAlignment = ShapeVerticalAlignment.Top;
// 設(shè)置文本框的垂直起始位置為上邊距區(qū)域
textBox.VerticalOrigin = VerticalOrigin.TopMarginArea;
// 設(shè)置文本錨點(diǎn)為頂部
textBox.Format.TextAnchor = ShapeVerticalAlignment.Top;
// 設(shè)置文字環(huán)繞方式為“浮于文字上方”
textBox.Format.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
// 設(shè)置文字環(huán)繞類型為兩側(cè)
textBox.Format.TextWrappingType = TextWrappingType.Both;
// 創(chuàng)建段落對(duì)象
Paragraph paragraph = new Paragraph(section.Document);
// 設(shè)置段落水平居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
// 創(chuàng)建字體對(duì)象
Font font = new Font("Times New Roman", 8);
// 創(chuàng)建繪圖對(duì)象,用于測(cè)量文本尺寸
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
string text1 = " - ";
// 測(cè)量文本大小
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);
}
// 創(chuàng)建字符格式對(duì)象
CharacterFormat characterFormat = new CharacterFormat(section.Document);
characterFormat.FontName = font.Name;
characterFormat.FontSize = font.Size;
// 將文本添加到段落并應(yīng)用字符格式
TextRange textRange = paragraph.AppendText(stringBuilder.ToString());
textRange.ApplyCharacterFormat(characterFormat);
// 將段落添加到文本框中
textBox.ChildObjects.Add(paragraph);
}
}
}到此這篇關(guān)于C#代碼實(shí)現(xiàn)在Word文檔頁(yè)面中添加裝訂線的文章就介紹到這了,更多相關(guān)C# Word添加裝訂線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)獲得某個(gè)枚舉的所有名稱
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)獲得某個(gè)枚舉的所有名稱,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2025-01-01
C#的鋸齒數(shù)組以及C++實(shí)現(xiàn)代碼
鋸齒數(shù)組首先是二維數(shù)組,第一維的維數(shù)是確定的。之所以在C#中能夠出現(xiàn)靈活的鋸齒數(shù)組,是因?yàn)?,C#的數(shù)組是引用類型(本質(zhì)上存放的是指針)。根據(jù)這個(gè)引用類型(指針)的概念,C++中用指針數(shù)組同樣可以實(shí)現(xiàn)2013-09-09
詳解Unity 實(shí)現(xiàn)語(yǔ)音識(shí)別功能
語(yǔ)言識(shí)別功能已經(jīng)在我們身邊普遍流行起來(lái),在unity開(kāi)發(fā)中語(yǔ)音識(shí)別也非?;馃幔裉炀徒榻B下Unity自帶的語(yǔ)音識(shí)別功能的實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2021-05-05
c# winform讀取xml文件創(chuàng)建菜單的代碼
動(dòng)態(tài)創(chuàng)建菜單使得程序靈活性大大增加,本文根據(jù)讀取xml文件中的配置菜單項(xiàng)來(lái)動(dòng)態(tài)創(chuàng)建菜單,代碼如下2013-09-09
C# Mutex實(shí)現(xiàn)線程與進(jìn)程同步的技術(shù)指南
在多線程或多進(jìn)程程序中,如果多個(gè)線程/程序同時(shí)訪問(wèn)同一資源,很容易導(dǎo)致數(shù)據(jù)沖突或程序異常,為了解決這種問(wèn)題,C# 提供了強(qiáng)大的同步原語(yǔ)Mutex,下面我們就來(lái)看看它的具體使用吧2025-11-11
C#使用MVC框架創(chuàng)建WebApi服務(wù)接口的流程步驟
WebAPI是一種基于HTTP協(xié)議的網(wǎng)絡(luò)應(yīng)用程序接口,它使用JSON或XML格式來(lái)傳輸數(shù)據(jù),本文通過(guò)圖文和代碼示例給大家介紹了C#使用MVC框架創(chuàng)建WebApi服務(wù)接口的流程步驟,需要的朋友可以參考下2025-01-01

