PSファイルでの透明度の設定 | Java

PS ドキュメントに透明度を追加する

PostScript は、ベクター グラフィック オブジェクトの描画において透明度をサポートしていません。ただし、半透明(半透明)画像は、完全に透明なピクセルと完全に不透明なピクセルのセットとしてレンダリングできます。 このような画像は マスク と呼ばれます。

Aspose.Page for Java ライブラリには、PS ドキュメントに透明画像を追加するメソッドが用意されています。ベクター グラフィック(図形やテキスト)の描画には、「擬似透明」 を使用できます。

「擬似透明」 とは、アルファ値が 255 未満の色を薄くする処理です。これは、赤、緑、青の要素をアルファ値 1 と混合することで実現されます。

「擬似透明」 では、もちろん、上の透明レイヤーの下にある下の色のレイヤーは見えませんが、下のレイヤーが白の場合に透明であるような錯覚を生み出します。

PSドキュメントに透明画像を追加する

前述の通り、透明画像はマスクとしてPSドキュメントに追加できます。Aspose.Page for Javaライブラリには、この目的のために**addTransparentImage()**メソッドが用意されています。

このメソッドは、画像が完全に不透明か、完全に透明か、あるいは半透明かを認識します。完全に不透明な場合はaddImage()メソッドで不透明画像として追加され、完全に透明な場合はドキュメントに追加されません。半透明画像の場合はPostScript画像マスクとして追加されます。

以下の例では、addImage()addTransparentImage() を使用して PS ドキュメントに透明な画像を追加する場合の違いを示します。 白い半透明画像が見えるように、画像の下に大きな赤い四角形を配置しています。

この例では、Aspose.Page for Java ライブラリを使用して新しい PsDocument に任意の画像を追加するには、次の手順を実行します。

  1. 結果の PS ファイルの出力ストリームを作成します。
  2. デフォルトのオプションで PsSaveOptions オブジェクトを作成します。
  3. 作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 新しいグラフィックス状態を作成します。
  5. 画像ファイルから java.awt.BufferedImage を作成します。
  6. 画像に必要な変換を作成します。
  7. 画像が不透明であることが確実な場合は、完全に不透明な画像として PsDocument に追加します (addImage() メソッドを使用)。不透明であることが確実でない場合は、透明な画像として追加します (addTransparentImage() メソッドを使用)。
  8. 現在のグラフィックス状態から上位レベルのグラフィックス状態に移行します。
  9. ページを閉じます。
  10. ドキュメントを保存します。
 1// Add transparent image to PS document.
 2
 3String outputFileName = "AddTransparentImage_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7//Set page's background color to see white image on it's own transparent background
 8options.setBackgroundColor(new Color(211, 8, 48));
 9
10// Create new 1-paged PS Document
11PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
12
13//document.setPaint(new Color(211, 8, 48));
14//document.fill(new Rectangle2D.Float(0, 0, (int) options.getPageSize().getWidth(), 300));
15
16document.writeGraphicsSave();
17document.translate(20, 100);
18
19//Create an image from translucent image file
20BufferedImage image = ImageIO.read(new File(getDataDir() + "mask1.png"));
21
22//Add this image to document as usual opaque RGB image
23document.drawImage(image, new AffineTransform(1, 0, 0, 1, 100, 0), null);
24
25//Add this image to document as transparent image
26document.drawTransparentImage(image, new AffineTransform(1, 0, 0, 1, 350, 0), 255);
27
28document.writeGraphicsRestore();
29
30//Close current page
31document.closePage();
32
33//Save the document
34document.save();

PSドキュメントの透明部分の操作については、 .NET をご覧ください。

このコードを実行した結果は次のとおりです。

透明画像を追加

透明なベクターグラフィックオブジェクトの追加

Aspose.Page for Java ライブラリは、透明な図形とテキストに「疑似透明」と呼ばれる淡色化アルゴリズムを使用することを先ほど説明しました。

以下の例では、同じ色で塗りつぶされた 2 つの図形の違いを示しています。ただし、最初の図形はアルファコンポーネントなし、2 番目の図形はアルファコンポーネントありです。

 1// Apply pseudo-transparency transparent image to PS document.
 2
 3String outputFileName = "ApplyPseudoTranparency_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7//Set page's background color to see white image on it's own transparent background
 8options.setBackgroundColor(new Color(211, 8, 48));
 9
10// Create new 1-paged PS Document
11PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
12
13float offsetX = 50;
14float offsetY = 100;
15float width = 200;
16float height = 100;
17
18///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
19GeneralPath path = new GeneralPath();
20path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
21
22LinearGradientPaint opaquePaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
23        new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0), new Color(40, 128, 70)} , MultipleGradientPaint.CycleMethod.NO_CYCLE,
24        MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
25
26document.setPaint(opaquePaint);
27document.fill(path);
28
29offsetX = 350;
30
31///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
32path = new GeneralPath();
33path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
34
35LinearGradientPaint translucentPaint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(width, height),
36        new float[] {0f, 1f}, new Color[] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
37        MultipleGradientPaint.ColorSpaceType.SRGB, new AffineTransform(width, 0, 0, height, offsetX, offsetY));
38
39document.setPaint(translucentPaint);
40document.fill(path);
41
42//Close current page
43document.closePage();
44
45//Save the document
46document.save();

.NET および C++ の PS ドキュメントでの透明性の操作を参照してください。

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

擬似透明度を表示

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.