C#: 向Word插入排版精良的Text Box
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;
步骤详解:
Document document = new Document();
document.LoadFromFile("李白生平.docx");

TextBox TB = document.Sections[].Paragraphs[].AppendTextBox(, );
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = ;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = ; 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 = ; TB.Format.InternalMargin.Top = ;
TB.Format.InternalMargin.Bottom = ;
TB.Format.InternalMargin.Left = ;
TB.Format.InternalMargin.Right = ; TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
Paragraph para1 = TB.Body.AddParagraph();
para1.Format.AfterSpacing = ;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = ;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = ;
picture.Height = ;
para2.Format.AfterSpacing = ;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center; Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = ;
para3.Format.LineSpacing = ;
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[].Paragraphs[].AppendTextBox(, );
TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
TB.Format.HorizontalPosition = ;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = ; 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 = ; TB.Format.InternalMargin.Top = ;
TB.Format.InternalMargin.Bottom = ;
TB.Format.InternalMargin.Left = ;
TB.Format.InternalMargin.Right = ; TB.Format.FillEfects.Type = BackgroundType.Picture;
TB.Format.FillEfects.Picture = Image.FromFile("2.jpg"); Paragraph para1 = TB.Body.AddParagraph();
para1.Format.AfterSpacing = ;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange TR1 = para1.AppendText("李白");
TR1.CharacterFormat.FontName = "华文新魏";
TR1.CharacterFormat.FontSize = ;
TR1.CharacterFormat.Bold = true;
Paragraph para2 = TB.Body.AddParagraph();
Image image = Image.FromFile("李白.jpg");
DocPicture picture = para2.AppendPicture(image);
picture.Width = ;
picture.Height = ;
para2.Format.AfterSpacing = ;
para2.Format.HorizontalAlignment = HorizontalAlignment.Center; Paragraph para3 = TB.Body.AddParagraph();
TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
TR2.CharacterFormat.FontName = "华文新魏";
TR2.CharacterFormat.FontSize = ;
para3.Format.LineSpacing = ;
para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
para3.Format.SuppressAutoHyphens = true;
document.SaveToFile("R1.docx");
System.Diagnostics.Process.Start("R1.docx");
}
}
}
感谢阅读,欢迎评论交流。
C#: 向Word插入排版精良的Text Box的更多相关文章
- C#: 向Word插入排版精良的文本框
Text Box(文本框)是Word排版的工具之一.在Word文档正文的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性.我们可以设置文本框的大小,线型,内部边距,背景填充等 ...
- 2、word插入目录、图/表
一.word插入目录 依次对每个标题在“段落”中进行大纲级别选择. 光标定位于目录生成的页面,再“引用”->“目录”->选择“自动目录1/2”,则可自动生成目录.若目录有所更改,则可选择“ ...
- Word 插入目录详细教程 -- 视频教程(6)
>> 视频教程链接:B站,速度快,清晰 更多关于插入目录的方法,参看:Word插入目录系列 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)
- [UE4]Text Box
Text Box:文本输入控件. 一.新建一个名为testTextBox的UserWidget,添加一个名为“EditableTextBox_0”的TextBox到默认容器Canvas Panel 二 ...
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
由于NPOI暂时没找到书签内容替换功能,所以换用Apose.Word组件. using System; using System.Collections.Generic; using System.C ...
- openXML向Word插入表
表是 Word 中的另一类型的块级内容,它是以行和列排列的一组段落(以及其他块级内容). Word 中的表格通过 tbl 元素定义,该元素类似于 HTML <表格>标记. 表元素指定文档中 ...
- 怎么用MathType解决Word公式排版很乱的问题
现在办公室起草文件,期刊论文投稿.学校试着编辑都要先在Word中编辑好后再打印出来.在Word中编辑这些文本内容时,如果遇到公式就要使用专门的MathType公式编辑器.而有很多人在用MathType ...
- word插入图片显示不完整的解决的方法
有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.
- word插入行
如何在Word中添加多行或多列 在弹出的列表中选择[插入],再选择[在下方插入行]即可. 选择多少行就可添加多少行. 按F4重复上一操作可快速添加. 添加列也同样如此,选中一个单元格,右键单击,在弹出 ...
随机推荐
- css3 linear-gradient实现页面加载进度条效果
最终效果图: html结构: <div> <p class="p1"> <span></span> < ...
- 不需要sql进行计算数据的平均值、最大值、最小值、和
介绍下SqlServer.前端js.后台C#三个阶段对均值.最大值.最小值.和计算int[] jisuan = {0, 1, 3, 5, 7,8 }; List<int> jisuan2 ...
- C++类型转换函数
1.什么是类型转换函数 类型转换函数的作用是将一个类的对象转换成另一类型的数据. class testclass { private: int r; int m; public : testclass ...
- 升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错(引用他人的)
升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...
- linux内核分析作业6:分析Linux内核创建一个新进程的过程
task_struct结构: struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标识符 u ...
- linux查看发行版本
redhat系 cat /etc/redhat-release 其他 lsb_release -a
- 通过一组RESTful API暴露CQRS系统功能
命令和查询责任分离(CQRS)是由Greg Young提出的一种将系统的读(查询).写(命令)操作分离为两种独立子系统的架构模式.命令通常是异步执行的,并存储在一个事务型数据库中,而读操作则通常是最终 ...
- 性能计数器与profiler的组合性能诊断
性能计数器和sql profiler都是常用的性能诊断工具和优化工具,最近和群友聊天发现很多人竟然不知道这两个可以“组合”使用,所以这篇算是一篇扫盲贴吧. 两种工具简述 通过计数器可以收集两部分内容: ...
- 一个Java程序员的实习总结(2)
在今天的总结里,主要讲述第二.三周这半个月的培训情况,并且穿插讲讲我对实习和见习的看法,有需要有兴趣的童鞋可以看看. 半个月的见习 其实我更愿意把实习和见习分开讲,实习指的是还没签三方或者直接就是大三 ...
- C#编程模式之扩展命令
C#编程模式之扩展命令 前言 根据上一篇的命令模式和在工作中遇到的一些实际情况,有了本篇文章,时时都是学习的一个过程,会在这个过程中发现许多好的模式或者是一种开发方式,今天写出来的就是我工作中常用到的 ...