Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Word文档的表格单元格中插入具有特定大小的图像_C#_Asp.net Mvc 4_Office Interop - Fatal编程技术网

C# 在Word文档的表格单元格中插入具有特定大小的图像

C# 在Word文档的表格单元格中插入具有特定大小的图像,c#,asp.net-mvc-4,office-interop,C#,Asp.net Mvc 4,Office Interop,我尝试的是根据Asp.NET MVC应用程序中传递的订单创建pdf文件 我必须将产品图片添加到OrderTemplateFR.docx中Order Items表的第一个单元格中 下面的代码可以工作,但保留了真实的图像大小,并且由于图像大小较大,文档的形式不正确 问题是是否有办法插入具有特定大小的图像 我可以在另一个地方保存图像,以便调整大小并将修改后的图像添加到文档中等。。。但我不确定这是否是最好的解决方案 你觉得怎么样 public static void SaveOrderAsPdf

我尝试的是根据Asp.NET MVC应用程序中传递的订单创建pdf文件

我必须将产品图片添加到OrderTemplateFR.docx中Order Items表的第一个单元格中

下面的代码可以工作,但保留了真实的图像大小,并且由于图像大小较大,文档的形式不正确

问题是是否有办法插入具有特定大小的图像

我可以在另一个地方保存图像,以便调整大小并将修改后的图像添加到文档中等。。。但我不确定这是否是最好的解决方案

你觉得怎么样

    public static void SaveOrderAsPdf()
    {
        using (CapronWebSiteEntities dc = new Models.CapronWebSiteEntities())
        {

            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            try
            {
                //Get cart content and totals
                var cartItems = CartHelper.GetCart();

                string pth = GeneralHelper.WordTemplatesPath + @"OrderTemplateFR.docx";
                var order = SessionHelper.NewOrder;
                var client = SessionHelper.CurrentClient;
                if (System.IO.File.Exists(pth))
                {
                    string tempPath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".docx";
                    System.IO.File.Copy(pth, tempPath);

                    app = new Microsoft.Office.Interop.Word.Application();
                    doc = app.Documents.Open(tempPath);
                    app.Visible = false;

                    doc.Bookmarks["AccountNumber"].Select();
                    app.Selection.TypeText(client.CompteClient);

                    //...

                    var items = (from ro in dc.abwebcomcs where ro.NoCommande == order.NoCommande orderby ro.NoLigneCommande select ro).ToList();
                    int i = 1;
                    foreach (var item in items)
                    {
                        i++;
                        doc.Tables[2].Rows.Add();

                        var product = cartItems.Where(ro => ro.RefNo == item.CodeArticleprestto).First();
                        Microsoft.Office.Interop.Word.Range rng = doc.Tables[2].Cell(i, 1).Range;

                        rng.InlineShapes.AddPicture(string.Format(@"{0}{1}.jpg", GeneralHelper.LocalPhotosPath, product.PhotoPath), ref missing, ref missing, ref missing);

                        doc.Tables[2].Cell(i, 2).Select();
                        app.Selection.TypeText(product.Name);

                        //...
                    }

                    string pdfPath = OrdersPath + "Order_" + order.NoCommande + ".pdf";
                    doc.SaveAs(pdfPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);

                    dc.SaveChanges();

                    doc.Close();
                    app.Quit();
                }
                else
                    GeneralHelper.ParseError(new Exception("Template doesn't exist"), "Prepare order");
            }
            catch (Exception ex)
            {
                GeneralHelper.ParseError(ex, "ValidateInstallationTrackingForm");
                if (doc != null)
                    doc.Close();
                if (app != null)
                    app.Quit();
            }
        }
    }
欲知详情, 我找到了解决办法:

       var shape = rng.InlineShapes.AddPicture(string.Format(@"{0}{1}.jpg", GeneralHelper.LocalPhotosPath, product.PhotoPath), ref missing, ref missing, ref missing).ConvertToShape();
                        shape.HeightRelative = 8f;
                        shape.WidthRelative = 10f;

提示:如果表格列设置为特定宽度(而不是允许使用内容调整大小),它将自动调整您添加的InlineShape的大小,以适合单元格。感谢您的提示。我找到了上面的代码,它是有效的。再次感谢。