调整 EPS 大小并裁剪

用于管理 EPS 图像大小的 Java 原生 API 解决方案

 

释放我们 Java API 解决方案的力量,轻松裁剪和调整 EPS 文件的大小。我们的技术允许您精确地修剪和增强视觉效果,调整边距以创建完美的图像。生成的 EPS 图像总是更加引人入胜,而最棒的部分是什么?您的文件大小保持不变。

裁剪图像是一项精确的操作,它可以修剪图像的边距(左、右、上和下),删除边缘多余的内容。这会产生更紧凑的 EPS 图像,其大小总是比原始图像小。值得注意的是,我们的解决方案通过专门关注 EPS 文件头来保持文件大小不变,而让您的内容保持完整。EPS 图像的边距由 BoundingBox 元数据决定。要重新定义这些边距并建立新的边界框 (bounding box),了解 EPS 图像现有的边界框至关重要。这可以确保无缝裁剪和完美的结果。

要调整 EPS 大小或裁剪,请先获取:

  • Aspose.Page for Java API,这是一个功能丰富、强大且易于使用的 Java 平台文档操作和转换 API。

  • 您可以直接从 Maven 下载其最新版本,并通过在 pom.xml 中添加以下配置将其安装到基于 Maven 的项目中。

Package Manager Console Command


    <repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
    </repository>

Dependency

    <dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>version of aspose-page API</version>
    <classifier>jdk17</classifier>
    </dependency>

在 Java 中裁剪 EPS 图像的步骤。

下面的示例显示了如何使用所选单位(点)去除 .eps 中的白色边距。顺序如下:

  1. 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
  2. 使用 extractEpsBoundingBox() 静态方法定义图像现有的边界框。
  3. 为生成的 EPS 文件创建输出流。
  4. 创建一个新的边界框。
  5. 使用 cropEps() 静态方法用新的边界框剪掉 EPS 文件多余的边界。
  6. 保存结果文件。

用于裁剪 EPS 图像的 Java 代码。

    // Set the path to the documents directory.
    String dataDir = getDataDir();

    //Create an input stream for an EPS file
    FileInputStream inputEpsStream = new FileInputStream(dataDir + "input.eps");

    //Initialize the PsDocument object with input stream
    PsDocument doc = new PsDocument(inputEpsStream);

    //Get the initial bounding box of EPS image
    int [] initialBoundingBox = doc.extractEpsBoundingBox();

    //Create an output stream for the PostScript document
    FileOutputStream outputEpsStream = new FileOutputStream(dataDir + "output_crop.eps");

    //Create a new bounding box
    //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
    float[] newBoundingBox = new float[] { 260, 300, 480, 432 };

    //Crop the EPS image and save to the output stream                   
    //cropping of the image is changing its bounding box so that the new values of the bounding box will be within the initial bounding box, that is
    //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
    //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
    //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
    //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
    doc.cropEps(outputEpsStream, newBoundingBox);



常问问题

1. 是否可以更改 EPS 文件的大小?

EPS 文件本质上是封装在边界框内的 PostScript 文件,可以通过修改边界框参数来调整它们的大小,以指定所需的图像区域。此过程有效地修剪掉 EPS 图像中任何不需要的区域或使它们更大。

2. 如何调整 Encapsulated PostScript 文件的大小?

Aspose.Page 允许您使用 ExtractEpsSize() 静态方法设置 EPS 文件的新大小,以获取实际文件的大小,并使用 ResizeEps() 静态方法使用新大小(以磅为单位)调整其大小。

3. 是否有专门设计用于更改 EPS(封装 PostScript)文件大小的工具或软件?

Aspose.Page 具有 EPS Resize 跨平台应用程序,可让您在一秒钟内在线调整文件大小。

EPS What is EPS File Format

EPS(Encapsulated PostScript)是一种基于 PostScript 的单页描述格式,适用于矢量图形和矢量‑光栅混合图像。导入后文件不可编辑,建议转换为可编辑格式(如 SVG 或 PDF)后使用。