• Word控件Spire.Doc 【文本】教程(10) ;在 word 文档中的字符或句子周围应用边框


    为了强调和美化一组字符或句子,在字符或句子周围应用边框是一个不错的选择。Spire.Doc 使开发人员能够在 C# 中实现此功能。并且有很多内置的边框样式可用,例如:Wave、Hairline、DotDash、DashSmallGap、DashLargeGap、DoubleWave、DashDotStroker、Emboss3D、Engrave3D、TwistedLines1 等等。下面的代码展示了如何使用上面提到的一些边框样式来实现字符边框: 

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

    注意:开始之前,请确保 Visual Studio 和 Spire.Doc 已正确安装。我们将使用 Spire.Doc .dll 作为参考。

    第一步:加载word文档

    Document doc = new Document();
    Section section = doc.AddSection();

    第 2 步:添加应用边框所需的字符并设置边框样式

    //DashSmallGap Border
    Paragraph para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    TextRange tr = para.AppendText("Spire.Doc for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap;
    tr.CharacterFormat.Border.Color = Color.Green;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.DarkKhaki;
    para.AppendBreak(BreakType.LineBreak);
    
    //Wave Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.PDF for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
    tr.CharacterFormat.Border.Color = Color.Aqua;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.BurlyWood;
    para.AppendBreak(BreakType.LineBreak);
    
    //Emboss3D Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.XLS for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D;
    tr.CharacterFormat.FontSize = 24;
    para.AppendBreak(BreakType.LineBreak);
    
    //DashDotStroker Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.Office for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker;
    tr.CharacterFormat.Border.Color = Color.Olive;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.Olive;
    para.AppendBreak(BreakType.LineBreak);
    
    //DoubleWave Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.Presentation for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave;
    tr.CharacterFormat.Border.Color = Color.Blue;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.Blue;
    para.AppendBreak(BreakType.LineBreak);
    

    第 3 步:保存并启动 word 文档

    doc.SaveToFile("S1.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start("S1.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 ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document doc = new Document();
    Section section = doc.AddSection();
    
    //DashSmallGap Border
    Paragraph para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    TextRange tr = para.AppendText("Spire.Doc for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashSmallGap;
    tr.CharacterFormat.Border.Color = Color.Green;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.DarkKhaki;
    para.AppendBreak(BreakType.LineBreak);
    
    //Wave Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.PDF for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Wave;
    tr.CharacterFormat.Border.Color = Color.Aqua;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.BurlyWood;
    para.AppendBreak(BreakType.LineBreak);
    
    //Emboss3D Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.XLS for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.Emboss3D;
    tr.CharacterFormat.FontSize = 24;
    para.AppendBreak(BreakType.LineBreak);
    
    //DashDotStroker Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.Office for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DashDotStroker;
    tr.CharacterFormat.Border.Color = Color.Olive;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.Olive;
    para.AppendBreak(BreakType.LineBreak);
    
    //DoubleWave Border
    para = section.AddParagraph();
    para.Format.HorizontalAlignment = HorizontalAlignment.Left;
    tr = para.AppendText("Spire.Presentation for .Net");
    tr.CharacterFormat.Border.BorderType = Spire.Doc.Documents.BorderStyle.DoubleWave;
    tr.CharacterFormat.Border.Color = Color.Blue;
    tr.CharacterFormat.FontSize = 24;
    tr.CharacterFormat.TextColor = Color.Blue;
    para.AppendBreak(BreakType.LineBreak);
    
    doc.SaveToFile("S1.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start("S1.docx");
    
    }
    }
    }

     如有产品相关需求,欢迎私聊我或者搜索[慧都]前往下载~

  • 相关阅读:
    时序预测 | Pytorch实现TCN-Transformer的时间序列预测
    计算机网络 - 概述 选择填空复习题
    Docker化Spring Boot应用
    jQuery---如何区分DOM对象和jQuery对象
    js异步编程面试题你能答上来几道
    代码随想录算法训练营第1天|二分查找|移除元素
    做个男人,做个成熟的男人
    vue 中 style 标签中的 scoped 属性(作用域)和 lang 属性的介绍
    Linux常用命令
    Ubuntu20.4安装QT6
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/127647626