PostScript での図形の操作 | .NET

PSドキュメントに図形を追加する

PSに四角形を追加する

Aspose.Page for .NETライブラリを使用して PsDocumentに四角形を追加するには、次の手順に従います。

  1. 出力されたPSファイル用の出力ストリームを作成します。
  2. デフォルトのオプションで PsSaveOptionsオブジェクトを作成します。
  3. 既に作成済みの出力ストリームと保存オプションを使用して、1ページのPsDocumentを作成します。
  4. 四角形から四角形System.Drawing.Drawing2D.GraphicsPathを作成します。
  5. PsDocumentの現在のグラフィックス状態にペイントを設定します。
  6. 四角形のパスを塗りつぶします。
  7. ページを閉じます。
  8. ドキュメントを保存します。

四角形の輪郭線を描く場合、最初の4つの手順と最後の2つの手順は同じですが、5と6の手順は以下のようになります。

  1. ストロークをPsDocumentの現在のグラフィック状態に設定します。

  2. 四角形のパスをストローク(アウトライン)します。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddRectangle_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    //Create graphics path from the first rectangle
11    GraphicsPath path = new GraphicsPath();
12    path.AddRectangle(new RectangleF(250, 100, 150, 100));
13    //Set paint
14    document.SetPaint(new SolidBrush(Color.Orange));
15    //Fill the rectangle
16    document.Fill(path);
17
18    //Create graphics path from the second rectangle
19    path = new GraphicsPath();
20    path.AddRectangle(new RectangleF(250, 300, 150, 100));
21    //Set stroke
22    document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23    //Stroke (outline) the rectangle
24    document.Draw(path);
25
26    //Close current page
27    document.ClosePage();
28
29    //Save the document
30    document.Save();
31}

Linux、macOS、その他のWindows以外のオペレーティングシステムでは、 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 ドキュメント内のシェイプの操作を参照してください。


このコードを実行した結果は次のように表示されます。

四角形の追加

PS に楕円を追加

PsDocument に楕円を追加するには、以下の 8 つの手順が必要です。

  1. 出力先の PS ファイルの出力ストリームを作成します。
  2. デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
  3. 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 四角形から楕円の System.Drawing.Drawing2D.GraphicsPath を作成します。
  5. PsDocument の現在のグラフィックス状態をペイントに設定します。
  6. 楕円のパスを塗りつぶします。
  7. ページを閉じます。
  8. ドキュメントを保存します。

楕円の輪郭線を描く場合、最初の4つの手順と最後の2つの手順は同じですが、5と6の手順は以下のようになります。

  1. ストロークをPsDocumentの現在のグラフィック状態に設定します。

  2. 楕円のパスを輪郭線で描きます。

 1//Create an output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddEllipse_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6
 7    // Create new 1-paged PS Document
 8    PsDocument document = new PsDocument(outPsStream, options, false);
 9
10    //Create graphics path from the first ellipse
11    GraphicsPath path = new GraphicsPath();
12    path.AddEllipse(new RectangleF(250, 100, 150, 100));
13    //Set paint
14    document.SetPaint(new SolidBrush(Color.Orange));
15    //Fill the ellipse
16    document.Fill(path);
17
18    //Create graphics path from the second ellipse
19    path = new SystemGraphicsPath();
20    path.AddEllipse(new RectangleF(250, 300, 150, 100));
21    //Set stroke
22    document.SetStroke(new Pen(new SolidBrush(Color.Red), 3));
23    //Stroke (outline) the ellipse
24    document.Draw(path);
25
26    //Close current page
27    document.ClosePage();
28
29    //Save the document
30    document.Save();
31}

このコードを実行した結果は次のように表示されます。

楕円を追加

ご覧のとおり、System.Drawing.Drawing2D.GraphicsPath に配置できる図形(閉じているものも閉じていないものも)は、 PsDocument で塗りつぶしたり描画したりできます。 切り取りも可能ですが、これについては別の記事で説明します。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.