使用 Java API 解决方案裁剪 EPS
Contents
[
Hide
Show
]概述
本文讲解如何使用 Java 裁剪 EPS 文件。内容涵盖以下主题:
Java 裁剪 EPS 文件说明
裁剪图像的操作会改变图像的一个或多个边距:左、右、上、下。换句话说,它会从边缘剪切图像的内容。 因此,最终的 EPS 图像大小将始终小于原始图像。由于我们的解决方案不处理内容,而是处理 EPS 文件的头文件,因此文件的体积不会改变。 EPS 图像的边距由 BoundingBox 元数据定义。为了进行裁剪,或者说设置新的边界框,我们需要知道 EPS 图像现有的边界框。 否则,我们可能会将新边界框的值赋给现有边界框之外,从而导致错误。 因此,在 Java 中裁剪 EPS 图像的步骤如下:
- 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
- 使用静态方法 extractEpsBoundingBox 提取图像的现有边界框。
- 为生成的 EPS 文件创建输出流。
- 创建新的边界框。
- 使用静态方法 cropEps 裁剪带有新边界框的 PsDocument 对象。
您可以通过免费在线Crop EPS检查 Aspose.Page EPS Crop 的质量并查看结果,然后使用我们的EPS Viewer Web 应用程序查看生成的 EPS 文件。
在 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 网页应用程序上在线评估裁剪 EPS 的效果。您可以在几秒钟内裁剪 EPS 文件并下载结果。
您可以从 GitHub 下载示例和数据文件。