在使用DrawObjectEventHandler类呈现到PDF时获取绘图对象和边界
可能的使用场景
Aspose.Cells提供了一个抽象类DrawObjectEventHandler,其中有一个draw()方法。用户可以实现DrawObjectEventHandler并利用draw()方法来获取DrawObject和Bound,同时在将Excel渲染为PDF或图像时。以下是draw()方法参数的简要描述。
-
drawObject: 在呈现时将初始化并返回DrawObject
-
x:DrawObject的左边界
-
y:DrawObject的顶部
-
width:DrawObject的宽度
-
height:DrawObject的高度
如果您正在将Excel文件呈现为PDF,则可以结合PdfSaveOptions.DrawObjectEventHandler使用DrawObjectEventHandler类。同样,如果您将Excel文件呈现为图像,则可以结合ImageOrPrintOptions.DrawObjectEventHandler使用DrawObjectEventHandler类。
在使用DrawObjectEventHandler类呈现PDF时获取绘图对象和边界
请参考以下示例代码。它加载了示例Excel文件(64716843.xlsx)并将其另存为输出PDF(64716842.pdf)。在生成PDF时,它利用了PdfSaveOptions.DrawObjectEventHandler属性并捕获了现有单元格和对象(例如图像等)的DrawObject和边界。如果drawObject类型为Cell,则打印其边界和StringValue。如果drawObject类型为Image,则打印其边界和形状名称。请参见下面提供的示例代码的控制台输出以获取更多帮助。
示例代码
// 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
\----------------------