11月优惠进行时,消费满额即享折上豪礼,想买Spire.Doc的朋友赶快咨询在线客服吧!
推荐阅读:【想要快速完成文档格式转换吗pire系列组件格式转换完整攻略来啦!】
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);
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!