裁剪 EPS | C++ API 解决方案

概述

本文讲解如何使用 C++ 裁剪 EPS 文件。内容涵盖以下主题:

C++ 裁剪 EPS 文件说明

裁剪图像的操作会改变图像的一个或多个边距:左、右、上、下。换句话说,它会从边缘剪切图像的内容。

因此,最终的 EPS 图像大小将始终小于原始大小。由于我们的解决方案不处理内容,而是处理 EPS 文件的头文件,因此文件的体积不会改变。

EPS 图像的边距由 BoundingBox 元数据定义。为了进行裁剪,或者说设置新的边界框,我们需要知道 EPS 图像现有的边界框。否则,我们可能会将新边界框的值赋给现有边界框之外,从而导致错误。 因此,在 C++ 中裁剪 EPS 图像的步骤如下:

  1. 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
  2. 使用静态方法 ExtractEpsBoundingBox 提取图像的现有边界框。
  3. 为生成的 EPS 文件创建输出流。
  4. 创建新的边界框。
  5. 使用静态方法 CropEps 裁剪带有新边界框的 PsDocument 对象。

您可以通过免费在线Crop EPS检查 Aspose.Page EPS Crop 的质量并查看结果,然后使用我们的EPS Viewer Web 应用程序查看生成的 EPS 文件。


在 C++ 中裁剪 EPS

在以下 C++ 代码片段中,我们获取图像的现有边界框并裁剪 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    }

请参阅 Java.NET 中的 EPS 裁剪。

初始 EPS 图像
初始图像
裁剪后的 EPS 图像
裁剪后的图像

在我们的EPS 裁剪网页应用程序上在线评估 EPS 裁剪。您可以在几秒钟内裁剪 EPS 文件并下载结果。

您可以从 GitHub 下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.