PostScript での図形の操作 | C++
Contents
[
Hide
Show
]PSドキュメントに図形を追加する
PSに四角形を追加する
Aspose.Page for C++ライブラリを使用して PsDocumentに四角形を追加するには、次の手順に従います。
- 出力されたPSファイル用の出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptionsオブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1ページのPsDocumentを作成します。
- 四角形から四角形System.Drawing.Drawing2D.GraphicsPathを作成します。
- PsDocumentの現在のグラフィックス状態にペイントを設定します。
- 四角形のパスを塗りつぶします。
- ページを閉じます。
- ドキュメントを保存します。
四角形の輪郭を描画する場合、最初の4つの手順と最後の2つの手順は同じですが、5番目と6番目の手順は次のようになります。
PsDocument の現在のグラフィック状態をストロークに設定します。
四角形のパスをストローク(アウトライン)します。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddRectangle_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19 //Create graphics path from the first rectangle
20 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
21 path->AddRectangle(System::Drawing::RectangleF(250.0f, 100.0f, 150.0f, 100.0f));
22 //Set paint
23 document->SetPaint(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
24 //Fill the rectangle
25 document->Fill(path);
26
27 //Create graphics path from the second rectangle
28 path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
29 path->AddRectangle(System::Drawing::RectangleF(250.0f, 300.0f, 150.0f, 100.0f));
30 //Set stroke
31 document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 3.0f));
32 //Stroke (outline) the rectangle
33 document->Draw(path);
34
35 //Close current page
36 document->ClosePage();
37
38 //Save the document
39 document->Save();
40 }
41 catch(...)
42 {
43 __dispose_guard_0.SetCurrentException(std::current_exception());
44 }
45 }
このコードを実行した結果は次のように表示されます。
PS に楕円を追加する
PsDocument に楕円を追加するには、以下の 8 つの手順が必要です。
- 出力先の PS ファイルの出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
- 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
- 四角形から楕円の System.Drawing.Drawing2D.GraphicsPath を作成します。
- PsDocument の現在のグラフィックス状態をペイントに設定します。
- 楕円のパスを塗りつぶします。
- ページを閉じます。
- ドキュメントを保存します。
楕円の輪郭を描く場合、最初の4つのステップと最後の2つのステップは同じですが、5番目と6番目のステップは以下のようになります。
ストロークをPsDocumentの現在のグラフィック状態に設定します。
楕円のパスをストローク(アウトライン)します。
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddEllipse_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19 //Create graphics path from the first ellipse
20 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
21 path->AddEllipse(System::Drawing::RectangleF(250.0f, 100.0f, 150.0f, 100.0f));
22 //Set paint
23 document->SetPaint(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
24 //Fill the ellipse
25 document->Fill(path);
26
27 //Create graphics path from the second ellipse
28 path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
29 path->AddEllipse(System::Drawing::RectangleF(250.0f, 300.0f, 150.0f, 100.0f));
30 //Set stroke
31 document->SetStroke(System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Red()), 3.0f));
32 //Stroke (outline) the ellipse
33 document->Draw(path);
34
35 //Close current page
36 document->ClosePage();
37
38 //Save the document
39 document->Save();
40 }
41 catch(...)
42 {
43 __dispose_guard_0.SetCurrentException(std::current_exception());
44 }
45 }
このコードを実行した結果は次のように表示されます。
ご覧のとおり、System.Drawing.Drawing2D.GraphicsPath に配置できる閉じた図形と閉じていない図形の両方は、 PsDocument によって塗りつぶしたり描画したりできます。 また、クリッピングも可能ですが、これについては別の記事で説明します。
サンプルとデータ ファイルは GitHub からダウンロードできます。