打开XML SDK,向所有图形添加渐变边框 [英] Open XML SDK adding gradient border to all drawings
问题描述
我正在做自己的项目,我想对ms word文件中的所有图片应用渐变边框.在我的研究中,我只能插入具有指定参数的新图片.问题是,我无法弄清如何访问和自定义现有图片元素.我以为,如果我创建新元素,然后将它们简单地附加到文件中,它将完成工作,但是以某种方式,Drawing元素会因为它属于一棵树或某处而丢弃错误. 下面是我的垃圾代码的代码片段.
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);
}
推荐答案
好吧,这样做让我感觉超级愚蠢.现在,我找到了访问和修改一张图纸的方式.要解决的下一个问题,我必须以某种方式遍历所有图像,而不仅仅是第一个.而且,我还没有测试过它将如何影响各种尺寸的图像.在下面,我发布了更新后的脚本:
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
}
));
这篇关于打开XML SDK,向所有图形添加渐变边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!