文档星空最火知名网站>>Spire.Doc系列教程>>Word .NET库组件Spire.Doc系列教程(39):在 Word 中创建文本框
Word .NET库组件Spire.Doc系列教程(39):在 Word 中创建文本框
Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。
本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,word文档中经常会使用脚注和尾注来为文档添加说明。本文主要描述如何使用C#在word中创建文本框。
11月优惠进行时,消费满额即享折上豪礼,想买Spire.Doc的朋友赶快吧!
推荐阅读:【想要快速完成文档格式转换吗?Spire系列组件格式转换完整攻略来啦!】
C# 在 Word 中创建文本框
创建只含文字的文本框
//实例化Document对象
Document doc = new Document();
//添加section和段落
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
//在段落上添加文本框
TextBox tb = paragraph.AppendTextBox(120, 50);
//设置文本框相对页边距的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;
tb.Format.HorizontalPosition = 0;
tb.Format.VerticalOrigin = VerticalOrigin.Margin;
tb.Format.VerticalPosition = 50;
//设置文本框填充色、边框颜色及样式
tb.Format.LineColor = Color.DarkBlue;
tb.Format.LineStyle = TextBoxLineStyle.Simple;
tb.Format.FillColor = Color.LightGreen;
//在文本框中添加段落及文字
Paragraph para = tb.Body.AddParagraph();
TextRange tr = para.AppendText("Spire.Doc是一款用于处理Word文档的.NET组件");
//设置文字格式
tr.CharacterFormat.FontName = "黑体";
tr.CharacterFormat.FontSize = 10;
tr.CharacterFormat.TextColor = Color.Black;
//设置段落对齐方式
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
//保存文档
doc.SaveToFile("添加文本框.docx", FileFormat.Docx);
在文本框中同时添加图片和文字
//实例化Document对象
Document doc = new Document();
//添加section和段落
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
//在段落上添加文本框
TextBox tb = paragraph.AppendTextBox(140, 250);
//设置文本框相对页边距的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;
tb.Format.HorizontalPosition = 0;
tb.Format.VerticalOrigin = VerticalOrigin.Margin;
tb.Format.VerticalPosition = 20;
//在文本框中添加段落一,并在段落一插入图片
Paragraph para1 = tb.Body.AddParagraph();
Image image = Image.FromFile("hualuogeng.png");
DocPicture picture = para1.AppendPicture(image);
//设置段落格式
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
para1.Format.AfterSpacing = 8;
//在文本框中添加段落二,添加文本到段落二
Paragraph para2 = tb.Body.AddParagraph();
TextRange textRange = para2.AppendText("华罗庚(1910.11.12—1985.6.12),出生于江苏常州金坛区,祖籍江苏丹阳。数学家,中国科学院院士,美国国家科学院外籍院士,第三世界科学院院士,联邦德国巴伐利亚科学院院士。");
textRange.CharacterFormat.FontName = "黑体";
textRange.CharacterFormat.FontSize = 9;
//设置段落格式
para2.Format.HorizontalAlignment = HorizontalAlignment.Left;
para2.Format.LineSpacing = 15;
//保存文档
doc.SaveToFile("插入图片及文字.docx", FileFormat.Docx2013);

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线