1
votes

I am adding an image to word document it is working but my main problem is as follows:

1) Insert image in the word document in a table at specific cell.

2) If there are many tables in the word document and if I want to insert in some specific table then what to do?

I am using Microsoft.Office.Interop.Word dll for doing operations on word. In code I have taken a static table, how to achieve dynamic?

My code is as follows:

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;

Please help me thanks in advance.

1
You need some rulesby which you can identify the table you wish to manipulate. This can be by a bookmark for the table, the caption text, or specific text within the table. Its down to you to determine what rule(s) to use and to devise the code to implement it. You can use a for each to iterate through the collection of tables in a document. - freeflow
@Freeflow Thanks, I have used a specific text within the table to identify the placeholder and to insert an image at that position. - Tejas Thakor

1 Answers

0
votes

I have fixed this by using specific text(placeholder) within the table to identify the placeholder and to insert an image at that position

            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++;
                            }                                    
                        }

                    }
                }
            }