PSファイルでのハッチパターンの操作 | Java
PSドキュメントにハッチパターンを追加する
ハッチパターンは、通常、小さな2色(通常は白黒)のシンプルな画像で表されるテクスチャタイリングパターンです。これらの小さな画像の主な内容は、様々なハッチです。
Javaプラットフォームには、図形やテキストをハッチで描画するための独立したクラスはありません。しかし、Aspose.Page for Javaライブラリには、 com.aspose.eps.HatchPaintLibrary クラスが用意されており、このクラスを使用することで、 com.aspose.eps.HatchStyle が提供する55種類のスタイルのいずれかで定義されたハッチで塗りつぶされたjava.awt.TexturePaintを作成できます。
Aspose.Page for Java ライブラリでグラフィック オブジェクトをハッチ パターンで ペイント するには、ハッチ スタイルが割り当てられたハッチ java.awt.TexturePaint を作成し、それを setPaint() または java.awt.Paint をパラメーターとして受け取る fillText() や fillAndStrokeText() メソッドのいずれかに渡す必要があります。
Aspose.Page for Java ライブラリでグラフィック オブジェクトをハッチ パターンで アウトライン するには、PsDocument でハッチ パターンを現在のペイントとして設定し、新しい java.awt.BasicStroke を作成し、それを setStroke() または java.awt.Stroke をパラメーターとして受け取る outlineText() や fillAndStrokeText() メソッドのいずれかに渡す必要があります。
以下の例では、まずハッチパターンで図形を塗りつぶす方法、次にJavaで様々なハッチスタイルを使用する方法、最後にハッチパターンでテキストを塗りつぶしてアウトライン化する方法を説明します。
新しいPSドキュメントでグラフィックオブジェクトをハッチパターンでペイントするアルゴリズムは、以下の手順で構成されます。
- 結果のPSファイル用の出力ストリームを作成します。
- PsSaveOptions を作成します。
- 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 塗りつぶすオブジェクトまたはアウトライン化するオブジェクトに応じて、図形またはフォントを作成します。
- com.aspose.eps.HatchPaintLibrary を使用して、希望するスタイルの java.awt.TexturePaint オブジェクトを作成します。
- PsDocument でハッチペイントを現在のペイントとして設定します。
- 現在のペイントで図形を塗りつぶすか、テキストを塗りつぶします。java.awt.Paint をパラメータとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
- ページを閉じます。
- ドキュメントを保存します。
最後の4点の代わりに、ハッチパターンでグラフィックオブジェクトを*ストローク(アウトライン)*する必要がある場合は、以下の手順に従います。
PsDocument でハッチペイントを現在のペイントとして設定します。
java.awt.BasicStroke オブジェクトを作成します。
このストロークを PsDocument で現在のストロークとして設定します。
現在のペイントとストロークで図形のアウトラインを作成するか、テキストのアウトラインを作成します。java.awt.Stroke をパラメータとして受け入れるテキストのアウトライン化メソッドのいずれかを使用する場合は、前の手順は無視されます。
ページを閉じます。
ドキュメントを保存します。
1// The path to the documents directory.
2String dataDir = Utils.getDataDir();
3
4//Create output stream for PostScript document
5FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddHatchPattern_outPS.ps");
6//Create save options with A4 size
7PsSaveOptions options = new PsSaveOptions();
8
9//Create new PS Document with the page opened
10PsDocument document = new PsDocument(outPsStream, options, false);
11
12int x0 = 20;
13int y0 = 100;
14int squareSide = 32;
15int width = 500;
16int sumX = 0;
17
18//Restore graphics state
19document.writeGraphicsSave();
20
21//Translate to initial point
22document.translate(x0, y0);
23
24//Create a square for every pattern
25Rectangle2D.Float square = new Rectangle2D.Float(0, 0, squareSide, squareSide);
26
27//Create pen for outlining pattern square
28BasicStroke stroke = new BasicStroke(2);
29
30HatchStyle [] hatchStyles = HatchStyle.values();
31
32//For every hatch pattern style
33for (int i = 0; i < hatchStyles.length; i++) {
34 //Create a hatch texture pattern by hatch style, foreground and background colors
35 TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(hatchStyles[i], Color.BLACK, Color.WHITE);
36 //Set paint with the current hatch pattern
37 document.setPaint(paint);
38
39 //Calculate a displacement in order to don't go beyond the page bounds
40 int x = squareSide;
41 int y = 0;
42 if (sumX >= width) {
43 x = -(sumX - squareSide);
44 y += squareSide;
45 }
46
47 //Translate current graphics state
48 document.translate(x, y);
49 //Fill pattern square
50 document.fill(square);
51
52 //Set current paint
53 document.setPaint(Color.BLACK);
54 //Set current stroke
55 document.setStroke(stroke);
56 //Draw square outline
57 document.draw(square);
58
59 //Calculate distance from X0
60 if (sumX >= width)
61 sumX = squareSide;
62 else
63 sumX += x;
64}
65
66//Restore graphics state
67document.writeGraphicsRestore();
68
69//Fill a text with the hatch pattern
70TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.DiagonalCross, Color.RED, Color.YELLOW);
71Font font = new Font("Arial", Font.BOLD, 96);
72document.fillAndStrokeText("ABC", font, 200, 400, paint, Color.BLACK, stroke);
73
74//Outline the text with the hatch pattern
75paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.Percent70, Color.BLUE, Color.WHITE);
76document.outlineText("ABC", font, 200, 600, paint, new BasicStroke(5));
77
78//Close current page
79document.closePage();
80//Save the document
81document.save();
.NET の PS ドキュメントでハッチ パターンを操作する方法を参照してください。
このコードを実行した結果は次のように表示されます。
サンプルとデータファイルは以下からダウンロードできます。 GitHub.