Java API ソリューションを使用した EPS の切り抜き
概要
この記事では、Java を使用して EPS をトリミングする 方法について説明します。以下のトピックを取り上げます。
Java Crop EPS の説明
画像の切り取りとは、画像の上下左右の余白のいずれか、または複数を変更する操作です。つまり、画像の端からコンテンツを切り取ることになります。 そのため、結果として得られる EPS 画像のサイズは、常に元の画像よりも小さくなります。このソリューションはコンテンツではなく EPS ファイルのヘッダーに基づいて動作するため、ファイルサイズは変更されません。 EPS 画像の余白は、BoundingBox メタデータによって定義されます。切り取り、つまり新しい境界ボックスを設定するには、EPS 画像の既存の境界ボックスを把握しておく必要があります。 そうしないと、新しい境界ボックスの値が既存の境界ボックスの外側に設定され、エラーが発生する可能性があります。 したがって、Java で EPS 画像をトリミングする手順は以下のとおりです。
- EPS ファイルを含む入力ストリームを使用して PsDocument オブジェクトを初期化します。
- 静的メソッド extractEpsBoundingBox を使用して、画像の既存のバウンディングボックスを抽出します。
- 結果の EPS ファイルの出力ストリームを作成します。
- 新しいバウンディングボックスを作成します。
- 静的メソッド cropEps を使用して、PsDocument オブジェクトを新しい境界ボックスで切り取ります。
Aspose.Page EPS Crop の品質をチェックし、無料のオンライン Crop EPS で結果を表示し、その後、結果の EPS ファイルを弊社の EPS Viewer Web アプリケーションで表示できます。
Java で EPS をトリミングする
次の Java コードスニペットでは、 画像の既存の境界ボックスを取得し、EPS をトリミングします。
1// Cropping EPS file.
2
3// Initialize PS document with EPS file
4PsDocument document = new PsDocument(getDataDir() + "input.eps");
5
6String outputFileName = "output_crop.eps";
7
8//Get initial bounding box of EPS image
9int[] initialBoundingBox = document.extractEpsBoundingBox();
10
11//Create new bounding box
12//Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
13float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
14
15//Crop EPS image and save to the output stream
16//Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
17//initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
18//initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
19//initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
20//initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
21document.cropEps(getOutputDir() + outputFileName, newBoundingBox);.NET および C++ での EPS の切り抜きを参照してください。

初期画像
トリミングされた画像
EPS 切り抜き Web アプリケーション で、オンラインで EPS の切り抜きをお試しください。数秒で EPS ファイルの切り抜きと結果のダウンロードが可能です。
サンプルファイルとデータファイルは GitHub からダウンロードできます。