Получить объект DrawObject и Bound при рендеринге в PDF с использованием класса DrawObjectEventHandler
Возможные сценарии использования
Aspose.Cells предоставляет абстрактный класс DrawObjectEventHandler, который содержит метод Draw(). Пользователь может реализовать DrawObjectEventHandler и использовать метод Draw() для получения DrawObject и границ при рендеринге Excel в PDF или изображение. Вот краткое описание параметров метода Draw().
-
drawObject: объект DrawObject будет инициализирован и возвращен при рендеринге
-
x: слева от DrawObject
-
y: сверху DrawObject
-
ширина: ширина DrawObject
-
высота: высота DrawObject
Если вы рендерите файл Excel в PDF, вы можете использовать класс DrawObjectEventHandler с PdfSaveOptions.DrawObjectEventHandler. Аналогично, если вы рендерите файл Excel в изображение, вы можете использовать класс DrawObjectEventHandler с ImageOrPrintOptions.DrawObjectEventHandler.
Получите объект DrawObject и Bound при рендеринге в Pdf с использованием класса DrawObjectEventHandler
Пожалуйста, посмотрите пример кода ниже. Он загружает образец файла Excel и сохраняет его в выходной PDF. При рендеринге в PDF используется свойство PdfSaveOptions.DrawObjectEventHandler и захватываются DrawObject и границы существующих ячеек и объектов, например изображений и т. д. Если тип DrawObject - ячейка, он выводит ее границы и StringValue. И если тип DrawObject - изображение, он выводит его границы и имя формы. Смотрите вывод консоли примера кода ниже для получения более подробной информации.
Образец кода
// 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); | |
} | |
} | |
} |
Вывод в консоль
[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
----------------------