最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#代碼實(shí)現(xiàn)在Word文檔頁(yè)面中添加裝訂線

 更新時(shí)間:2026年01月22日 08:16:01   作者:2501_93070778  
在 Word 文檔頁(yè)面中添加裝訂線,可以有效提升文檔的專業(yè)性和整體美觀度,本文將介紹如何在 C# 項(xiàng)目中使用 Spire.Doc for .NET 為 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)文章

最新評(píng)論

镇沅| 正定县| 德兴市| 比如县| 鄂托克旗| 沁水县| 张家界市| 惠来县| 鄂伦春自治旗| 平度市| 宁武县| 沛县| 屯留县| 红河县| 潜山县| 惠来县| 手游| 桦川县| 独山县| 沂源县| 东安县| 海晏县| 苏尼特左旗| 仙桃市| 江川县| 柳州市| 罗田县| 承德县| 木里| 新丰县| 宜黄县| 英吉沙县| 肃宁县| 壤塘县| 宜州市| 和林格尔县| 绥中县| 兴仁县| 平顶山市| 万州区| 吉首市|