PSファイル内の図形の操作 | Java
Contents
[
Hide
Show
]PSドキュメントに図形を追加する
PSに四角形を追加する
Aspose.Page for Javaライブラリを使用して PsDocumentに四角形を追加するには、次の手順に従います。
- 出力されたPSファイル用の出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptionsオブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1ページのPsDocumentを作成します。
- 四角形(java.awt.geom.Rectangle2Dオブジェクト)を作成します。
- PsDocumentの現在のグラフィック状態にペイントを設定します。
- 四角形を塗りつぶします。
- ページを閉じます。
- ドキュメントを保存します。
四角形のストローク(アウトライン)を描く場合、最初の4つの手順と最後の2つの手順は同じですが、5番目と6番目の手順は以下のようになります。
ストロークをPsDocumentの現在のグラフィック状態に設定します。
四角形のストローク(アウトライン)を描きます。
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 ドキュメント内の図形の操作を参照してください。
このコードを実行した結果は次のように表示されます。
PS に楕円を追加する
PsDocument に楕円を追加するには、以下の 8 つの手順が必要です。
- 出力先の PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 楕円 (java.awt.geom.Ellipse2D オブジェクト) を作成します。
- PsDocument の現在のグラフィック状態にペイントを設定します。
- 楕円のパスを塗りつぶします。
- ページを閉じます。
- ドキュメントを保存します。
楕円の輪郭線を描く場合、最初の4つのステップと最後の2つのステップは同じですが、5番目と6番目のステップは以下のようになります。
- ストロークをPsDocumentの現在のグラフィック状態に設定します。
- 楕円の輪郭線を描きます。
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.