使用 DrawObjectEventHandler 类渲染到 PDF 时获取 DrawObject 和 Bound
可能的使用场景
Aspose.Cells 提供抽象类绘图对象事件处理器它有一个[画()](https://reference.aspose.com/cells/java/com.aspose.cells/drawobjecteventhandler#draw(com.aspose.cells.DrawObject,%20float,%20float,%20float,%20float)) 方法。用户可以实施绘图对象事件处理器并利用[画()](https://reference.aspose.com/cells/java/com.aspose.cells/drawobjecteventhandler#draw(com.aspose.cells.DrawObject,%20float,%20float,%20float,%20float)方法获取绘图对象和边界在将 Excel 渲染为 PDF 或图像时。下面简单介绍一下参数[画()](https://reference.aspose.com/cells/java/com.aspose.cells/drawobjecteventhandler#draw(com.aspose.cells.DrawObject,%20float,%20float,%20float,%20float)) 方法。
如果您将 Excel 文件渲染到 PDF,那么您可以利用绘图对象事件处理器类PdfSaveOptions.DrawObjectEventHandler.同样,如果您将 Excel 文件渲染为图像,您可以使用绘图对象事件处理器类ImageOrPrintOptions.DrawObjectEventHandler.
使用 DrawObjectEventHandler 类在渲染到 Pdf 时获取 DrawObject 和 Bound
请参阅以下示例代码。它加载了示例 Excel 文件并将其另存为输出 PDF.在渲染到 PDF 时,它利用PdfSaveOptions.DrawObjectEventHandler财产和捕获绘图对象和边界现有的单元格和对象,例如图像等。如果 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
\----------------------