DrawObjectEventHandler クラスを使用して PDF にレンダリングする際の DrawObject および Bound を取得

可能な使用シナリオ

Aspose.Cellsは、DrawObjectEventHandler抽象クラスを提供しており、Draw()メソッドがあります。ユーザーはDrawObjectEventHandlerを実装し、ExcelをPDFや画像にレンダリングする際にDraw()メソッドを利用してDrawObjectとバウンドを取得できます。以下に、Draw()メソッドのパラメータの簡単な説明を示します。

ExcelファイルをPDFにレンダリングする場合は、DrawObjectEventHandlerクラスとPdfSaveOptions.DrawObjectEventHandlerと組み合わせて使用できます。同様に、Excelファイルをイメージにレンダリングする場合は、DrawObjectEventHandlerクラスとImageOrPrintOptions.DrawObjectEventHandlerと組み合わせて使用できます。

DrawObjectEventHandler クラスを使用して PDF にレンダリングする際の DrawObject と Bound を取得

以下のサンプルコードをご覧ください。サンプル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

----------------------