基于表单元格动态地插入Word文档中的图像



我正在向Word文档添加图像,但我的主要问题如下:

1(在特定单元格处的表格文档中插入图像。

2(如果Word文档中有很多表,并且如果我想在某个特定表中插入,那么该怎么办?

我正在使用microsoft.office.interop.word dll在Word上进行操作。在代码中,我拿了一个静态表,如何实现动态?

我的代码如下:

Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref wordfilename, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    Range rngPic = doc.Tables[1].Cell(1, 3).Range;    

                        // we can even select a particular cell in the table
                        //Range rngPic = rng.Tables[1].Cell(2, 3).Range;
                        string logoLocation = ConfigurationManager.AppSettings["LogoLocation"];
                        var LogoPath = Path.Combine((HttpContext.Current.Server.MapPath(logoLocation)), fpnInfo.LogoPath);
                        Microsoft.Office.Interop.Word.InlineShape picture = rngPic.InlineShapes.AddPicture(LogoPath, Type.Missing, Type.Missing, Type.Missing);
                        picture.Height = 50;
                        picture.Width = 50;

请提前帮助我。

我已经通过表中的特定文本(占位符(来确定占位符并在该位置插入图像

            foreach (Microsoft.Office.Interop.Word.Table tb in doc.Tables)
            {
                int imageCounter = 0;
                for (int row = 1; row <= tb.Rows.Count; row++)
                {
                    for (int Cols = 1; Cols <= tb.Columns.Count; Cols++)
                    {
                        var tableCell = tb.Cell(row, Cols);
                        var cellText = tableCell.Range.Text;                            
                        if (cellText.Contains("Image1"))
                        {
                            tableCell.Range.Text = "";
                            if (fpnImage  != null && fpnImage.Any() && fpnImage[imageCounter].Path != null)
                            {
                                var MediaPathImage1 = Path.Combine((HttpContext.Current.Server.MapPath(mediaImage)), fpnImage[imageCounter].Path);
                                var rangePic = tb.Cell(row, Cols).Range;
                                Microsoft.Office.Interop.Word.InlineShape rangePicture = rangePic.InlineShapes.AddPicture(MediaPathImage1, Type.Missing, Type.Missing, Type.Missing);
                                rangePicture.Height = imageHeight;
                                rangePicture.Width = imageWidth;
                                imageCounter++;
                            }                                    
                        }
                    }
                }
            }

相关内容

  • 没有找到相关文章

最新更新