将特定大小的图像插入Word文档中的表格单元格

时间:2018-07-27 09:38:25

标签: c# asp.net-mvc-4 office-interop

我要尝试的是根据在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();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

有关信息, 我找到了解决方法:

       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;
相关问题