文档星空最火知名网站>>Spire.Doc系列教程>>Spire.Doc系列教程(4):在Word中插入和删除文本框
Spire.Doc系列教程(4):在Word中插入和删除文本框
在Word中,文本框是一种可移动、可调节大小的文字或图片的容器。使用文本框,我们可以在文档的一个页面上放置多个可移动的文字或图片块,也可以使文字按与文档中其他文字不同的方向排列,从而使文档的排版效果更加精美。本文将介绍如何使用Spire.Doc在Word文档中插入和删除文本框。
插入文本框
//加载Word文档
Document document = new Document();
document.LoadFromFile(@"测试文档.docx");
//添加一个段落到第一个section并添加文本框到该段落
TextBox textBox = document.Sections[0].AddParagraph().AppendTextBox(150, 30);
//设置文本框格式
textBox.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left;
textBox.Format.LineColor = Color.White;
textBox.Format.LineStyle = TextBoxLineStyle.Simple;
textBox.Format.FillColor = Color.ForestGreen;
//在文本框中添加一个段落,并给段落添加文本
Paragraph para = textBox.Body.AddParagraph();
TextRange textRange = para.AppendText("文本框");
//设置文本格式
textRange.CharacterFormat.FontName = "Century Gothic";
textRange.CharacterFormat.FontSize = 20;
textRange.CharacterFormat.TextColor = Color.White;
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//保存文档
document.SaveToFile("添加文本框.docx", FileFormat.Docx);

删除文本框
//加载文档
Document document = new Document();
document.LoadFromFile(@"添加文本框.docx");
//删除文档中第一个文本框
document.TextBoxes.RemoveAt(0);
//删除所有文本框
//document.TextBoxes.Clear();
//保存文档
document.SaveToFile("删除文本框.docx", FileFormat.Docx);


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线