表データ抽出機能のUIカスタマイズ

このサンプルでは、表データ抽出時の選択領域のグリッド線やハイライト部分の色、設定項目などを変更し、表データ抽出機能の外観をカスタマイズする方法を紹介しています。

/** * 表データ抽出機能の外観をカスタマイズしたPDFビューワの初期化 * @param {string} selector - ビューワコンテナ要素用CSSセレクタ * @param {string} pdfUrlToOpen - ロードするPDFドキュメントのURL/パス */ function loadPdfViewer(selector, pdfUrlToOpen) { const viewer = new DsPdfViewer(selector, { supportApi: getSupportApiSettings() }); // 表データ抽出機能の設定・外観をカスタマイズする viewer.options.tableDataExtraction = { // エクスポート形式の制限 (TSVおよびHTML形式のみ可) allowedExportFormats: ['tsv', 'html'], // オプション設定の変更 extractOptions: { /** * 非表示データを抽出内容に含めるかどうか * @default true */ IncludeInvisibleText: false, /** * 表データ抽出時の値比較のための類似係数 * - Default: 0.25 * - 低い値 = より異なる列・行として認識される * - 高い値 = 行列判定が寛容になる(同一判定され、行・列数が少なくなる) */ CoefPrecision: 0.9 }, // 選択領域の外観のカスタマイズ appearance: { visualEditor: { lineColor: "#4CAF50", // 緑色のグリッド線 selectionAreaColor: "rgba(76, 175, 80, 0.2)", // 薄緑色のハイライト色 emptyCellColor: "rgba(76, 175, 80, 0.9)", // 空のセルを赤色でハイライト表示 cssText: ` .extracted-table-editor .table-cell { background: rgba(100, 255, 0, 0.2); // 背景色はダーク色 opacity: 0.1; } `, selectionAreaOpacity: 1.0 // 選択領域を透過 } } }; const panelHandle = viewer.addTableExtractionPanel(); viewer.expandPanel(panelHandle); viewer.addDefaultPanels(); // PDFドキュメントをロードし、表データ抽出処理を実行する viewer.open(pdfUrlToOpen).then(function() { viewer.zoomMode = 1; // Set to fit-page view /** * あらかじめ定義された領域に対して表データ抽出を実行する * @type {number[]} selectionBounds - 選択領域に関する座標の配列 [x1, y1, x2, y2] : * - x1, y1: 左下角の座標 (PDF 座標) * - x2, y2: 右上角の座標 (PDF 座標) * 72 points = 1 inch */ const selectionBounds = [ 29.26, // 左辺 334.33, // 下辺 688.92, // 右辺 490.81 // 上辺 ]; // 先頭ページから表データ抽出を行う viewer.tableDataExtractor.extractTableData(0, selectionBounds); }); } window.onload = function() { // DsPdfViewer.LicenseKey = "your_license_key_here"; const demoPdfPath = "/diodocs/pdfviewer/demos/product-bundles/assets/pdf/table-data-one-page.pdf"; loadPdfViewer("#viewer", demoPdfPath); }
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>表データ抽出機能のUIカスタマイズ</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./src/styles.css"> <script src="/diodocs/pdfviewer/demos/product-bundles/build/dspdfviewer.js"></script> <script src="/diodocs/pdfviewer/demos/product-bundles/build/wasmSupportApi.js"></script> <script src="/diodocs/pdfviewer/demos/resource/js/init.js"></script> <script src="./src/app.js"></script> </head> <body> <div id="viewer"></div> </body> </html>
#viewer { height: 100%; }