打开XML SDK,向所有图形添加渐变边框

时间:2018-09-05 11:41:05

标签: c# xml openxml openxml-sdk wordprocessingml

我正在做自己的项目,我想对ms word文件中的所有图片应用渐变边框。在我的研究中,我只能插入具有指定参数的新图片。问题是,我无法弄清如何访问和自定义现有图片元素。我以为,如果我创建新元素,然后简单地将它们附加到文件中,它将完成工作,但是以某种方式,Drawing元素会因为它属于一棵树或某处而丢弃错误。 下面是我的垃圾代码的代码片段。

            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true)) {
            MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

            int imageCount = mainPart.GetPartsCountOfType<ImagePart>();
            Console.Write(imageCount);

            Drawing sdtElementDrawing = mainPart.Document.Body.Descendants<Drawing>().First();

            Body body1 = new Body();
            Paragraph paragraph1 = new Paragraph();
            Run run1 = new Run();
            DW.Inline inline1 = new DW.Inline();
            A.Graphic graphic1 = new A.Graphic();
            A.GraphicData graphicData1 = new A.GraphicData();
            PIC.Picture picture1 = new PIC.Picture();
            PIC.ShapeProperties shapeProperties1 = new PIC.ShapeProperties();

            A.NoFill noFill1 = new A.NoFill();
            A.Outline outline1 = new A.Outline(new A.GradientFill(
                                      new A.GradientStopList(
                                          new A.GradientStop(
                                              new A.SchemeColor(
                                                  new A.LuminanceModulation() { Val = 5000 },
                                                  new A.LuminanceOffset() { Val = 95000 }
                                              ) { Val = A.SchemeColorValues.Accent1 }
                                          ) { Position = 0 },
                                          new A.GradientStop(
                                              new A.SchemeColor(
                                                  new A.LuminanceModulation() { Val = 45000 },
                                                  new A.LuminanceOffset() { Val = 55000 }
                                              ) { Val = A.SchemeColorValues.Accent1 }
                                          ) { Position = 74000 },
                                          new A.GradientStop(
                                              new A.SchemeColor(
                                                  new A.LuminanceModulation() { Val = 45000 },
                                                  new A.LuminanceOffset() { Val = 55000 }
                                              ) { Val = A.SchemeColorValues.Accent1 }
                                          ) { Position = 83000 },
                                          new A.GradientStop(
                                              new A.SchemeColor(
                                                  new A.LuminanceModulation() { Val = 30000 },
                                                  new A.LuminanceOffset() { Val = 70000 }
                                              ) { Val = A.SchemeColorValues.Accent1 }
                                          ) { Position = 100000 }),
                                      new A.LinearGradientFill() {
                    Angle = 5400000,
                    Scaled = true
                }
                                  ));

            shapeProperties1.Append(noFill1);
            shapeProperties1.Append(outline1);

            picture1.Append(shapeProperties1);
            graphicData1.Append(picture1);
            graphic1.Append(graphicData1);
            inline1.Append(graphic1);

            sdtElementDrawing.Append(inline1);
            run1.Append(sdtElementDrawing);
            paragraph1.Append(run1);

            mainPart.Document.Body.Append(paragraph1);
        }

1 个答案:

答案 0 :(得分:0)

好吧,这样做让我感觉非常愚蠢。现在,我找到了访问和修改一张图纸的方式。要解决的下一个问题,我必须以某种方式遍历所有图像,而不仅仅是第一个。而且,我还没有测试过它将如何影响各种尺寸的图像。下面,我发布更新后的脚本:

using (WordprocessingDocument myDocument = WordprocessingDocument.Open(fileName, true)) {
            // Get the first paragraph.

            Paragraph p = myDocument.MainDocumentPart.Document.Body.Elements<Paragraph>().First();
            Run r = p.Elements<Run>().First();
            Drawing drawing1 = r.Elements<Drawing>().First();
            DW.Inline inline1 = drawing1.Elements<DW.Inline>().First();
            A.Graphic graphic1 = inline1.Elements<A.Graphic>().First();
            A.GraphicData graphicData1 = graphic1.Elements<A.GraphicData>().First();
            PIC.Picture picture1 = graphicData1.Elements<PIC.Picture>().First();
            PIC.ShapeProperties shapeProperties1 = picture1.Elements<PIC.ShapeProperties>().First();
            A.Outline outline1 = shapeProperties1.Elements<A.Outline>().First();
            A.NoFill noFillOutline = outline1.Elements<A.NoFill>().First();
            noFillOutline.Remove();

            outline1.AppendChild(new A.GradientFill(
                new A.GradientStopList(
                    new A.GradientStop(
                        new A.SchemeColor(
                            new A.LuminanceModulation() { Val = 5000 },
                            new A.LuminanceOffset() { Val = 95000 }
                        ) { Val = A.SchemeColorValues.Accent1 }
                    ) { Position = 0 },
                    new A.GradientStop(
                        new A.SchemeColor(
                            new A.LuminanceModulation() { Val = 45000 },
                            new A.LuminanceOffset() { Val = 55000 }
                        ) { Val = A.SchemeColorValues.Accent1 }
                    ) { Position = 74000 },
                    new A.GradientStop(
                        new A.SchemeColor(
                            new A.LuminanceModulation() { Val = 45000 },
                            new A.LuminanceOffset() { Val = 55000 }
                        ) { Val = A.SchemeColorValues.Accent1 }
                    ) { Position = 83000 },
                    new A.GradientStop(
                        new A.SchemeColor(
                            new A.LuminanceModulation() { Val = 30000 },
                            new A.LuminanceOffset() { Val = 70000 }
                        ) { Val = A.SchemeColorValues.Accent1 }
                    ) { Position = 100000 }),
                new A.LinearGradientFill() {
                    Angle = 5400000,
                    Scaled = true
                }
            ));