図形の操作 | Python

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

PSに四角形を追加する

.NETライブラリ経由でAspose.Page for Pythonを使用して PsDocumentに四角形を挿入するには、以下の手順に従います。

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

四角形をストローク(アウトライン)するには、最初の4つの手順と最後の2つの手順は同じですが、5番目と6番目の手順は以下のようになります。

  1. ストロークをPsDocumentの現在のグラフィック状態に設定します。
  2. 四角形をストローク(アウトライン)します。
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "AddRectangle_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first rectangle
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_rectangle(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the rectangle
18    document.fill(path)
19    
20    # Create a graphics path from the second rectangle
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_rectangle(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the rectangle
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

.NETJava の PS ドキュメント内のシェイプの操作を参照してください。

このコードを実行した結果は

四角形の追加

PS に楕円を追加

PsDocument に楕円を追加するには、以下の 8 つの手順を実行する必要があります。

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

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

  1. ストロークをPsDocumentの現在のグラフィック状態に設定します。
  2. 楕円の輪郭線を描きます。
 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_shapes()
 3
 4# Create an output stream for PostScript document
 5with open(data_dir + "AddEllipse_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    # Create a graphics path from the first ellipse
13    path = aspose.pydrawing.drawing2d.GraphicsPath()
14    path.add_ellipse(aspose.pydrawing.RectangleF(250, 100, 150, 100))
15    # Set the paint
16    document.set_paint(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange))
17    # Fill the ellipse
18    document.fill(path)
19    
20    # Create a graphics path from the second ellipse
21    path = aspose.pydrawing.drawing2d.GraphicsPath()
22    path.add_ellipse(aspose.pydrawing.RectangleF(250, 300, 150, 100))
23    # Set the stroke
24    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.red), 3))
25    # Stroke (outline) the ellipse
26    document.draw(path)
27    
28    # Close the current page
29    document.close_page()
30    
31    # Save the document
32    document.save()

このコードを実行した結果は

楕円を追加

ご覧のとおり、 PsDocument を使えば、閉じた図形も閉じていない図形も、塗りつぶしたり描画したりできます。切り取りも可能ですが、これについては別の記事で説明します。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.