【问题标题】:Changing color of oval in word docx using openxml使用openxml更改word docx中椭圆的颜色
【发布时间】:2018-04-10 21:14:44
【问题描述】:

我有一个包含椭圆的模板文档。我可以使用以下代码访问椭圆形,在调试时我看到 FillColor 的值发生变化,但是当打开 docx 一词时,椭圆形是原始颜色。

我将此代码用作测试。颜色变化将根据应用值动态变化。尝试获取 FillColor 字符串的正确格式。

更多信息 - 添加了更改椭圆中的一些文本(使用更改正文中的标题的相同代码)并且也没有保存。

public ActionResult ....
{
.... other code newpath = copy of original  word Template as you suggested with one Header to replace and Oval
        WordUpdateDocxProfile(pptvm, newpath, replacethem, colwidths, 4, 0);
        //download file and then delete
        DownloadandDelete(newpath);
        Return View();

} 



private void WordUpdateDocxProfile(PerfProfileTotalsViewModel m, string newpath, List<MatrixReplaceViewModel> replacethem, List<int> colwidths, int textcols, int numcols)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(newpath, true))
            {
                var body = doc.MainDocumentPart.Document.Body;
                var paras = body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
                WordReplaceItems(paras, replacethem);
                IEnumerable<DocumentFormat.OpenXml.Vml.Oval> shapes = doc.MainDocumentPart.Document.Body.Descendants<DocumentFormat.OpenXml.Vml.Oval>();
                foreach( var oo in shapes)
                {
                   oo.FillColor= "#c5e0b3 [1305]";
                    IEnumerable<DocumentFormat.OpenXml.Wordprocessing.Paragraph> ps = oo.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
                    List<MatrixReplaceViewModel> replacerv = new List<MatrixReplaceViewModel>();
                    replacerv.Add(new MatrixReplaceViewModel { replacewith = "RV", toreplace = "RS" });
                    replacerv.Add(new MatrixReplaceViewModel { replacewith = "2.7", toreplace = "Val" });
                    replacerv.Add(new MatrixReplaceViewModel { replacewith = "Valuesss", toreplace = "Score" });
                    WordReplaceItems(ps, replacerv);


}
            }
        }
 private void WordReplaceItems(IEnumerable<DocumentFormat.OpenXml.Wordprocessing.Paragraph> paras, List<MatrixReplaceViewModel> replacethem)
        {
            foreach (var para in paras)
            {
                foreach (var run in para.Elements<Run>())
                {
                    foreach (var text in run.Elements<Text>())
                    {
                        foreach (var replace in replacethem)
                        {
                            if (text.Text.Contains(replace.toreplace))
                            {
                                text.Text = text.Text.Replace(replace.toreplace, replace.replacewith);
                                break;  //leave if we found and replaced in this text element within the run
                            }
                        }
                    }
                }
            }
        }

 private void DownloadandDelete(string path)
        {
            //download file and then delete
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
            System.IO.File.Delete(path);
        }

在 WordUpdateDocxProfile 中使用 WordprocessingDocument doc = ... 结束时,向下钻取到椭圆时的值。FillColor = "#c5e0b3 [1305]"。我在去 DownloadandDelete(newpath) 之前停止了代码,并在 Open XML SDK Productivity Tool 中打开了 word 文档。标题文本已更改,但 Oval FillColor 未更改。

这是来自 Open XML SDK 生产力工具

