asp.net导出图片到word如何定位图片的位置。
程序代码:
private void SaveImageToWord() { Application wordApp = new Application(); Document wordDoc; string strContent = Editor1.Text; // 要生成word的内容 object strFileName = System.Web.HttpContext.Current.Server.MapPath("~/App_UpLoad/20120703"); // 保存文件路径 object fileName = System.Web.HttpContext.Current.Server.MapPath("~/App_UpLoad/SXFSZ-NEW-TZMB.dot"); // 模板路径 Object oMissing = System.Reflection.Missing.Value; wordDoc = wordApp.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing); // 获取word对象 wordDoc.Activate(); object unith; foreach (Bookmark bm in wordDoc.Bookmarks) { if (bm.Name == "通知_Content") { bm.Select(); // 将光标移到当前标签内 Selection currentSelection = wordApp.Selection; // 获取当前光标的位置 if (Regex.IsMatch(strContent, @"<img[^>]*>")) { string[] matches = Regex.Split(strContent, @"(<img[^>]*>)"); foreach (string ma in matches) { if (Regex.IsMatch(ma, @"<img[^>]*>")) { Match match = Regex.Match(ma, @"src=.*"); string ss = match.Value.Replace("src=\"/", "").Replace("\" />", ""); string imgPath = System.Web.HttpContext.Current.Server.MapPath(ss);//图片所在路径 object LinkToFile = false; object SaveWithDocument = true; object Anchor = wordDoc.ActiveWindow.Selection.Range; InlineShape li = currentSelection.InlineShapes.AddPicture(imgPath, ref LinkToFile, ref SaveWithDocument, ref Anchor); Shape shape = li.ConvertToShape(); shape.WrapFormat.Type = WdWrapType.wdWrapSquare; } else { currentSelection.TypeText(ma); } } } else { currentSelection.TypeText(strContent); } } } //将WordDoc文档对象的内容保存为DOC文档 wordDoc.SaveAs(ref strFileName, 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); //关闭WordDoc文档对象 wordDoc.Close(ref oMissing, ref oMissing, ref oMissing); }