C# 创建Word文本框(文本/图片/背景)

 时间:2026-04-22 09:01:50

1、实例化Document类,并加载Word文档            

Document document = new Document();            

document.LoadFromFile("Sample.docx");

2、获取首个section中的第一个Paragraph,并添加指定大小的文本框            

TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(180, 340);

3、指定文本框在页面中的位置            

TB.Format.HorizontalOrigin = HorizontalOrigin.Page;            

TB.Format.HorizontalPosition = 330;            

TB.Format.VerticalOrigin = VerticalOrigin.Page;            

TB.Format.VerticalPosition = 110;

4、设置文本环绕方式            

TB.Format.TextWrappingStyle = TextWrappingStyle.Square;            

TB.Format.TextWrappingType = TextWrappingType.Both;

5、格式化文本框,包括设置文本框边框样式         

TB.Format.LineStyle = TextBoxLineStyle.Double;            

TB.Format.LineColor = Color.Black;            

TB.Format.LineDashing = LineDashing.Solid;            

TB.Format.LineWidth = 3;            

TB.Format.InternalMargin.Top = 15;           

TB.Format.InternalMargin.Bottom = 10;            

TB.Format.InternalMargin.Left = 12;            

TB.Format.InternalMargin.Right = 10;

6、加载图片并填充图片作为文本框背景

TB.Format.FillEfects.Type = BackgroundType.Picture;

TB.Format.FillEfects.Picture = Image.FromFile(@"C:\Users\Administrator\Desktop\1.jpg");

7、//添加段落1到文本框,并添加文本,设置文本格式            

Paragraph para1 = TB.Body.AddParagraph();            

para1.Format.AfterSpacing = 6;            

para1.Format.HorizontalAlignment = HorizontalAlignment.Center;            TextRange TR1 = para1.AppendText("The TIMES");            

TR1.CharacterFormat.FontName = "Andalus";            

TR1.CharacterFormat.FontSize = 12;            

TR1.CharacterFormat.TextColor = Color.Black;            

//添加段落2,加载图片并设置图片大小、位置            

Paragraph para2 = TB.Body.AddParagraph();           

 Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\The times.jpg");            

DocPicture picture = para2.AppendPicture(image);            

picture.Width = 120;            

picture.Height = 160;           

para2.Format.AfterSpacing = 8;            

para2.Format.HorizontalAlignment = HorizontalAlignment.Center;            //添加段落3,插入文本并设置格式            

Paragraph para3 = TB.Body.AddParagraph();            

TextRange TR2 = para3.AppendText("The Times is the first newspaper to have borne that name, lending it to numerous other papers around the world, such as The Times of India and The New York Times. ");            

TR2.CharacterFormat.FontName = "Cambria";            

TR2.CharacterFormat.FontSize = 10;            

para3.Format.LineSpacing = 15;            

para3.Format.HorizontalAlignment = HorizontalAlignment.Left;

8、保存并打开文档            

document.SaveToFile("Result.docx");            

System.Diagnostics.Process.Start("Result.docx");

C# 创建Word文本框(文本/图片/背景)

  • 如何使用Word 2010的样式?
  • word2016在页眉或页脚添加页码
  • Word2016 第8课 怎样设置首字下沉
  • ppt如何设置“梳理”的切换效果
  • PPT2007版如何在大纲模式下编辑框架文字
  • 热门搜索
    邮箱怎么填写 跳跳糖为什么会跳 个人交社保怎么交 全身发冷是怎么回事 凛怎么读音 怎么变白皮肤简单有效 怎么安慰失去亲人的人 怕冷是什么原因怎么调理 猫为什么吃老鼠 唛怎么读