XPS グラフィックユーティリティの使用 | Java

XPS で基本的な図形を 簡単に 作成する方法

XPS 仕様では、複雑な図形を作成するために使用できるグラフィックの基本要素が定義されています。Aspose.Page for Java は、これらの要素をカプセル化するクラスを提供しています。しかし、扇形や円弧、円に内接または外接する正 n 角形など、比較的単純な図形の作成でさえ、少々面倒な場合があります。円や楕円を描画することさえ、簡単ではありません。そのため、Aspose.Page では、これらの作業にかかる時間を節約するためのユーティリティ メソッドも提供しています。

以下の例では、利用可能なすべての図形ユーティリティを使用しています。これらのユーティリティメソッドはそれぞれ、 XpsPathGeometryオブジェクトを返す点に注意してください。このオブジェクトは XPSパスの作成に使用できます。また、これらのパスには、ブラシ、ストロークパターン、不透明度などの外観プロパティを指定できます。

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4// Create new XPS Document
 5final XpsDocument doc = new XpsDocument();
 6try {
 7    // Set first page's size.
 8    doc.getPage().setWidth(650f);
 9    doc.getPage().setHeight(240f);
10
11    // Draw a circle with center (120, 120) and radius 100.
12    XpsPath path = doc.createPath(doc.getUtils().createCircle(new Point2D.Float(120f, 120f), 100f));
13    path.setFill(doc.createSolidColorBrush(Color.GREEN));
14    doc.add(path);
15
16    // Inscribe a regular pentagon in the circle.
17    path = doc.createPath(doc.getUtils().createRegularInscribedNGon(5, new Point2D.Float(120f, 120f), 100f));
18    path.setFill(doc.createSolidColorBrush(Color.RED));
19    doc.add(path);
20
21    // Circumscribe a regular hexagon around the circle.
22    path = doc.createPath(doc.getUtils().createRegularCircumscribedNGon(6, new Point2D.Float(120f, 120f), 100f));
23    path.setStroke(doc.createSolidColorBrush(Color.MAGENTA));
24    path.setStrokeThickness(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.getUtils().createPieSlice(new Point2D.Float(340f, 120f), 100f, -45f, 45f));
29    path.setStroke(doc.createSolidColorBrush(Color.RED));
30    path.setStrokeThickness(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.getUtils().createCircularSegment(new Point2D.Float(340f, 120f), 100f, -45f, 45f));
35    path.setFill(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.getUtils().createRectangle(new Rectangle2D.Float(530f, 20f, 100f, 200f)));
40    path.setStroke(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.getUtils().createEllipse(new Point2D.Float(580f, 120f), 50f, 100f));
45    path.setFill(doc.createSolidColorBrush(Color.YELLOW));
46    doc.add(path);
47
48    doc.save(dataDir + "UseShapeUtilsXPS_out.xps");
49} finally {
50  if (doc != null)
51    doc.close();
52}

まず、新しいXPSドキュメントを作成し、最初のページのサイズを調整します。ページに追加する最初の図形は、中心と半径で指定された緑色の円です。次に、その円に内接する塗りつぶされていない赤色の正五角形を描きます。次に、マゼンタで線を引いた正六角形を描き、円に外接します。

右側には、-45度から+45度までの赤色の扇形(または「パイスライス」)を描き、その扇形の上に同じパラメータを持つ黒い円を描きます。

描画の最後の部分には、赤色の四角形(左上隅と寸法で指定)と、その四角形に内接する黄色の楕円(中心と半径で指定)が含まれます。この場合、内接は「手作業」で行います。

保存されたXPSファイルの意図された出力は次のとおりです。

XPS shapes utilities

XPS ページに画像を 簡単に 配置する方法

XPS 仕様で定義されたプリミティブを使用する場合、ページへの画像配置は次の 2 つの手順で行います。

しかし幸いなことに、Aspose.Page API for Java の XPS グラフィック ユーティリティの中に、これらの作業のほとんどを自動で処理できる便利なメソッドがあります。また、一般的なフィットモードも提供されています。次の例をご覧ください。

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2// The path to the documents directory.
 3String dataDir = Utils.getDataDir();
 4// Create new XPS Document
 5final XpsDocument doc = new XpsDocument();
 6try {
 7    // Set first page's size.
 8    doc.getPage().setWidth(540f);
 9    doc.getPage().setHeight(220f);
10
11    // Draw the image box.
12    Rectangle2D imageBox = new Rectangle2D.Float(10f, 10f, 200f, 200f);
13    XpsPath path = doc.addPath(doc.getUtils().createRectangle(imageBox));
14    path.setStroke(doc.createSolidColorBrush(Color.BLACK));
15    // Add an image to fit width.
16    path = doc.getUtils().createImage(dataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
17    // Prevent tiling.
18    ((XpsImageBrush)path.getFill()).setTileMode(XpsTileMode.None);
19    doc.add(path);
20
21    // Add an image to fit width.
22    doc.add(doc.getUtils().createImage(
23        dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
24
25    // Add an image to fit width.
26    doc.add(doc.getUtils().createImage(
27        dataDir + "R08LN_NN.jpg", new Rectangle2D.Float(430f, 10f, 100f, 200f), ImageMode.FitToBox));
28
29    // Save resultant XPS document 
30    doc.save(dataDir + "UseImageUtilsXPS_out.xps");
31} finally {
32  if (doc != null)
33    doc.close();
34}

ここでも、新しい XPS ドキュメントを作成し、最初のページのサイズを変更します。ここで注目すべき点は、ブラシとして使用されている画像は、既定では XpsTileMode.Tile モードで使用された場合と同じように、画像ボックス全体に並べて表示されることです。ただし、例の最初の部分では、この並べて表示されないようにする方法を示します。

まず、画像を座標 (10, 10) に表示し、幅 200 単位、高さ 200 単位の長方形ボックスの幅に合わせる必要があります。結果をより視覚的に確認するために、まずボックス自体を描画します。次に、画像 (基本的には塗りつぶされたパス) を作成します。最後に、パスの塗りつぶしを取得して XpsImageBrushにキャストした後、TileModeプロパティをXpsTileMode.Noneに設定します。

右側には、同じ画像を画像ボックスの高さに合わせて配置しています。タイリング効果に注目してください。

最後に、同じ画像をもう一度右側に配置し、画像ボックスの高さと幅に合わせて調整します。その結果、画像が若干歪んでしまいます。

保存したXPSファイルは以下のようになります。

XPS画像ユーティリティ

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.