如何使用OpenXML在PowerPoint幻灯片中插入形状 [英] How to insert a shape in a powerpoint slide using OpenXML

查看:130
本文介绍了如何使用OpenXML在PowerPoint幻灯片中插入形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题看似很基本,但是如何在c#中使用OpenXML在幻灯片中插入形状(即矩形)?

This question could seem rather basic, but how do I insert a shape (i. e. a rectangle) in a slide using OpenXML in c#?

我到处搜索,然后看到的只是创建具有形状的幻灯片,并使用SDK生产力工具来反映代码.这并不是真的有帮助:(

I've searched around and all I see is "create a slide with the shape and use the SDK Productivity Tool to reflect the code. That's not really helpful :(

推荐答案

将我的头撞在墙上一会儿后,我终于接受了建议,制作了一个带有形状的幻灯片,并使用该工具来反映代码.因此,对于子孙后代,这里是有关如何手动操作的简要说明.

After hitting my head against the wall for a while, finally I took the advice, created a slide with the shape and used the tool to reflect the code. So for next generations, here is a brief explanation of how to do it by hand.

首先要知道的是,将形状插入到ShapeTree中,该树是CommonSlideData的一部分:

Firts of all you have to know is that a shape is inserted into a ShapeTree, which is part of a CommonSlideData:

Slide s = GetDesiredSlide(); // Get the slide where you want to insert the shape.
s.CommonSlideData.ShapeTree.Append(GenerateShape());

第二,您应该知道一个形状必须包含至少4个描述其行为的子元素:

Second you should know is that a shape must contain at least 4 childs that describe its behavior:

  • 一个ShapeStyle对象
  • ShapeProperties对象
  • TextBody对象
  • NonVisualShapeProperties

在下面的示例代码中,我正在使用以下名称空间和变量:

In the sample code bellow I am using the following namespaces and variables:

using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;

D.ShapeTypeValues shapeType; // Any of the built-in shapes (ellipse, rectangle, etc)
string rgbColorHex; // Hexadecimal RGB color code to fill the shape.
long x; // Represents the shape x position in 1/36000 cm.
long y; // Represents the shape y position in 1/36000 cm.
long width; // Shapw width in in 1/36000 cm.
long heigth;// Shapw heigth in in 1/36000 cm.

ShapeStyle对象

形状样式对象描述了常规形状样式属性,例如边框,填充样式,文本字体和视觉效果(阴影等)

The shape style object describes general shape style attributes like the borders, the fill style, the text font and the visual effect (shadows, and other)

ShapeStyle shapeStyle1 = new ShapeStyle();

D.LineReference lineReference1 = new D.LineReference() { Index = (UInt32Value)2U };

D.SchemeColor schemeColor2 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };
D.Shade shade1 = new D.Shade() { Val = 50000 };

schemeColor2.Append(shade1);

lineReference1.Append(schemeColor2);

D.FillReference fillReference1 = new D.FillReference() { Index = (UInt32Value)1U };
D.SchemeColor schemeColor3 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };

fillReference1.Append(schemeColor3);

D.EffectReference effectReference1 = new D.EffectReference() { Index = (UInt32Value)0U };
D.SchemeColor schemeColor4 = new D.SchemeColor() { Val = D.SchemeColorValues.Accent1 };

effectReference1.Append(schemeColor4);

D.FontReference fontReference1 = new D.FontReference() { Index = D.FontCollectionIndexValues.Minor };
D.SchemeColor schemeColor5 = new D.SchemeColor() { Val = D.SchemeColorValues.Light1 };

fontReference1.Append(schemeColor5);

shapeStyle1.Append(lineReference1);
shapeStyle1.Append(fillReference1);
shapeStyle1.Append(effectReference1);
shapeStyle1.Append(fontReference1);

ShapeProperties

定义形状的视觉属性,例如填充"方法(实体,渐变等),几何"(大小,位置,翻转,旋转),填充"和轮廓".

Define shape's visual Properties like Fill method (solid, gradient, etc.) Geometry (size, location, flipping, rotarion) Fill and Outline.

ShapeProperties shapeProperties1 = new ShapeProperties();

D.Transform2D transform2D1 = new D.Transform2D();
D.Offset offset1 = new D.Offset() { X = x, Y = y };
D.Extents extents1 = new D.Extents() { Cx = width, Cy = heigth };

transform2D1.Append(offset1);
transform2D1.Append(extents1);

D.PresetGeometry presetGeometry1 = new D.PresetGeometry() { Preset = shapeType };
D.AdjustValueList adjustValueList1 = new D.AdjustValueList();

presetGeometry1.Append(adjustValueList1);

D.SolidFill solidFill1 = new D.SolidFill();
D.RgbColorModelHex rgbColorModelHex1 = new D.RgbColorModelHex() { Val = rgbColorHex };

solidFill1.Append(rgbColorModelHex1);

D.Outline outline1 = new D.Outline() { Width = 12700 };

D.SolidFill solidFill2 = new D.SolidFill();
D.SchemeColor schemeColor1 = new D.SchemeColor() { Val = D.SchemeColorValues.Text1 };

solidFill2.Append(schemeColor1);

outline1.Append(solidFill2);

shapeProperties1.Append(transform2D1);
shapeProperties1.Append(presetGeometry1);
shapeProperties1.Append(solidFill1);
shapeProperties1.Append(outline1);

TextBody

定义文本框属性,例如列数,对齐方式,锚定等.

Defines the textbox attributes like number of columns, alignment, anchoring, etc.

TextBody textBody1 = new TextBody();
D.BodyProperties bodyProperties1 = new D.BodyProperties() { RightToLeftColumns = false, Anchor = D.TextAnchoringTypeValues.Center };
D.ListStyle listStyle1 = new D.ListStyle();

D.Paragraph paragraph1 = new D.Paragraph();
D.ParagraphProperties paragraphProperties1 = new D.ParagraphProperties() { Alignment = D.TextAlignmentTypeValues.Center };
D.EndParagraphRunProperties endParagraphRunProperties1 = new D.EndParagraphRunProperties() { Language = "es-ES" };

paragraph1.Append(paragraphProperties1);
paragraph1.Append(endParagraphRunProperties1);

textBody1.Append(bodyProperties1);
textBody1.Append(listStyle1);
textBody1.Append(paragraph1);

NonVisualShapeProperties

定义非视觉属性,例如名称和ID.

Define non visual properties like the Name and the Id.

NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "1 Shape Name" };
NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new NonVisualShapeDrawingProperties();
ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);

全部组合

最后,您必须创建形状对象并将以下属性附加到该对象:

Finally you have to create the shape object and append these properties to it:

Shape shape1 = new Shape();
shape1.Append(nonVisualShapeProperties1);
shape1.Append(shapeProperties1);
shape1.Append(shapeStyle1);
shape1.Append(textBody1);

然后,将形状添加到幻灯片的形状树中:

And then, add the shape to the slide's shape tree:

s.CommonSlideData.ShapeTree.Append(shape1);

这篇关于如何使用OpenXML在PowerPoint幻灯片中插入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