Get DrawObject and Bound while rendering to PDF with C++ using DrawObjectEventHandler class

Possible Usage Scenarios

Aspose.Cells provides an abstract class DrawObjectEventHandler which has a Draw() method. The user can implement DrawObjectEventHandler and utilize the Draw() method to get the DrawObject and Bound while rendering Excel to PDF or Image. Here is a brief description of the parameters of the Draw() method.

If you are rendering an Excel file to PDF, then you can utilize DrawObjectEventHandler class with PdfSaveOptions.PaginatedSaveOptions(PaginatedSaveOptions_Impl impl)*. Similarly, if you are rendering an Excel file to Image, you can utilize DrawObjectEventHandler class with ImageOrPrintOptions.DrawObjectEventHandler.

Get DrawObject and Bound while rendering to Pdf using DrawObjectEventHandler class

Please see the following sample code. It loads the sample Excel file and saves it as output PDF. While rendering to PDF, it utilizes PdfSaveOptions.PaginatedSaveOptions(PaginatedSaveOptions_Impl impl)* property and captures the DrawObject and Bound of existing cells and objects e.g. images etc. If the DrawObject type is Cell, it prints its Bound and StringValue. And if the DrawObject type is Image, it prints its Bound and Shape Name. Please see the console output of the sample code given below for more help.

Sample Code

#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Rendering;

class ClsDrawObjectEventHandler : public DrawObjectEventHandler
{
public:
    void Draw(DrawObject& drawObject, float x, float y, float width, float height) override
    {
        std::cout << std::endl;

        if (drawObject.GetType() == DrawObjectEnum::Cell)
        {
            std::cout << "[X]: " << x << " [Y]: " << y << " [Width]: " << width << " [Height]: " << height 
                      << " [Cell Value]: " << drawObject.GetCell().GetStringValue().ToUtf8() << std::endl;
        }

        if (drawObject.GetType() == DrawObjectEnum::Image)
        {
            std::cout << "[X]: " << x << " [Y]: " << y << " [Width]: " << width << " [Height]: " << height 
                      << " [Shape Name]: " << drawObject.GetShape().GetName().ToUtf8() << std::endl;
        }

        std::cout << "----------------------" << std::endl;
    }
};

void Run()
{
    Workbook wb(u"sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx");
    PdfSaveOptions opts;
    auto drawObjectEventHandler = std::make_shared<ClsDrawObjectEventHandler>();
    opts.SetDrawObjectEventHandler(drawObjectEventHandler.get());
    wb.Save(u"outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts);
}

int main()
{
    Aspose::Cells::Startup();
    Run();
    Aspose::Cells::Cleanup();
    return 0;
}

Console Output

[X]: 153.6035 [Y]: 82.94118 [Width]: 103.2035 [Height]: 14.47059 [Cell Value]: This is sample text.

----------------------

[X]: 267.6917 [Y]: 153.4853 [Width]: 160.4491 [Height]: 128.0647 [Shape Name]: Sun

----------------------