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;
步骤详解:
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插入排版精良的文本框的更多相关文章
- C#: 向Word插入排版精良的Text Box
Text Box(文本框)是Word排版的工具之一.在Word文档正文的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性.我们可以设置文本框的大小,线型,内部边距,背景填充等 ...
- Word插入htm文件导致文本域动态增加的一个问题
受html标签影响,超链接也会被变成文本域(HYPERLINK).当遍历文本域进行替换之前如果预存了文本域的数量(Count/Length/etc.)将导致遗漏.
- C# 操作Word文本框——插入表格/读取表格/删除表格
在文本框中,我们可以操作很多元素,如文本.图片.表格等,在本篇文章中将着重介绍如何插入表格到文本框,插入的表格我们可以对表格进行格式化操作来丰富表格内容.此外,对于文本框中的表格内容,我们也可以根据需 ...
- class3_Entry & Text 输入和文本框
程序总体运行效果图如下; #!/usr/bin/env python # -*- coding:utf-8 -*- # -------------------------------------- ...
- (9)Microsoft office Word 2013版本操作入门_文本框_word排版
1.插入文本框 :[插入]---[文本框]可以自己画,也可以选择已有的模板 2.文本框设置 :点中文本框选择[格式]---[对齐文本]可以让文字上下居中,[形状填充]填充不同的颜色.[形状轮廓]可以改 ...
- C# 设置Word文本框中的文字旋转方向
在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,如本次测试中的文档排版为考生试卷类型,考生信息栏的内容为下图中的这种, 本文将以C ...
- *像word一样编辑复杂的文本:SpannableString 样式详介
简介: 使用android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder; 和 and ...
- word中创建文本框
word中创建文本框 在插入中点击"文本框"选项卡,例如以下图所看到的: 手工加入自己想要的文本框格式,然后选择所创建的文本框,在工具栏处会发现多了一 ...
- Java 添加Word文本框
在Word中,文本框是指一种可移动.可调节大小的文字或图形容器.我们可以向文本框中添加文字.图片.表格等对象,下面,将通过Java编程来实现添加以上对象到Word文本框. 使用工具:Free Spir ...
随机推荐
- .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上(ZT)
本文转载 https://walterlv.github.io/dotnet/2017/09/22/dotnet-version.html ,感谢 吕毅 (包含链接: https://walte ...
- 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本
[源码下载] 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本 作者:webabcd 介绍背水一 ...
- jquery mobile Touch事件
Touch事件在用户触摸屏幕(页面)时触发 1.jquery mobile tap tap事件在用户敲击某个元素时触发 $("p").on("tap",fucn ...
- Hive数据仓库之快速入门
Hive定位:ETL(数据仓库)工具 将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的工具,如像:kettle 有关Hive数据导入导出mysql的问题 ...
- Javascript高级编程学习笔记(38)—— DOM(4)Text
Text类型 html页面中的纯文本内容就属于Text类型 纯文本内容可以包含转义后的html字符,但不能包括 html 代码 text类型具有以下属性.方法 nodeType:3 nodeName: ...
- HTTP 协议中 GET 和 POST 方法详解
GET请求报文分析 1.请求行 请求方法 GET(描述该请求采用了什么请求方法),HTTP 1.0 和 1.1 协议中共包含10种请求方法.不过 HTTP 1.1 中只有8种方法. URI 请求WEB ...
- Eclipse 中构建 Maven 项目的完整过程 - SpringBoot 项目
进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...
- 《http权威指南》读书笔记9
概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...
- Python开发端口扫描器
首先是最常用的端口扫描器: 虽说有nmap等强大的工具,不过如果由于条件限制无法安装Nmap呢? 我这个脚本写的比较简单,默认扫描1-65535全部的端口 实际的话,可以根据需要自己修改脚本来实现定制 ...
- 原生JS-旋转木马
原生JS-旋转木马 今天写一个原生JS写的旋转木马JS效果. 实现原理: 1.建立一个数组给每一张图片写对应的z-index,opacity,top,width: 2.实现旋转的操作是把建造的数组里面 ...