椭圆 椭圆1 = new Oval(){ Id = "椭圆 1", Style = “位置:绝对;左:0;文本对齐:左;边距左:0;边距顶部:-.05pt;宽度:90pt;高度:80.4pt;z-index:251661312;可见性:可见;mso-环绕样式:方形;mso 宽度百分比:0;mso 高度百分比:0;mso 环绕距离左:9pt;mso 环绕距离顶部:0;mso 环绕距离右: 9pt;mso-wrap-distance-bottom:0;mso-position-horizo​​ntal:absolute;mso-position-horizo​​ntal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-宽度百分比:0;mso-高度百分比:0;mso-width-relative:边距;mso-height-relative:边距;v-text-anchor:top", Alternate =“标题:圆圈”,OptionalString =“_x0000_s1026”, FillColor = "#4472c4 [3204]", StrokeColor = "白色 [3212]", StrokeWeight = "1pt" };

【问题讨论】:

  • 尝试创建两个只有椭圆形的文档,一个使用一种颜色,第二个使用另一种颜色。在 Open XML SDK Productivity Tool 中打开第一个,然后选择与第二个进行比较。您应该获得显示如何将一个更改为另一个的代码 - 将其与您正在做的事情进行比较,看看您是否能找到差异。如果您希望这里的任何人能够更详细地帮助您,您需要提供重现步骤和更完整的代码(请参阅minimal reproducible example)。
  • @CindyMeister 辛迪,谢谢。我为简短的解释道歉。直到您建议我才下载该工具,我很高兴我这样做了。我按照你说的做了,创建了两个简单的文档,只有一个要替换的标题和椭圆(没有表格)。使用工具的输出来获取 FillColor 的新十六进制代码。我在上面添加了更多。仍然没有得到结果。我是否需要 .Append 到 Oval.FillColor 属性而不是尝试直接设置它?我是否需要为属性单独保存 WordProcessingDocument 而不是更新的文本?
  • 该工具还应该向您显示从另一个文档创建一个文档所需的代码。这对你有什么有用的吗? (请注意,我不知道答案 - 我必须自己完成这些步骤,而且目前我没有太多时间进行这种研究......)
  • @CindyMeister 是的,这个工具就是答案,我非常感谢你。我刚刚开始使用 openxml,该工具帮助我了解了 xml 中有多少嵌套。它向我展示了 2 docx 中的 Oval.FillColor 可能不同,并且颜色仍然相同。我将把答案放在下面。再次感谢。我会在搜索时寻找你的名字。

标签: c#-4.0 openxml-sdk


【解决方案1】:

注意:我正在使用存储在我的服务器上的 docx 模板(在下面的 using 语句中复制为新路径)。为了访问您想要更改的形状,在本例中是我的椭圆形,我必须为形状设置标题。通过右键单击 docx 中的形状来执行此操作... 格式化 Shape/Layout & Properties/Alt Text/Title 并设置 Title 值(我的是圆形)

DocumentFormat.OpenXml.Vml.OvalFillColor 属性不是设置椭圆颜色的原因。 Open XML Productivity Tool 帮助我找到了实际设置颜色的位置。在我的实际 docx 中,我需要的 ShapeProperties 是 xml 层次结构的 17 级。通过接受 Cindy 的建议并创建一个仅包含椭圆形和其他段落的简单 docx,我能够确定需要设置的值。

ShapeProperties 嵌套在Anchor 中,AnchorDocProperties 有一个Title,在我的情况下,我正在寻找“圆圈”,这是我在 docx 中设置的标题。

 using (WordprocessingDocument doc = WordprocessingDocument.Open(newpath, true))
            {
          var body = doc.MainDocumentPart.Document.Body;
          var anchor = body.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.Anchor>();
                foreach (var anc in anchor)
                {
                    var docProperties = anc.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties>().Where(tp => tp.Title != null);
                    foreach (DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties docProp in docProperties)
                    {
                        if (docProp.Title.Value == "circle")
                        {
                            var shapes = anc.Descendants<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape>();
                            foreach (var sh in shapes)
                            {
                                foreach (var sp in sh.Elements<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeProperties>())
                                {
                                    foreach (var fill in sp.Elements<DocumentFormat.OpenXml.Drawing.SolidFill>())
                                    {
                                        // deleting SchemeColor if already set up in the solidfill
                                        if (fill.SchemeColor != null) { fill.SchemeColor.Remove(); }
                                        // m.QLA.CircleColorHex is simply a string representing the color in 6 char Hex - this does not use the # before, so a value is like "FFFFFF" 
                                        DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgb = new DocumentFormat.OpenXml.Drawing.RgbColorModelHex() { Val = m.QLA.CircleColorHex };
                                        fill.Append(rgb);
                                    }
                                }
                            }
                        }
                    }
                }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多