Crop EPS using Java API Solution
Overview
This article explains how to crop EPS using Java. It covers the following topics.
Java Crop EPS Description
Croping the image is an operation that changes one of the or several margins of the image: left, right, top and bottom. In other words, it cuts the content of the image from the edges. Thus, a size of the resulting representation of EPS image will always be less than original. The volume of the file will not be changed as our solution doesnt’t work with the content but works with the header of EPS file. The margins of EPS image is defined by BoundingBox metadata. In order to crop, or in other words, to set up new bounding box, we should to know existing bounding box of EPS image. Otherwise we can assign the values of new bounding box outside of the existing bounding box that results in error. So the steps for cropping EPS image in Java follows:
- Initialize PsDocument object with an input stream containing EPS file.
- Extract the existing bounding box of the image using static method extractEpsBoundingBox.
- Create an output stream for resulting EPS file.
- Create new bounding box.
- Crop PsDocument object with new bounding box with the static method cropEps.
You can check the quality of Aspose.Page EPS Crop and view the results via free online Crop EPS and then view the resulting EPS file with our EPS Viewer web application.
Crop EPS in Java
In the following Java code snippet we get existing bounding box of the image and crop 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);
See Crop EPS in .NET and C++.
Initial image
Cropped image
Evaluate cropping EPS online on our Crop EPS web application. You can crop EPS file and dowload result in a few seconds.
You can download examples and data files from GitHub.