PSファイル内のクリップの操作 | Java
Contents
[
Hide
Show
]PSドキュメントにクリップを追加
PSドキュメント内のクリップとは、PSビューアまたはエディターに表示される現在のグラフィック状態のコンテンツを囲むパスです。境界からはみ出したコンテンツは切り取られます。
Javaでは、クリッピングパスを3つの方法で割り当てることができます。
- 任意の閉じた図形を含むことができるjava.awt.Shapeを実装した任意のクラスを使用する。
- テキストのアウトラインを使用する。
- 1bpp(ピクセルあたりのビット数)の2色画像をステンシルマスクとして使用する。
現在、Aspose.Page for Javaライブラリは、1番目と2番目のクリッピング方法を提供しています。 以下の例では、円形をクリッピングパスとして取得し、同じグラフィック状態にある青色で塗りつぶされた四角形を切り取ります。
この例では、Aspose.Page for Java ライブラリを使用して新しい PsDocument にクリップを追加します。
- 生成される PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- 円 (java.awt.geom.Ellipse2D オブジェクト) の図形を作成します。
- このパスでクリップを設定します。
- PsDocument の現在のグラフィックス状態にペイントを設定します。
- 現在のペイントで四角形のパスを塗りつぶします。
- 現在のグラフィック状態から上位レベルへ移動します。
- 塗りつぶされた四角形の位置に移動します。
- 塗りつぶされた四角形の上にある同じ四角形の境界を破線で囲み、切り取られた塗りつぶされた四角形の境界を表示します。
- ページを閉じます。
- ドキュメントを保存します。
1// Demonstrates clipping by shape and clipping by text in PS document.
2String outputFileName = "ApplyClipByShape_outPS.ps";
3
4//Create save options with A4 size
5PsSaveOptions options = new PsSaveOptions();
6
7// Create new 1-paged PS Document
8PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
9
10//Create graphics path from the rectangle
11GeneralPath rectanglePath = new GeneralPath();
12rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
13
14////////////////////////////////////// Clipping by shape ///////////////////////////////
15
16//Save graphics state in order to return back to this state after transformation
17document.writeGraphicsSave();
18
19//Displace current graphics state on 100 points to the right and 100 points to the bottom.
20document.translate(100, 100);
21
22//Create graphics path from the circle
23GeneralPath circlePath = new GeneralPath();
24circlePath.append(new Ellipse2D.Float(50, 0, 200, 200), false);
25
26//Add clipping by circle to the current graphics state
27document.clip(circlePath);
28
29//Set paint in the current graphics state
30document.setPaint(Color.BLUE);
31
32//Fill the rectangle in the current graphics state (with clipping)
33document.fill(rectanglePath);
34
35//Restore graphics state to the previous (upper) level
36document.writeGraphicsRestore();
37
38//Displace upper level graphics state on 100 points to the right and 100 points to the bottom.
39document.translate(100, 100);
40
41//Create dashed stroke similar to Pen with DashStyle.Dash
42float[] dash = new float[] { 5.0f };
43BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
44
45document.setStroke(stroke);
46
47//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
48document.draw(rectanglePath);
49
50//Close current page
51document.closePage();
52
53//Save the document
54document.save();See working with clips in PS documents in .NET.
このコードを実行した結果は次のように表示されます。

次の例では、テキストのアウトラインで青く塗りつぶされた四角形をクリッピングするために使用するフォントを取得します。
この例では、Aspose.Page for Java ライブラリを使用して、新しい PsDocument にテキストによるクリッピングを追加するには、次の手順を実行します。
- 結果の PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- フォントを作成します。
- テキストとフォントでクリップを設定します。
- PsDocument の現在のグラフィックス状態にペイントを設定します。
- 現在のペイントで長方形のパスを塗りつぶします。
- 現在のグラフィック状態から上位レベルへ移動します。
- 塗りつぶされた長方形の場所へ移動します。
- 塗りつぶされた長方形の上にある同じ長方形の境界を破線で囲み、切り取られた塗りつぶされた長方形の境界を示します。
- ページを閉じます。
- ドキュメントを保存します。
1// Demonstrates clipping by text in PS document.
2String outputFileName = "ApplyClipByText_outPS.ps";
3
4//Create save options with A4 size
5PsSaveOptions options = new PsSaveOptions();
6
7// Create new 1-paged PS Document
8PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
9
10//Create graphics path from the rectangle
11GeneralPath rectanglePath = new GeneralPath();
12rectanglePath.append(new Rectangle2D.Float(0, 0, 300, 200), false);
13
14//Save graphics state in order to return back to this state after transformation
15document.writeGraphicsSave();
16
17//Displace current graphics state on 100 points to the right and 100 points to the bottom.
18document.translate(100, 100);
19
20float[] dash = new float[] { 5.0f };
21BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
22
23int fontSize = 120;
24Font font = new Font("Arial", Font.BOLD, fontSize);
25
26//Clip rectangle by text's outline
27document.clipText("ABC", font, 20, fontSize + 10);
28
29//Set paint in the current graphics state
30document.setPaint(Color.BLUE);
31
32document.fill(rectanglePath);
33
34document.writeGraphicsRestore();
35
36document.translate(100, 100);
37
38//Set paint in the current graphics state
39document.setPaint(Color.BLUE);
40
41document.setStroke(stroke);
42//Draw the rectangle in the current graphics state (has no clipping) above clipped rectangle
43document.draw(rectanglePath);
44
45//Close current page
46document.closePage();
47
48//Save the document
49document.save();PSドキュメント内のクリップの操作については、 .NET.
このコードを実行すると、次のような結果が表示されます。

サンプルとデータファイルは以下からダウンロードできます。 GitHub.