C#Core Interop附加/添加图片文件到生成的Word文档[关闭]

问题描述 投票:0回答:1

我正在使用Interop生成一个Word文档,它工作得非常好。

我有一个模板文件,在该模板中我有一个标签#name#,我的代码传递了一个值。我只是想知道如何调整我的代码,以便它也可以添加图片?

假设图像文件位置是Path.Combine(_hostingEnvironment.WebRootPath,“template \ image”+“.jpg”)(我还想将该图像调整到某个尺寸)

public FileResult Download()
    {

        var stream = new MemoryStream();

        string TemplateLoc = "template\\Template.docx";
        string path = Path.Combine(_hostingEnvironment.WebRootPath, TemplateLoc);

        string sourceFile = Path.Combine(path);
        string destinationFile = Path.Combine(_hostingEnvironment.WebRootPath, "template\\Test" + ".docx");

            try
            {
                System.IO.File.Copy(sourceFile, destinationFile);

                Dictionary<string, string> keyValues = new Dictionary<string, string>();

                keyValues.Add("#name#", "Sarah");

                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                word.Visible = false;
                word.ScreenUpdating = false;

                Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(destinationFile);
                Microsoft.Office.Interop.Word.Range range = word.ActiveDocument.Content;

                doc.Activate();

                object missing = Type.Missing;
                object sourceDoc = sourceFile;
                object destinationDoc = destinationFile;
                object matchCase = false;
                object matchWholeWord = true;
                object findWrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                object findFormat = true;
                object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                object fileFormat2 = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
                //

                //Replace text ##
                foreach (KeyValuePair<string, string> kvp in keyValues)
                {
                    object findText = kvp.Key;
                    object replaceText = kvp.Value;

                    if (range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                        ref missing, ref missing, ref missing, ref findWrap, ref missing,
                        ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                    {
                        //Console.WriteLine("Found " + findText + ", replaced with " + replaceText);
                        continue;
                    }
                }


                doc.SaveAs(destinationFile);
                doc.Close();
                word.Quit();


            }
            catch (Exception e)
            {
                throw e;
            }


        byte[] fileBytes = System.IO.File.ReadAllBytes(Path.Combine(_hostingEnvironment.WebRootPath, "template\\Test" + ".docx"));
        string fileName = "test.docx";
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }
c# asp.net asp.net-mvc ms-word interop
1个回答
1
投票

Word有两种插入图形的方法:Shapes或InlineShapes。 InlineShapes与文本一致;形状具有文本换行格式。下面的代码显示了InlineShapes的语法。 Shapes的语法非常相似,如果键入doc.Shapes.AddPicture,则应该获得Intellisense。如果我有一个选择,我使用InlineShape,因为它在页面上的位置更可预测/稳定。

无论你使用哪个,重要的是指定Range参数以正确定位图形。

            foreach (KeyValuePair<string, string> kvp in keyValues)
            {
                object findText = kvp.Key;
                object replaceText = kvp.Value;

                if (range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref missing,
                    ref missing, ref missing, ref missing, ref findWrap, ref missing,
                    ref replaceText, ref replaceAll, ref missing, ref missing, ref missing, ref missing))
                {
                    //Console.WriteLine("Found " + findText + ", replaced with " + replaceText);
                   object oRange = range;
                   object oTrue = true;
                   Microsoft.Office.Interop.Word.InlineShape ils = doc.InlineShapes.AddPicture(Path.Combine(_hostingEnvironment.WebRootPath, "template\image" + ".jpg"), 
                       ref missing, ref oTrue, ref oRange);
                   ils.Height = 100;
                   ils.Width = 100;

                    //Replace text ##
                    continue;
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.