DrawObjectEventHandler クラスを使用して PDF にレンダリングする際の DrawObject および Bound を取得
可能な使用シナリオ
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 にレンダリングする際の DrawObject と Bound を取得
以下はサンプルコードです。サンプル Excel ファイル を読み込み、それを出力 PDF として保存します。PDF へのレンダリング時に、PdfSaveOptions.DrawObjectEventHandler プロパティを利用して既存のセルや画像などの DrawObject と Bound をキャプチャします。drawObject タイプが Cell の場合、その Bound と StringValue が表示されます。drawObject タイプが Image の場合、その Bound と Shape 名が表示されます。詳細については、以下のサンプルコードのコンソール出力をご覧ください。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
import com.aspose.cells.*; | |
import AsposeCellsExamples.Utils; | |
public class GetDrawObjectAndBoundUsingDrawObjectEventHandler { | |
static String srcDir = Utils.Get_SourceDirectory(); | |
static String outDir = Utils.Get_OutputDirectory(); | |
//Implement the concrete class of DrawObjectEventHandler | |
class clsDrawObjectEventHandler extends DrawObjectEventHandler | |
{ | |
public void draw(DrawObject drawObject, float x, float y, float width, float height) | |
{ | |
System.out.println(); | |
//Print the coordinates and the value of Cell object | |
if (drawObject.getType() == DrawObjectEnum.CELL) | |
{ | |
System.out.println("[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() == DrawObjectEnum.IMAGE) | |
{ | |
System.out.println("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Shape Name]: " + drawObject.getShape().getName()); | |
} | |
System.out.println("----------------------"); | |
} | |
} | |
void Run() throws Exception | |
{ | |
//Load sample Excel file | |
Workbook wb = new Workbook(srcDir + "sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx"); | |
//Specify Pdf save options | |
PdfSaveOptions opts = new PdfSaveOptions(); | |
//Assign the instance of DrawObjectEventHandler class | |
opts.setDrawObjectEventHandler(new clsDrawObjectEventHandler()); | |
//Save to Pdf format with Pdf save options | |
wb.save(outDir + "outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts); | |
} | |
public static void main(String[] args) throws Exception { | |
System.out.println("Aspose.Cells for Java Version: " + CellsHelper.getVersion()); | |
new GetDrawObjectAndBoundUsingDrawObjectEventHandler().Run(); | |
// Print the message | |
System.out.println("GetDrawObjectAndBoundUsingDrawObjectEventHandler executed successfully."); | |
} | |
} |
コンソール出力
[X]: 153.60349 [Y]: 82.94118 [Width]: 103.203476 [Height]: 14.470589 [Cell Value]: This is sample text.
\----------------------
[X]: 267.28854 [Y]: 153.12354 [Width]: 161.25542 [Height]: 128.78824 [Shape Name]: Sun
\----------------------