DrawObjectEventHandler クラスを使用して PDF へのレンダリング中に DrawObject と Bound を取得する
考えられる使用シナリオ
Aspose.Cells は抽象クラスを提供しますDrawObjectEventHandlerを持っている[描く()](https://reference.aspose.com/cells/java/com.aspose.cells/drawobjecteventhandler#draw(com.aspose.cells.DrawObject,%20float,%20float,%20float,%20float)) 方法。ユーザーが実装できるDrawObjectEventHandlerを利用し、描く() メソッドを取得するDrawObjectとバウンドExcel を PDF またはイメージにレンダリングしている間。のパラメータを簡単に説明します。[描く()](https://reference.aspose.com/cells/java/com.aspose.cells/drawobjecteventhandler#draw(com.aspose.cells.DrawObject,%20float,%20float,%20float,%20float)) 方法。
-
描画オブジェクト:DrawObjectレンダリング時に初期化されて返されます
-
x: 左DrawObject
-
y: 上DrawObject
-
幅: の幅DrawObject
-
高さ: の高さDrawObject
ExcelファイルをPDFにレンダリングしている場合は、利用できますDrawObjectEventHandlerクラスPdfSaveOptions.DrawObjectEventHandler.同様に、Excel ファイルを画像にレンダリングする場合は、DrawObjectEventHandlerクラスImageOrPrintOptions.DrawObjectEventHandler.
DrawObjectEventHandler クラスを使用して Pdf へのレンダリング中に DrawObject と Bound を取得する
以下のサンプルコードをご覧ください。それはサンプル Excel ファイルそしてそれをとして保存します出力 PDFPDFにレンダリングしながら、それは利用しますPdfSaveOptions.DrawObjectEventHandlerプロパティとキャプチャDrawObjectとバウンド画像などの既存のセルとオブジェクトの。 drawObject タイプが Cell の場合、その Bound と StringValue を出力します。また、drawObject タイプが Image の場合、その Bound および Shape Name を出力します。詳細については、以下のサンプル コードのコンソール出力を参照してください。
サンプルコード
// 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
\----------------------