文檔首頁>>E-iceblue中文文檔>>在 word 文檔中的字符或句子周圍應(yīng)用邊框
在 word 文檔中的字符或句子周圍應(yīng)用邊框
為了強(qiáng)調(diào)和美化一組字符或句子,在字符或句子周圍應(yīng)用邊框是一個(gè)不錯(cuò)的選擇。Spire.Doc 使開發(fā)人員能夠在 C# 中實(shí)現(xiàn)此功能。并且有很多內(nèi)置的邊框樣式可用,例如:Wave、Hairline、DotDash、DashSmallGap、DashLargeGap、DoubleWave、DashDotStroker、Emboss3D、Engrave3D、TwistedLines1 等等。下面的代碼展示了如何使用上面提到的一些邊框樣式來實(shí)現(xiàn)字符邊框:
注意:開始之前,請(qǐng)確保 Visual Studio 和 Spire.Doc 已正確安裝。我們將使用 Spire.Doc .dll 作為參考。
第一步:加載word文檔
Document doc = new Document(); Section section = doc.AddSection();
第 2 步:添加應(yīng)用邊框所需的字符并設(shè)置邊框樣式
//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 步:保存并啟動(dòng) 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"); } } }