Using XPS graphic utilities | .NET
XPS でシンプルな図形を 簡単に 作成する方法
XPS 仕様では、複雑な図形を作成するために使用できるグラフィック プリミティブ要素が定義されています。Aspose.Page for .NET は、これらの要素をカプセル化するクラスを提供しています。しかし、扇形や円弧、円に内接または外接する正 n 角形など、比較的シンプルな図形を作成する場合でも、これらのクラスを使用するのは少々面倒です。円や楕円を描画することさえ、実際にはそれほど簡単ではなく、むしろそうあるべきかもしれません。そのため、Aspose.Page は、こうしたタスクの実行時間を節約できるユーティリティ メソッドのセットも提供しています。
次の例では、利用可能なすべての図形ユーティリティを使用しています。これらすべてが XpsPathGeometry オブジェクトを返すことに注意してください。このオブジェクトは XPS パス の構築に使用できます。また、これらのパスには、ブラシ、ストロークパターン、不透明度などの外観プロパティを指定できます。
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
4// Create new XPS Document
5using (XpsDocument doc = new XpsDocument())
6{
7 // Set first page's size.
8 doc.Page.Width = 650f;
9 doc.Page.Height = 240f;
10
11 // Draw a circle with center (120, 120) and radius 100.
12 XpsPath path = doc.CreatePath(doc.Utils.CreateCircle(new PointF(120f, 120f), 100f));
13 path.Fill = doc.CreateSolidColorBrush(Color.Green);
14 doc.Add(path);
15
16 // Inscribe a regular pentagon in the circle.
17 path = doc.CreatePath(doc.Utils.CreateRegularInscribedNGon(5, new PointF(120f, 120f), 100f));
18 path.Fill = doc.CreateSolidColorBrush(Color.Red);
19 doc.Add(path);
20
21 // Circumscribe a regular hexagon around the circle.
22 path = doc.CreatePath(doc.Utils.CreateRegularCircumscribedNGon(6, new PointF(120f, 120f), 100f));
23 path.Stroke = doc.CreateSolidColorBrush(Color.Magenta);
24 path.StrokeThickness = 3f;
25 doc.Add(path);
26
27 // Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
28 path = doc.CreatePath(doc.Utils.CreatePieSlice(new PointF(340f, 120f), 100f, -45f, 45f));
29 path.Stroke = doc.CreateSolidColorBrush(Color.Red);
30 path.StrokeThickness = 5f;
31 doc.Add(path);
32
33 // Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
34 path = doc.CreatePath(doc.Utils.CreateCircularSegment(new PointF(340f, 120f), 100f, -45f, 45f));
35 path.Fill = doc.CreateSolidColorBrush(Color.Black);
36 doc.Add(path);
37
38 // Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
39 path = doc.CreatePath(doc.Utils.CreateRectangle(new RectangleF(530f, 20f, 100f, 200f)));
40 path.Stroke = doc.CreateSolidColorBrush(Color.Red);
41 doc.Add(path);
42
43 // Draw an ellipse with center (580, 120) and radii 50 and 100.
44 path = doc.CreatePath(doc.Utils.CreateEllipse(new PointF(580f, 120f), 50f, 100f));
45 path.Fill = doc.CreateSolidColorBrush(Color.Yellow);
46 doc.Add(path);
47
48 doc.Save(dataDir + "UseShapeUtilsXPS_out.xps");
49}
まず、新しい XPS ドキュメントを作成し、最初のページのサイズを調整します。ページに配置する最初の図形は、緑色で塗りつぶされた円(中心と半径で指定)です。次に、その円に塗りつぶされていない赤い正五角形を内接させます。次に、マゼンタで線を引いた外接正六角形を描きます。
右側では、まず-45度から+45度までの赤い扇形(または「パイスライス」)を描き、次に扇形の上に同じパラメータを持つ黒い円弧を描きます。
描画の最後の部分は、赤い長方形(左上の頂点と寸法で指定)と、長方形に内接する黄色い楕円(中心と半径で指定)で構成されます。ここでは、内接を「手作業」で制御します。
保存されたXPSファイルには、以下の内容が表示されるはずです。
XPSページに簡単に画像を追加する方法
XPS仕様で定義されたプリミティブを使用すると、ページに画像を追加するには、次の2つの手順が必要です。
- 画像で塗りつぶしたい長方形のパスを追加します。
- パスに画像ブラシを設定します。ここでは、ビューボックスとビューポートを指定する必要があります。通常、最初の2つのパラメータを正確に指定するには、画像の解像度とピクセル単位のサイズを知っておく必要があります。
しかし幸いなことに、Aspose.Page API for .NETのXPSグラフィックユーティリティの中に、(ほぼ)すべての作業を自動的に実行できる便利なメソッドがあります。また、さまざまなフィットモードも提供しています。以下の例をご覧ください。
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithImages();
4// Create new XPS Document
5using (XpsDocument doc = new XpsDocument())
6{
7 // Set first page's size.
8 doc.Page.Width = 540f;
9 doc.Page.Height = 220f;
10
11 // Draw the image box.
12 RectangleF imageBox = new RectangleF(10f, 10f, 200f, 200f);
13 XpsPath path = doc.AddPath(doc.Utils.CreateRectangle(imageBox));
14 path.Stroke = doc.CreateSolidColorBrush(Color.Black);
15 // Add an image to fit width.
16 path = doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
17 // Prevent tiling.
18 ((XpsImageBrush)path.Fill).TileMode = XpsTileMode.None;
19 doc.Add(path);
20
21 // Add an image to fit width.
22 doc.Add(doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", new RectangleF(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
23
24 // Add an image to fit width.
25 doc.Add(doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", new RectangleF(430f, 10f, 100f, 200f), ImageMode.FitToBox));
26
27 // Save resultant XPS document
28 doc.Save(dataDir + "UseImageUtilsXPS.xps");
29}
ここでも、新しい XPS ドキュメントを作成し、最初のページのサイズを調整します。ここで注目すべき点は、ブラシとして使用されている画像は、既定では XpsTileMode.Tile モードで使用された場合と同じように、長方形領域全体に並べて表示されることです。ただし、例の最初の部分では、この並べて表示されないようにする方法を示します。
まず、画像を (10, 10) に表示し、幅 200 単位、高さ 200 単位の長方形のボックスの幅に合わせる必要があります。結果をより明確にするために、まずボックス自体を描画します。次に、画像を作成します (実際には塗りつぶされたパスであることに注意してください)。最後に、パスの塗りつぶしを取得して
XpsImageBrush にキャストした後、TileMode
プロパティを XpsTileMode.None
に設定します。
右側には、同じ画像を画像ボックスの高さに合わせて配置します。タイリングされていることに注意してください。
最後に、同じ画像をもう一度右側に配置し、画像ボックスの高さと幅の両方に合わせて配置します。これにより、画像が歪んでしまいます。
保存された XPS ファイルの結果は次のとおりです。