Get Draw Object Boundaries with Aspose.Cells

Introduction

Are you ready to dive into the world of creating, manipulating, and extracting information from Excel spreadsheets using Aspose.Cells for .NET? In today’s tutorial, we’ll explore how to get the boundaries of drawing objects in an Excel file by utilizing the capabilities of Aspose.Cells. Whether you’re a developer looking to enhance your applications with Excel-related functionalities or simply eager to learn a new skill, you’ve come to the right place!

Prerequisites

Before we jump into coding, there are a few prerequisites you need to get your hands on:

  1. Visual Studio: Make sure you have Visual Studio installed on your computer. You can use any version you prefer.
  2. Aspose.Cells for .NET: Download and install Aspose.Cells from the download link. A free trial is also available here.
  3. Basic Knowledge of C#: Familiarity with C# programming will be beneficial. If you’re new, don’t worry! We’ll guide you through each step.

Once you have your environment set up, we’ll move on to the necessary packages.

Import Packages

Before utilizing the classes provided by Aspose.Cells, you need to import the necessary namespaces in your C# project. Here’s how you do it:

  1. Open your Visual Studio project.
  2. At the top of your C# file, add the following using directives:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells.Rendering;

With the packages imported, you’re now fully equipped to start working with Excel files.

Let’s break this down into manageable steps. We’ll be creating a class that captures the draw object boundaries and prints them out in a console application.

Step 1: Create a Draw Object Event Handler Class

First, you need to create a class that extends the DrawObjectEventHandler. This class will handle the drawing events and allow you to extract the object’s coordinates.

class clsDrawObjectEventHandler : DrawObjectEventHandler
{
    public override void Draw(DrawObject drawObject, float x, float y, float width, float height)
    {
        Console.WriteLine("");

        //Print the coordinates and the value of Cell object
        if (drawObject.Type == DrawObjectEnum.Cell)
        {
            Console.WriteLine("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Cell Value]: " + drawObject.Cell.StringValue);
        }

        // Print the coordinates and the shape name of Image object
        if (drawObject.Type == DrawObjectEnum.Image)
        {
            Console.WriteLine("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Shape Name]: " + drawObject.Shape.Name);
        }

        Console.WriteLine("----------------------");
    }
}
  • In this class, we override the Draw method, which gets called whenever a drawing object is encountered.
  • We check the type of DrawObject. If it’s a Cell, we log its position and value. If it’s an Image, we log its position and name.

Step 2: Set Input and Output Directories

Next, you need to specify where your Excel document is located and where to save the output PDF.

// Source directory
string sourceDir = "Your Document Directory";

// Output directory
string outputDir = "Your Document Directory";
  • Replace "Your Document Directory" with the path to your actual document. Ensure you have a sample Excel file named "sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx" stored in this directory.

Step 3: Load the Sample Excel File

With the directories set, we can now load the Excel file into an instance of the Workbook class.

// Load sample Excel file
Workbook wb = new Workbook(sourceDir + "sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx");
  • This code initializes a workbook instance with your sample Excel file.

Step 4: Specify PDF Save Options

Now that we have our workbook loaded, we’ll need to define how we want to save our output as a PDF file.

// Specify Pdf save options
PdfSaveOptions opts = new PdfSaveOptions();

Step 5: Assign the Event Handler

It’s crucial to assign the DrawObjectEventHandler instance to our PDF save options. This step will ensure that our custom event handler processes each drawing object.

// Assign the instance of DrawObjectEventHandler class
opts.DrawObjectEventHandler = new clsDrawObjectEventHandler();

Step 6: Save the Workbook as a PDF

Finally, it’s time to save our workbook as a PDF and execute the operation.

// Save to Pdf format with Pdf save options
wb.Save(outputDir + "outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts);
  • This code saves the workbook as a PDF file in the specified output directory, applying our save options to ensure our draw objects are processed.

Step 7: Display Success Message

Last but not least, we’ll display a success message to the console after the operation is completed.

Console.WriteLine("GetDrawObjectAndBoundUsingDrawObjectEventHandler executed successfully.");

Conclusion

And there you have it! With just a few steps, you can get draw object boundaries from an Excel file using Aspose.Cells for .NET. So whether you’re building a reporting tool, need to automate document handling, or simply want to explore the power of Aspose.Cells, this guide has set you on the right path.

FAQ’s

What is Aspose.Cells?

Aspose.Cells is a powerful library designed for working with Excel files in .NET applications, allowing for creating, editing, and converting spreadsheets.

Can I try Aspose.Cells for free?

Yes! You can download a free trial of Aspose.Cells here.

What file formats does Aspose.Cells support?

Aspose.Cells supports various formats, including XLSX, XLS, CSV, PDF, and more.

Where can I find more examples of using Aspose.Cells?

You can explore more examples and detailed documentation on their site at Aspose.Cells Documentation.

How can I get support for Aspose.Cells?

For support, visit the Aspose Forum where you can ask questions and get assistance from the community.