将图像添加到Word模板的占位符

时间:2019-04-24 12:44:09

标签: c# image ms-word office-interop

您好,我拥有带有Open XML SDK的代码,可以在Microsoft Word文档中找到MERGEFIELD并将其替换为提供的数据,这很好,但是我现在想用图像替换提供的字符串 >。

链接中的代码:

  

https://www.codeproject.com/Articles/38575/Fill-Mergefields-in-docx-Documents-without-Microso

    using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true))
                    {
                        //  2010/08/01: addition
                        ConvertFieldCodes(docx.MainDocumentPart.Document);

                        // first: process all tables
                        foreach (var field in docx.MainDocumentPart.Document.Descendants<SimpleField>())
                        {
                            string fieldname = GetFieldName(field, out switches);
                           // I will get fieldname "ImgLogo" and then I want to add an Image at this position. 
                        }
                    }

我将获得如上所示的字段名称“ ImgLogo”,然后我想在此位置添加一个图像。完整的代码显示在上面的链接中。

在这里帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用mergefield if语句。写出语句,完成后将图像粘贴到if语句的else段中。

https://wordmvp.com/FAQs/MailMerge/MMergeIfFields.htm

按Alt + F9以word / excel显示合并域代码。如果要编辑模板文件而不是最终产品。合并后,您将看不到字段。

当您合并图片时,它将覆盖{MERGEFIELD MergeImage}字段。覆盖它们时,{MERGEFIELD MergeImage} =“”将不为真,并且将显示图像而不是占位符。如前所述,占位符图像应粘贴到=“在此处粘贴占位符”。

这与合并图像之前一样,{MERGEFIELD MergeImage}等于空白字符串。

例如

{ IF { MERGEFIELD MergeImage } = "" "Paste Placeholder here" "{ MERGEFIELD MergeImage }" }

以上内容的分解

{ IF CONDITION "TRUE" "FALSE" }

如果未进行任何合并,则将获得占位符,否则将获得合并的图像。

相关问题