DrawObjectEventHandlerクラスを使用して、ノード.js経由でC++で描画中のDrawObjectとBoundを取得する

可能な使用シナリオ

Aspose.Cellsは、DrawObjectEventHandlerという抽象クラスを提供し、DrawObjectEventHandler.draw(DrawObject, number, number, number, number)メソッドを持ちます。ユーザーはDrawObjectEventHandlerを実装し、ExcelをPDFまたは画像にレンダリングする際にDrawObjectEventHandler.draw(DrawObject, number, number, number, number)メソッドを利用してDrawObjectとBoundを取得できます。以下はDrawObjectEventHandler.draw(DrawObject, number, number, number, number)メソッドのパラメータの概要です。

ExcelファイルをPDFへレンダリングする場合、DrawObjectEventHandlerクラスとPdfSaveOptions.getDrawObjectEventHandler()を利用できます。同様に、Excelファイルを画像にレンダリングする場合は、DrawObjectEventHandlerクラスとImageOrPrintOptions.getDrawObjectEventHandler()を利用できます。

DrawObjectEventHandlerクラスを使用してPDFへのレンダリング中にDrawObjectとバインドを取得

次のサンプルコードを参照してください。これは、サンプルExcelファイル([64716821.xlsx])を読み込み、出力PDFとして保存します。PDFへのレンダリング中に、PdfSaveOptions.getDrawObjectEventHandler()プロパティを利用し、既存のセルや画像などのオブジェクトのDrawObjectとBoundを取得します。DrawObjectタイプがCellの場合、BoundとStringValueを出力します。DrawObjectタイプがImageの場合、BoundとShape Nameを出力します。詳細は以下のサンプルコードのコンソール出力をご参照ください。

サンプルコード

const AsposeCells = require("aspose.cells.node");

class ClsDrawObjectEventHandler extends AsposeCells.DrawObjectEventHandler {
draw(drawObject, x, y, width, height) {
console.log("");

// Print the coordinates and the value of Cell object
if (drawObject.getType() === AsposeCells.DrawObjectEnum.Cell) {
console.log(`[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() === AsposeCells.DrawObjectEnum.Image) {
console.log(`[X]: ${x} [Y]: ${y} [Width]: ${width} [Height]: ${height} [Shape Name]: ${drawObject.getShape().getName()}`);
}

console.log("----------------------");
}
}

async function run() {
// Load sample Excel file
const workbook = new AsposeCells.Workbook("sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx");

// Specify Pdf save options
const opts = new AsposeCells.PdfSaveOptions();

// Assign the instance of DrawObjectEventHandler class
opts.setDrawObjectEventHandler(new ClsDrawObjectEventHandler());

// Save to Pdf format with Pdf save options
await workbook.saveAsync("outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts);
}

run();

コンソール出力

 [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

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