PSファイル内の図形の操作 | Java

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

PSに四角形を追加する

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

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

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

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

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

 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddRectangle_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first rectangle
12document.fill(new Rectangle2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second rectangle
19document.draw(new Rectangle2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.save();

.NET の PS ドキュメント内の図形の操作を参照してください。


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

Add Rectangle

PS に楕円を追加する

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

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

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

  1. ストロークをPsDocumentの現在のグラフィック状態に設定します。
  2. 楕円の輪郭線を描きます。
 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddEllipse_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new multipaged PS Document with one page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Set paint for filling rectangle
10document.setPaint(Color.ORANGE);        
11//Fill the first ellipse
12document.fill(new Ellipse2D.Float(250, 100, 150, 100));
13
14//Set paint for stroking rectangle
15document.setPaint(Color.RED);
16//Set stroke
17document.setStroke(new BasicStroke(3));
18//Stroke (outline) the second ellipse
19document.draw(new Ellipse2D.Float(250, 300, 150, 100));
20
21//Close current page
22document.closePage();
23//Save the document
24document.save();

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

楕円を追加

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

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.