Is there a trick to adding a picture to a word table cell?

Get technical support of Document .Net in C# and VB.Net
FromCustomer
Posts: 3
Joined: Mon May 24, 2021 1:44 pm
Contact:

Is there a trick to adding a picture to a word table cell?

Post by FromCustomer »

Hi. I need to insert an Image in Word Table Cell. How to do that using Document.Net?
Thanks
Oliver
Posts: 30
Joined: Wed Aug 19, 2020 11:59 am
Contact:

Re: Is there a trick to adding a picture to a word table cell?

Post by Oliver »

Hello.

You may insert any images using DocumentBuilder.

If it's the local picture:

Code: Select all

DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);
            
            byte[] imgdata = System.IO.File.ReadAllBytes(@"..\..\image.png");

            // Create a new table with preferred width.
            Table table = db.StartTable();
            db.TableFormat.PreferredWidth = new TableWidth(LengthUnitConverter.Convert(5, LengthUnit.Inch, LengthUnit.Point), TableWidthUnit.Point);

            // Specify formatting of cells and alignment.
            db.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Green, 1);
            db.CellFormat.VerticalAlignment = VerticalAlignment.Top;
            db.ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Specify height of rows and write text with images.
            db.RowFormat.Height = new TableRowHeight(105f, HeightRule.Exact);
            db.InsertCell();
            db.Write("This is Row 1 Cell 1");
            db.InsertCell();
            db.InsertImage(imgdata);
            db.InsertCell();
            db.Write("This is Row 1 Cell 3");
            db.EndRow();
If it's the URL image (please using the same code with):

Code: Select all

using (WebClient client = new WebClient())
            {
                byte[] imageBytes = client.DownloadData("https://www.sautinsoft.com/images/banner_sautinsoft.jpg");
                    using (MemoryStream ms = new MemoryStream(imageBytes))
                {
                    picturl = new Picture(dc, InlineLayout.Inline(new Size(300, 100)), ms);
                }
            }
            /*HttpClient hclient = new HttpClient();
            async Task<byte[]> DownloadImageAsync(string imageUrl)
            {
                try
                {
                    using (var httpResponse = await hclient.GetAsync(imageUrl))
                    {
                        if (httpResponse.StatusCode == HttpStatusCode.OK)
                        {
                            return await httpResponse.Content.ReadAsByteArrayAsync();
                        }
                        else
                        {
                            //Url is Invalid
                            return null;
                        }
                    }
                }
                catch (Exception e)
                {
                    //Handle Exception
                    return null;
                }
            }
I hope, that it was helpful for you.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests