Java API ソリューションを使用した EPS の切り抜き

概要

この記事では、Java を使用して EPS をトリミングする 方法について説明します。以下のトピックを取り上げます。

Java Crop EPS の説明

画像の切り取りとは、画像の上下左右の余白のいずれか、または複数を変更する操作です。つまり、画像の端からコンテンツを切り取ることになります。 そのため、結果として得られる EPS 画像のサイズは、常に元の画像よりも小さくなります。このソリューションはコンテンツではなく EPS ファイルのヘッダーに基づいて動作するため、ファイルサイズは変更されません。 EPS 画像の余白は、BoundingBox メタデータによって定義されます。切り取り、つまり新しい境界ボックスを設定するには、EPS 画像の既存の境界ボックスを把握しておく必要があります。 そうしないと、新しい境界ボックスの値が既存の境界ボックスの外側に設定され、エラーが発生する可能性があります。 したがって、Java で EPS 画像をトリミングする手順は以下のとおりです。

  1. EPS ファイルを含む入力ストリームを使用して PsDocument オブジェクトを初期化します。
  2. 静的メソッド extractEpsBoundingBox を使用して、画像の既存のバウンディングボックスを抽出します。
  3. 結果の EPS ファイルの出力ストリームを作成します。
  4. 新しいバウンディングボックスを作成します。
  5. 静的メソッド cropEps を使用して、PsDocument オブジェクトを新しい境界ボックスで切り取ります。

Aspose.Page EPS Crop の品質をチェックし、無料のオンライン Crop EPS で結果を表示し、その後、結果の EPS ファイルを弊社の EPS Viewer Web アプリケーションで表示できます。


Java で EPS をトリミングする

次の Java コードスニペットでは、 画像の既存の境界ボックスを取得し、EPS をトリミングします

 1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
 2
 3// The path to the documents directory.
 4String dataDir = getDataDir();
 5
 6//Create an input stream for EPS file
 7FileInputStream inputEpsStream = new FileInputStream(dataDir + "input.eps");
 8
 9//Initialize PsDocument object with input stream
10PsDocument doc = new PsDocument(inputEpsStream);
11
12//Get initial bounding box of EPS image
13int [] initialBoundingBox = doc.extractEpsBoundingBox();
14
15//Create output stream for PostScript document
16FileOutputStream outputEpsStream = new FileOutputStream(dataDir + "output_crop.eps");
17
18//Create new bounding box
19//Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
20float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
21
22//Crop EPS image and save to the output stream                   
23//Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
24//initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
25//initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
26//initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
27//initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
28doc.cropEps(outputEpsStream, newBoundingBox);

.NET および C++ での EPS の切り抜きを参照してください。

初期EPS画像
初期画像
トリミングされたEPS画像
トリミングされた画像

EPS 切り抜き Web アプリケーション で、オンラインで EPS の切り抜きをお試しください。数秒で EPS ファイルの切り抜きと結果のダウンロードが可能です。

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.