JavaScript経由のDrawObjectEventHandlerクラスを使用して、描画時にDrawObjectと境界を取得する方法を学ぶ。
可能な使用シナリオ
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)メソッドのパラメータの概要です。
-
drawObject: DrawObjectはレンダリング時に初期化され返されます。
-
x: DrawObjectの左端。
-
y: DrawObjectの上端。
-
width: DrawObjectの幅。
-
height: DrawObjectの高さ。
ExcelファイルをPDFに変換する際には、DrawObjectEventHandlerクラスとそのPdfSaveOptions.drawObjectEventHandlerプロパティを活用できます。同様に、Excelファイルを画像に変換する場合は、DrawObjectEventHandlerクラスとImageOrPrintOptions.drawObjectEventHandlerプロパティを活用します。
DrawObjectEventHandlerクラスを使用してPDFへのレンダリング中にDrawObjectとバインドを取得
次のサンプルコードを参照してください。これは、サンプルExcelファイル([64716821.xlsx])を読み込み、出力PDFとして保存します。PDFへのレンダリング中に、PdfSaveOptions.drawObjectEventHandlerプロパティを利用し、既存のセルや画像などのオブジェクトのDrawObjectとBoundを取得します。DrawObjectタイプがCellの場合、BoundとStringValueを出力します。DrawObjectタイプがImageの場合、BoundとShape Nameを出力します。詳細は以下のサンプルコードのコンソール出力をご参照ください。
サンプルコード
<!DOCTYPE html>
<html>
<head>
<title>Get Draw Object and Bound Using DrawObjectEventHandler</title>
</head>
<body>
<h1>Get Draw Object and Bound Using DrawObjectEventHandler</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, PdfSaveOptions, DrawObjectEventHandler, DrawObjectEnum } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
class ClsDrawObjectEventHandler extends DrawObjectEventHandler {
draw(drawObject, x, y, width, height) {
console.log("");
// Print the coordinates and the value of Cell object
if (drawObject.type === DrawObjectEnum.Cell) {
console.log(`[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.log(`[X]: ${x} [Y]: ${y} [Width]: ${width} [Height]: ${height} [Shape Name]: ${drawObject.shape.name}`);
}
console.log("----------------------");
}
}
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load sample Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Specify Pdf save options
const opts = new PdfSaveOptions();
// Assign the instance of DrawObjectEventHandler class
opts.drawObjectEventHandler = new ClsDrawObjectEventHandler();
// Save to Pdf format with Pdf save options
const outputData = workbook.save(SaveFormat.Pdf, opts);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the PDF file.</p>';
});
</script>
</html>
コンソール出力
[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
----------------------