在 Word 文档的 table 单元格中插入特定大小的图像
Insert image with specific size in a table cell in Word document
我尝试根据在我的 Asp.NET MVC 应用程序中传递的订单创建一个 pdf 文件。
我必须在 OrderTemplateFR.docx 中的订单项 table 的第一个单元格中添加产品图片。
下面的代码有效,但保留了真实的图像尺寸,并且由于图像尺寸过大,文档的格式不正确。
问题是有没有办法插入特定尺寸的图片。
我可以将图像保存在另一个地方以调整大小并将修改后的图像添加到文档等...但我不确定这是否是最佳解决方案。
你怎么看?
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;
我尝试根据在我的 Asp.NET MVC 应用程序中传递的订单创建一个 pdf 文件。
我必须在 OrderTemplateFR.docx 中的订单项 table 的第一个单元格中添加产品图片。
下面的代码有效,但保留了真实的图像尺寸,并且由于图像尺寸过大,文档的格式不正确。
问题是有没有办法插入特定尺寸的图片。
我可以将图像保存在另一个地方以调整大小并将修改后的图像添加到文档等...但我不确定这是否是最佳解决方案。
你怎么看?
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;