-
C#: 向Word插入排版精良的文本框
Text Box(文本框)是Word排版的工具之一。在Word文档正文的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性。我们可以设置文本框的大小,线型,内部边距,背景填充等效果。文本框内可以图文混排,设置字体,字号,图片大小等。 在日常使用中,我们很容易忽略这些元素,仅仅插入一个黑色单线,仅含文字的文本框。因而,我觉得有必要向大家介绍并制作一个版式精良的文本框,抛砖引玉。
本篇博文主要介绍,如何使用C#在Word文档的特定位置,插入一个有图片填充,内部边距,图文混排,线型精致的文本框。感兴趣的博友请从E-iceblue下载Free Spire.Doc,并添加为Visual Studio引用。
需要用的命名空间:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;
步骤详解:
步骤一:加载一个只含有文本的Word文档,如下图。
Document document = new Document();
document.LoadFromFile("李白生平.docx");
document.LoadFromFile("李白生平.docx");
步骤二:在加载的Word文档中添加一个文本框,并设定其具体位置。这里需要考虑两点:插入的页和页面的位置。即:在哪一页插入这个文本框,文本框在该页的位置。只有定位好这两点,文本框的位置才能具体确认。此外,还需考虑文本框和文本的位置关系,即设置位置和自动换行(text wrapping)。所以,以下代码,通过设定文本框在哪一段落,相较于页面的位置和自动换行,来确定其位置。
TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300);
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = 370;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 155;
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
TB.Format.TextWrappingType = TextWrappingType.Both;
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = 370;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 155;
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
TB.Format.TextWrappingType = TextWrappingType.Both;
步骤三:设置文本框框的颜色,内部边距,图片填充。
TB.Format.LineStyle = TextBoxLineStyle.Double;
TB.Format.LineColor = Color.LightGoldenrodYellow;
TB.Format.LineDashing = LineDashing.Solid;
TB.Format.LineWidth = 3;
TB.Format.InternalMargin.Top = 12;
TB.Format.InternalMargin.Bottom = 8;
TB.Format.InternalMargin.Left = 12;
TB.Format.InternalMargin.Right = 12;
TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
TB.Format.LineColor = Color.LightGoldenrodYellow;
TB.Format.LineDashing = LineDashing.Solid;
TB.Format.LineWidth = 3;
TB.Format.InternalMargin.Top = 12;
TB.Format.InternalMargin.Bottom = 8;
TB.Format.InternalMargin.Left = 12;
TB.Format.InternalMargin.Right = 12;
TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
步骤四:在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。
Paragraph para1 = TB.Body.AddParagraph();
para1.Format.AfterSpacing = 6;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = 16;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = 120;
picture.Height = 160;
para2.Format.AfterSpacing = 8;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = 11;
para3.Format.LineSpacing = 15;
para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
para3.Format.SuppressAutoHyphens = true;
document.SaveToFile("R1.docx");
System.Diagnostics.Process.Start("R1.docx");
para1.Format.AfterSpacing = 6;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = 16;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = 120;
picture.Height = 160;
para2.Format.AfterSpacing = 8;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = 11;
para3.Format.LineSpacing = 15;
para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
para3.Format.SuppressAutoHyphens = true;
document.SaveToFile("R1.docx");
System.Diagnostics.Process.Start("R1.docx");
效果图:
完整代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;
namespace textbox
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("李白生平.docx");
TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300);
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = 370;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 155;
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
TB.Format.TextWrappingType = TextWrappingType.Both;
TB.Format.LineStyle = TextBoxLineStyle.Double;
TB.Format.LineColor = Color.LightGoldenrodYellow;
TB.Format.LineDashing = LineDashing.Solid;
TB.Format.LineWidth = 3;
TB.Format.InternalMargin.Top = 12;
TB.Format.InternalMargin.Bottom = 8;
TB.Format.InternalMargin.Left = 12;
TB.Format.InternalMargin.Right = 12;
TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
Paragraph para1 = TB.Body.AddParagraph();
para1.Format.AfterSpacing = 6;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = 16;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = 120;
picture.Height = 160;
para2.Format.AfterSpacing = 8;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = 11;
para3.Format.LineSpacing = 15;
para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
para3.Format.SuppressAutoHyphens = true;
document.SaveToFile("R1.docx");
System.Diagnostics.Process.Start("R1.docx");
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;
namespace textbox
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("李白生平.docx");
TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300);
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = 370;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 155;
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
TB.Format.TextWrappingType = TextWrappingType.Both;
TB.Format.LineStyle = TextBoxLineStyle.Double;
TB.Format.LineColor = Color.LightGoldenrodYellow;
TB.Format.LineDashing = LineDashing.Solid;
TB.Format.LineWidth = 3;
TB.Format.InternalMargin.Top = 12;
TB.Format.InternalMargin.Bottom = 8;
TB.Format.InternalMargin.Left = 12;
TB.Format.InternalMargin.Right = 12;
TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
Paragraph para1 = TB.Body.AddParagraph();
para1.Format.AfterSpacing = 6;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = 16;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = 120;
picture.Height = 160;
para2.Format.AfterSpacing = 8;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = 11;
para3.Format.LineSpacing = 15;
para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
para3.Format.SuppressAutoHyphens = true;
document.SaveToFile("R1.docx");
System.Diagnostics.Process.Start("R1.docx");
}
}
}
出处:https://www.cnblogs.com/Yesi/p/4894166.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数