Get DrawObject and Bound while rendering to PDF 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 Excel file to PDF, then you can utilize DrawObjectEventHandler class with PdfSaveOptions.DrawObjectEventHandler. Similarly, if you are rendering 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.DrawObjectEventHandler 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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
import com.aspose.cells.*;
import AsposeCellsExamples.Utils;
public class GetDrawObjectAndBoundUsingDrawObjectEventHandler {
static String srcDir = Utils.Get_SourceDirectory();
static String outDir = Utils.Get_OutputDirectory();
//Implement the concrete class of DrawObjectEventHandler
class clsDrawObjectEventHandler extends DrawObjectEventHandler
{
public void draw(DrawObject drawObject, float x, float y, float width, float height)
{
System.out.println();
//Print the coordinates and the value of Cell object
if (drawObject.getType() == DrawObjectEnum.CELL)
{
System.out.println("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Cell Value]: " + drawObject.getCell().getStringValue());
}
//Print the coordinates and the shape name of Image object
if (drawObject.getType() == DrawObjectEnum.IMAGE)
{
System.out.println("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Shape Name]: " + drawObject.getShape().getName());
}
System.out.println("----------------------");
}
}
void Run() throws Exception
{
//Load sample Excel file
Workbook wb = new Workbook(srcDir + "sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx");
//Specify Pdf save options
PdfSaveOptions opts = new PdfSaveOptions();
//Assign the instance of DrawObjectEventHandler class
opts.setDrawObjectEventHandler(new clsDrawObjectEventHandler());
//Save to Pdf format with Pdf save options
wb.save(outDir + "outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts);
}
public static void main(String[] args) throws Exception {
System.out.println("Aspose.Cells for Java Version: " + CellsHelper.getVersion());
new GetDrawObjectAndBoundUsingDrawObjectEventHandler().Run();
// Print the message
System.out.println("GetDrawObjectAndBoundUsingDrawObjectEventHandler executed successfully.");
}
}

Console Output

[X]: 153.60349 [Y]: 82.94118 [Width]: 103.203476 [Height]: 14.470589 [Cell Value]: This is sample text.

\----------------------

[X]: 267.28854 [Y]: 153.12354 [Width]: 161.25542 [Height]: 128.78824 [Shape Name]: Sun

\----------------------