Open XML SDK adding gradient border to all drawings

ε祈祈猫儿з 提交于 2019-12-25 04:14:12

问题


I'm working on my own project, and i want to apply gradient borders to all pictures in the ms word file. On my research, i was only able to insert new pictures with assigned parameters. The problem is, that i can't figure it out how to access and customize existing picture elements. I thought, that if i create new elements, and then simply append them to a file it will do the work, but somehow Drawing element drops an error because it belongs to a tree or smth. Below is the code fragment of my trash code.

            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:


Ok, i was like super stupid by doing this. Now i found my way to access and modify one drawing. Next problem to solve, i have to somehow iterate trough all the images, not only the first one. What is more, i still haven't tested how it will affect images in various sizes. Below i post my updated script:

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
                }
            ));


来源:https://stackoverflow.com/questions/52184296/open-xml-sdk-adding-gradient-border-to-all-drawings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!