13

この質問はかなり基本的なように思えるかもしれませんが、C# で OpenXML を使用してスライドに形状 (つまり、四角形) を挿入するにはどうすればよいですか?

私は周りを検索しましたが、「シェイプでスライドを作成し、SDK Productivity Tool を使用してコードを反映するだけです。それはあまり役に立ちません:(

4

1 に答える 1

21

しばらく壁に頭をぶつけた後、最終的にアドバイスを受けて、その形でスライドを作成し、ツールを使用してコードを反映させました。そこで、次の世代のために、手で行う方法を簡単に説明します。

まず知っておくべきことは、形状が CommonSlideData の一部である ShapeTree に挿入されるということです。

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

次に知っておくべきことは、形状にはその動作を説明する少なくとも 4 つの子が含まれている必要があるということです。

  • ShapeStyle オブジェクト
  • ShapeProperties オブジェクト
  • TextBody オブジェクト
  • NonVisualShapeProperties

以下のサンプル コードでは、次の名前空間と変数を使用しています。

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 オブジェクト

シェイプ スタイル オブジェクトは、境界線、塗りつぶしスタイル、テキスト フォント、視覚効果 (影など) などの一般的なシェイプ スタイル属性を記述します。

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 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 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

Name や 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);

すべてをまとめる

最後に、シェイプ オブジェクトを作成し、これらのプロパティを追加する必要があります。

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

次に、スライドのシェイプ ツリーにシェイプを追加します。

s.CommonSlideData.ShapeTree.Append(shape1);
于 2013-09-15T07:31:12.397 に答える