在Word文档中,文本框可以包含很多种元素,如文本、图片和表格等。前面我们已经讲了如何插入表格到Word文本框,以及获取和删除Word文本框中的表格。本文将从两个方面详细描述如何插入图片到文本框。
新建word文档,添加文本框,并插入图片和文本到文本框
C#
//创建Document实例并添加节和段落
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
//添加文本框到段落,并指定文本框的宽度和高度
TextBox tb = paragraph.AppendTextBox(140, 120);
//设置文本框的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Page;
tb.Format.HorizontalPosition = 50;
tb.Format.VerticalOrigin = VerticalOrigin.Page;
tb.Format.VerticalPosition = 50;
//插入图片到文本框
Paragraph para1 = tb.Body.AddParagraph();
Image image = Image.FromFile("logo.png");
DocPicture picture = para1.AppendPicture(image);
para1.Format.AfterSpacing = 8;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
//插入文字到文本框
Paragraph para2 = tb.Body.AddParagraph();
TextRange textRange = para2.AppendText("成都冰蓝科技,您的办公文档开发技术专家");
textRange.CharacterFormat.FontName = "Cambria";
textRange.CharacterFormat.FontSize = 9;
para2.Format.LineSpacing = 15;
para2.Format.HorizontalAlignment = HorizontalAlignment.Left;
para2.Format.SuppressAutoHyphens = true;
//保存文档
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
VB.NET
'创建Document实例并添加节和段落
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
Dim paragraph As Paragraph = section.AddParagraph
'添加文本框到段落,并指定文本框的宽度和高度
Dim tb As TextBox = paragraph.AppendTextBox(140, 120)
'设置文本框的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Page
tb.Format.HorizontalPosition = 50
tb.Format.VerticalOrigin = VerticalOrigin.Page
tb.Format.VerticalPosition = 50
'插入图片到文本框
Dim para1 As Paragraph = tb.Body.AddParagraph
Dim image As Image = Image.FromFile("logo.png")
Dim picture As DocPicture = para1.AppendPicture(image)
para1.Format.AfterSpacing = 8
para1.Format.HorizontalAlignment = HorizontalAlignment.Center
'插入文字到文本框
Dim para2 As Paragraph = tb.Body.AddParagraph
Dim textRange As TextRange = para2.AppendText("成都冰蓝科技,您的办公文档开发技术专家")
textRange.CharacterFormat.FontName = "Cambria"
textRange.CharacterFormat.FontSize = 9
para2.Format.LineSpacing = 15
para2.Format.HorizontalAlignment = HorizontalAlignment.Left
para2.Format.SuppressAutoHyphens = true
'保存文档
doc.SaveToFile("Result.docx", FileFormat.Docx2013)
加载已有word文档,获取文本框,在文本框已有文字前插入图片
C#
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
//获取第一个文本框
TextBox textbox = doc.TextBoxes[0];
//插入段落到文本框首行
Paragraph para1 = new Paragraph(doc);
textbox.Body.Paragraphs.Insert(0,para1);
//插入图片到文本框并设置图片大小
Image image = Image.FromFile("logo2.png");
DocPicture picture = para1.AppendPicture(image);
picture.Width = 80f;
picture.Height = 80f;
para1.Format.AfterSpacing = 8;
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
//保存文档
doc.SaveToFile("Result2.docx", FileFormat.Docx2013);
VB.NET
'加载Word文档
Dim doc As New Document()
doc.LoadFromFile("Sample.docx")
'获取第一个文本框
Dim textbox As TextBox = doc.TextBoxes(0)
'插入段落到文本框首行
Dim para1 As New Paragraph(doc)
textbox.Body.Paragraphs.Insert(0, para1)
'插入图片到文本框并设置图片大小
Dim image As Image = Image.FromFile("logo2.png")
Dim picture As DocPicture = para1.AppendPicture(image)
picture.Width = 80F
picture.Height = 80F
para1.Format.AfterSpacing = 8
para1.Format.HorizontalAlignment = HorizontalAlignment.Center
'保存文档
doc.SaveToFile("Result2.docx", FileFormat.Docx2013)