实现如图所示效果:
实现代码如下所示:
注意,此处不做代码格式化处理...
using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using TXTextControl;namespace InsertImage{public partial class Form1 : Form{string FilePath1 = Application.StartupPath + "\\Image\\1.png";string FilePath2 = Application.StartupPath + "\\Image\\2.png";string FilePath3 = Application.StartupPath + "\\Image\\3.png";string FilePath4 = Application.StartupPath + "\\Image\\4.png";public Form1(){InitializeComponent();}private void 插入图片ToolStripMenuItem_Click(object sender, EventArgs e){System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);// 完成TX中使用的度量单位缇(Twip)与.NET使用的度量单位像素(Pixel)的转换Graphics g = txContent.CreateGraphics();int iTwipsPerPixel = (int)(1600 / g.DpiX);// 创建TX中的图片对象System.Drawing.Image thumbImage = img.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);TXTextControl.Image image = new TXTextControl.Image(thumbImage);// 设置图片IDimage.ID = 1001;// 将图片插入到TextFrame中txContent.Images.Add(image, txContent.InputPosition.TextPosition);}private void AddImage(){System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);System.Drawing.Image img2 = System.Drawing.Image.FromFile(FilePath2);System.Drawing.Image img3 = System.Drawing.Image.FromFile(FilePath3);System.Drawing.Image img4 = System.Drawing.Image.FromFile(FilePath4);Listlist = new List ();list.Add(img);list.Add(img2);list.Add(img3);list.Add(img4);//AddOneImage(list);AddOne2Image(list,200,200);}private void AddOne2Image(List list, int width, int height){System.Drawing.Image thumbImage = list[0].GetThumbnailImage(width, height, null, System.IntPtr.Zero);TXTextControl.Image image = new TXTextControl.Image(thumbImage);int txWidth = txContent.Width;txContent.Images.Add(image, txContent.InputPosition.TextPosition);this.txContent.Select(this.txContent.Text.Length, 0);this.txContent.Selection.Text = " ";}public Table AddTable(int rows, int cols, int id){this.txContent.Tables.Add(rows, cols, id);Table table = this.txContent.Tables.GetItem(id);return table;}private void AddOneImage(List list){for (int i = 0; i < list.Count; i++){System.Drawing.Image thumbImage = list[i].GetThumbnailImage(200, 200, null, System.IntPtr.Zero);TXTextControl.Image image = new TXTextControl.Image(thumbImage);int width = txContent.Width;int imageW = image.Size.Width;txContent.Images.Add(image, txContent.InputPosition.TextPosition);this.txContent.Select(this.txContent.Text.Length, 0);this.txContent.Selection.Text = " ";}}private void 插入ToolStripMenuItem_Click(object sender, EventArgs e){AddImage();}}}
2.动态添加表格并为表格添加图片
private void 插入表格ToolStripMenuItem_Click(object sender, EventArgs e) { this.txContent.Tables.Add(5,3,100); } private void 表格中插入图片ToolStripMenuItem_Click(object sender, EventArgs e) { System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1); TXTextControl.Image txImage = new TXTextControl.Image(img); TXTextControl.Table table = txContent.Tables.GetItem(100); TableCell cell = table.Cells.GetItem(1, 1);//第一行第一列,需减1 txContent.Images.Add(txImage, cell.Start-1); TableCell cell2 = table.Cells.GetItem(2, 1);//第二行第一列 txContent.Images.Add(txImage, cell2.Start);//第二行第二列则为5——cell2.Start TableCell cell3 = table.Cells.GetItem(3, 1);//第三行第一列 txContent.Images.Add(txImage, cell3.Start+1); //注意:cell.Start 表示当前单元格的位置,从第一行开始计算 //例如,三行三列的表格,第一行第一个单元格为1,以此类推2、3.....;第二行第二列则为5 //每行第一个单元格插入需:cell.Start-1; //每一行第二列单元格插入则为:cell.Start,以此类推第三列为cell.Star+1,...... }
运行效果如下:
添加图片、图片下方添加文字描述如下所示:
private void 插入图片图片下方添加备注ToolStripMenuItem_Click(object sender, EventArgs e) { this.txContent.Tables.Clear(); this.txContent.Tables.Add(5, 3, 100); System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1); TXTextControl.Image txImage = new TXTextControl.Image(img); TXTextControl.Table table = txContent.Tables.GetItem(100); TableCell cell = table.Cells.GetItem(1, 1);//第一行第一列,需减1 cell.CellFormat.VerticalAlignment = VerticalAlignment.Center; cell.CellFormat.LeftTextDistance = 500; txContent.Images.Add(txImage, cell.Start - 1); TableCell remark1 = table.Cells.GetItem(2, 1);//第1行第2列 remark1.Text = "添加备注1信息成功"; remark1.CellFormat.VerticalAlignment = VerticalAlignment.Center; remark1.CellFormat.LeftTextDistance=500; TableCell cell2 = table.Cells.GetItem(1, 2); cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center; cell2.CellFormat.LeftTextDistance = 500; txContent.Images.Add(txImage, cell2.Start-1); TableCell remark2 = table.Cells.GetItem(2, 2);//第2行第2列 remark2.Text = "添加备注2信息成功"; remark2.CellFormat.VerticalAlignment = VerticalAlignment.Center; remark2.CellFormat.LeftTextDistance = 500; //TableCell cell3 = table.Cells.GetItem(3, 1);//第三行第一列 //txContent.Images.Add(txImage, cell3.Start + 1); }
运行效果如下: