Obtener DrawObject y Bound al representar a PDF usando la clase DrawObjectEventHandler

Escenarios de uso posibles

Aspose.Cells proporciona una clase abstracta DrawObjectEventHandler que tiene un método draw(). El usuario puede implementar DrawObjectEventHandler y utilizar el método draw() para obtener el DrawObject y Bound al renderizar Excel a PDF o imagen. Aquí hay una breve descripción de los parámetros del método draw().

Si está renderizando un archivo de Excel a PDF, entonces puede utilizar la clase DrawObjectEventHandler con PdfSaveOptions.DrawObjectEventHandler. Del mismo modo, si está renderizando un archivo de Excel a imagen, puede utilizar la clase DrawObjectEventHandler con ImageOrPrintOptions.DrawObjectEventHandler.

Obtener DrawObject y Bound al representar a PDF utilizando la clase DrawObjectEventHandler

Consulte el siguiente código de ejemplo. Carga el archivo de Excel de muestra y lo guarda como PDF de salida. Al renderizar a PDF, utiliza la propiedad PdfSaveOptions.DrawObjectEventHandler y captura el DrawObject y Bound de celdas existentes y objetos, como imágenes, etc. Si el tipo de drawObject es Cell, imprime su Bound y StringValue. Y si el tipo de drawObject es Image, imprime su Bound y Nombre de forma. Consulte la salida de la consola del código de muestra a continuación para obtener más ayuda.

Código de muestra

// 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.");
}
}

Salida de la consola

[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

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