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 mientras se representa Excel a PDF o Imagen. Aquí tienes una breve descripción de los parámetros del método Draw().
-
drawObject: DrawObject se inicializará y se devolverá al representar
-
x: Izquierda de DrawObject
-
y: Arriba de DrawObject
-
width: Ancho de DrawObject
-
height: Altura de DrawObject
Si estás representando un archivo de Excel a PDF, puedes utilizar la clase DrawObjectEventHandler con PdfSaveOptions.DrawObjectEventHandler. De manera similar, si estás representando un archivo de Excel a Imagen, puedes utilizar la clase DrawObjectEventHandler con ImageOrPrintOptions.DrawObjectEventHandler.
Obtener DrawObject y Bound al representar a PDF utilizando la clase DrawObjectEventHandler
Por favor, consulte el siguiente código de muestra. Carga el archivo de Excel de muestra y lo guarda como PDF de salida. Mientras se renderiza a PDF, utiliza la propiedad PdfSaveOptions.DrawObjectEventHandler y captura el DrawObject y Bound de las celdas y objetos existentes, por ejemplo, imágenes, etc. Si el tipo DrawObject es Celda, imprime su Bound y StringValue. Y si el tipo DrawObject es Imagen, imprime su Bound y nombre de forma. Consulte la salida de consola del código de muestra que se 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-.NET | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Aspose.Cells.Rendering; | |
namespace Aspose.Cells.Examples.CSharp.Rendering | |
{ | |
class GetDrawObjectAndBoundUsingDrawObjectEventHandler | |
{ | |
//Implement the concrete class of DrawObjectEventHandler | |
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("----------------------"); | |
} | |
} | |
public static void Run() | |
{ | |
//Load sample Excel file | |
Workbook wb = new Workbook("sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx"); | |
//Specify Pdf save options | |
PdfSaveOptions opts = new PdfSaveOptions(); | |
//Assign the instance of DrawObjectEventHandler class | |
opts.DrawObjectEventHandler = new clsDrawObjectEventHandler(); | |
//Save to Pdf format with Pdf save options | |
wb.Save("outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts); | |
} | |
} | |
} |
Salida de la consola
[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
----------------------