Crop EPS | API Solution for C++
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:
- Initialize PsDocument object with the input stream containing EPS file.
- Extract the existing bounding box of the image using static method ExtractEpsBoundingBox.
- Create the 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 C++
In the following C++ code snippet we get existing bounding box of the image and crop EPS:
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithEPS();
3
4 //Create an input stream for EPS file
5 {
6 System::SharedPtr<System::IO::Stream> inputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"input.eps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_1({ inputEpsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Initialize PsDocument object with input stream
14 System::SharedPtr<PsDocument> doc = System::MakeObject<PsDocument>(inputEpsStream);
15
16 //Get initial bounding box of EPS image
17 System::ArrayPtr<int32_t> initialBoundingBox = doc->ExtractEpsBoundingBox();
18
19 //Create an output stream for resized EPS
20 {
21 System::SharedPtr<System::IO::Stream> outputEpsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"output_crop.eps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
22 // Clearing resources under 'using' statement
23 System::Details::DisposeGuard<1> __dispose_guard_0({ outputEpsStream});
24 // ------------------------------------------
25
26 try
27 {
28 //Create new bounding box
29 //Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
30 System::ArrayPtr<float> newBoundingBox = System::MakeArray<float>({260, 300, 480, 432});
31
32 //Crop EPS image and save to the output stream
33 //Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
34 //initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
35 //initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
36 //initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
37 //initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
38 doc->CropEps(outputEpsStream, newBoundingBox);
39 }
40 catch(...)
41 {
42 __dispose_guard_0.SetCurrentException(std::current_exception());
43 }
44 }
45 }
46 catch(...)
47 {
48 __dispose_guard_1.SetCurrentException(std::current_exception());
49 }
50 }
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.