在使用DrawObjectEventHandler类呈现到PDF时获取绘图对象和边界
可能的使用场景
Aspose.Cells提供了一个抽象类 DrawObjectEventHandler,其中有一个 Draw() 方法。用户可以实现 DrawObjectEventHandler 并利用 Draw() 方法来获取渲染Excel为PDF或图像时的 DrawObject 和 Bound。以下是对 Draw() 方法参数的简要描述。
-
drawObject: 在呈现时会初始化并返回 DrawObject
-
x:DrawObject的左边界
-
y:DrawObject的顶部
-
width:DrawObject的宽度
-
height:DrawObject的高度
如果要将Excel文件呈现为PDF,则可以利用 DrawObjectEventHandler 类结合 PdfSaveOptions.DrawObjectEventHandler。同样,如果要将Excel文件呈现为图像,则可以利用 DrawObjectEventHandler 类结合 ImageOrPrintOptions.DrawObjectEventHandler。
在使用DrawObjectEventHandler类呈现PDF时获取绘图对象和边界
请参阅以下示例代码。它加载 示例Excel文件 并将其保存为 输出PDF。在呈现为PDF时,它利用 PdfSaveOptions.DrawObjectEventHandler 属性来捕获现有单元格和对象(例如图像等)的 DrawObject 和 Bound。如果 DrawObject 类型是单元格,则打印其 Bound 和 StringValue。如果 DrawObject 类型是图像,则打印其 Bound 和形状名称。请参见下方所提供的示例代码的控制台输出以获取更多帮助。
示例代码
// 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
----------------------