Crop EPS | C# .NET API Solution

Overview

This article explains how to crop EPS using C#. It covers the following topics.

C# 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 C# follows:

  1. Initialize PsDocument object with the input stream containing EPS file.
  2. Extract the existing bounding box of the image using static method ExtractEpsBoundingBox.
  3. Create the output stream for resulting EPS file.
  4. Create new bounding box.
  5. 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 C#

In the following C# code snippet we get existing bounding box of the image and crop EPS:

 1// Cropping EPS file.
 2
 3// Initialize PS document with EPS file
 4PsDocument document = new PsDocument(DataDir + "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(OutputDir + outputFileName, newBoundingBox);
Example-CropEPS.cs hosted with ❤ by GitHub

See Crop EPS in Java and C++.

Initial EPS Image
Initial image
Cropped EPS Image
Cropped image

Building an EPS cropping AI Agent

If you want your own AI Agent Automated Image Processing and EPS Vector Cropping based on Aspose.Page its implementation workflow may look the following way:

Rather than trying to rasterize and visually slice a file, the AI agent calculates coordinate bounding frames natively to deliver an optimized workflow.

  1. Geometry intent extraction (LLM analysis layer) The user inputs an EPS file along with natural-language layout constraints (e.g., “Crop out the bottom 50 points of white margin” or “Isolate the central 200x200 pixel artwork area”). The LLM translates the prompt into structural coordinate modifications (e.g., identifying adjustments relative to left, top, right, or bottom boundary frames).

  2. Native bounding box ingestion To prevent the LLM from hallucinating coordinates that clip outside the actual dimensions of the graphic (which throws a compiler exception), the agent queries the original document attributes. The system loads the target asset via PsDocument and invokes document.ExtractEpsBoundingBox(). This array ([x0, y0, x1, y1]) acts as a protective boundary constraint for the AI: Constraint Loop: initialBoundingBox[0] ≤ newBoundingBox[0] ≤ initialBoundingBox[2]

  3. Coordinate recalculation and calibration (Mapping layer) The agent passes the extracted boundary array and the user’s structural prompt to its internal calculation matrix. Then it computes the target coordinate parameters float[] newBoundingBox. If the user asks for center-focus alignment, the AI calculates the geometric center relative to the original parameters and offsets the bounding values uniformly.

  4. Metadata patching and asset serialization (Execution node) The calibrated array is written directly to the document structure without initializing a complete, resource-heavy canvas re-render. The system executes the native document.CropEps (outputPath, newBoundingBox) command. By updating only the PostScript page descriptions, the system delivers a perfectly clipped file instantly to the endpoint interface.

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.