PostScript でのクリップ操作 | .NET
PS ドキュメントにクリップを追加
PS ドキュメント内のクリップとは、PS ビューアーまたはエディターに表示される現在のグラフィック ステートのコンテンツを囲むパスです。境界からはみ出したコンテンツは切り取られます。
.NET では、クリッピング パスは 3 つの方法で割り当てることができます。
- 任意の閉じた図形を含む System.Drawing.Drawing2D.GraphicsPath を使用する。
- テキストのアウトラインを使用する。
- 1 bpp (ビット/ピクセル) の 2 色画像をステンシル マスクとして使用する。
現在、Aspose.Page for .NET ライブラリでは、1 番目と 2 番目のクリッピング方法を提供しています。以下の例では、四角形から円 System.Drawing.Drawing2D.GraphicsPath をクリッピング パスとして取得し、同じグラフィック ステートで青色に塗りつぶされた四角形を切り取っています。
この例では、Aspose.Page for .NET ライブラリを使用して新しい PsDocument にクリップを追加するために、次の手順を実行します。
- 結果の PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- 四角形から円 System.Drawing.Drawing2D.GraphicsPath を作成します。
- このパスにクリップを設定します。
- PsDocument の現在のグラフィックス状態にペイントを設定します。
- 現在のペイントで四角形のパスを塗りつぶします。
- 現在のグラフィック状態から上位レベルへ移動します。
- 塗りつぶされた四角形の位置に移動します。
- 塗りつぶされた四角形の上にある同じ四角形の境界を破線で囲み、切り取られた塗りつぶされた四角形の境界を表示します。
- ページを閉じます。
- ドキュメントを保存します。
1//Create an output stream for the PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
3{
4 //Create save options with default values
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create a new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create a graphics path from the rectangle
11 GraphicsPath rectangePath = new GraphicsPath();
12 rectangePath.AddRectangle(new RectangleF(0, 0, 300, 200));
13
14 //Save the graphics state in order to return back to this state after transformation
15 document.WriteGraphicsSave();
16
17 //Displace the current graphics state on 100 points to the right and 100 points to the bottom.
18 document.Translate(100, 100);
19
20 //Create a graphics path from the circle
21 GraphicsPath circlePath = new GraphicsPath();
22 circlePath.AddEllipse(new RectangleF(50, 0, 200, 200));
23
24 //Add a clipping by the circle to the current graphics state
25 document.Clip(circlePath);
26
27 //Set the paint in the current graphics state
28 document.SetPaint(new SolidBrush(Color.Blue));
29
30 //Fill the rectangle in the current graphics state (with the clipping)
31 document.Fill(rectangePath);
32
33 //Restore the graphics state to the previus (upper) level
34 document.WriteGraphicsRestore();
35
36 //Displace the upper level graphics state on 100 points to the right and 100 points to the bottom.
37 document.Translate(100, 100);
38
39 Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
40 pen.DashStyle = DashStyle.Dash;
41
42 document.SetStroke(pen);
43
44 //Draw the rectangle in the current graphics state (has no clipping) above the clipped rectngle
45 document.Draw(rectangePath);
46
47 //Close the current page
48 document.ClosePage();
49
50 //Save the document
51 document.Save();
52}
Linux、macOS、その他Windows以外のOSでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。
そのため、System.Drawing名前空間ではなく、Aspose.Page.Drawing名前空間をインポートしてください。上記のコードスニペットでは、System.Drawing.Rectangleの代わりにAspose.Page.Drawing.Rectangleが、System.Drawing.Drawing2D.GraphicsPathの代わりにAspose.Page.Drawing.Drawing2D.GraphicsPathが使用されるなど、必要な置換はすべてGitHubのコード例に記載されています。
Java の PS ドキュメント内のクリップの操作を参照してください。
このコードを実行した結果は次のように表示されます。
次の例では、テキストのアウトラインで青く塗りつぶされた四角形をクリッピングするフォントを取得します。
この例では、Aspose.Page for .NET ライブラリを使用して、新しい PsDocument にテキストによるクリッピングを追加するために、以下の手順を実行します。
- 生成された PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 新しいグラフィックス状態を作成します。
- フォントを作成します。
- テキストとフォントでクリップを設定します。
- PsDocument の現在のグラフィック状態にペイントを設定します。
- 現在のペイントで四角形のパスを塗りつぶします。
- 現在のグラフィック状態から上位レベルへ移動します。
- 塗りつぶされた四角形の位置に移動します。
- 塗りつぶされた四角形の上にある同じ四角形の境界を破線で囲み、切り取られた塗りつぶされた四角形の境界を示します。
- ページを閉じます。
- ドキュメントを保存します。
1//Create an output stream for the PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "Clipping_outPS.ps", FileMode.Create))
3{
4 //Create save options with default values
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create a new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 //Create a graphics path from the rectangle
11 GraphicsPath rectangePath = new GraphicsPath();
12 rectangePath.AddRectangle(new RectangleF(0, 0, 300, 200));
13
14 //Save the graphics state in order to return back to this state after transformation
15 document.WriteGraphicsSave();
16
17 //Displace the current graphics state on 100 points to the right and 100 points to the bottom.
18 document.Translate(100, 100);
19
20 //Set the paint in the current graphics state
21 document.SetPaint(new SolidBrush(Color.Blue));
22
23 //Create a font
24 int fontSize = 120;
25 Font font = new Font("Arial", fontSize, FontStyle.Bold);
26
27 //Clip the rectangle by text's outline
28 document.ClipText("ABC", font, 20, fontSize + 10);
29 document.Fill(rectanglePath);
30
31 //Restore the graphics state to the previus (upper) level
32 document.WriteGraphicsRestore();
33
34 //Displace the upper level graphics state on 100 points to the right and 100 points to the bottom.
35 document.Translate(100, 100);
36
37 Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
38 pen.DashStyle = DashStyle.Dash;
39
40 document.SetStroke(pen);
41
42 //Draw the rectangle in the current graphics state (has no clipping) above the clipped rectangle
43 document.Draw(rectanglePath);
44
45 //Close the current page
46 document.ClosePage();
47
48 //Save the document
49 document.Save();
50}
Linux、macOS、その他Windows以外のOSでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。
そのため、System.Drawing名前空間ではなく、Aspose.Page.Drawing名前空間をインポートしてください。上記のコードスニペットでは、System.Drawing.Rectangleの代わりにAspose.Page.Drawing.Rectangleが使用され、System.Drawing.Drawing2D.GraphicsPathの代わりにAspose.Page.Drawing.Drawing2D.GraphicsPathが使用されます。GitHubのコード例には、必要な置換がすべて含まれています。
Java の PS ドキュメント内のクリップの操作方法をご覧ください。
このコードを実行した結果は次のように表示されます。
サンプルとデータ ファイルは GitHub からダウンロードできます。