Obtenez DrawObject et Bound lors du rendu à PDF à l'aide de la classe DrawObjectEventHandler

Scénarios d’utilisation possibles

Aspose.Cells fournit une classe abstraiteDrawObjectEventHandlerDrawObjectEventHandler qui a undessiner() méthode. L’utilisateur peut implémenterDrawObjectEventHandlerDrawObjectEventHandleret utiliser ledessiner() méthode pour obtenir leDessinerObjetetBondirlors du rendu d’Excel en PDF ou Image. Voici une brève description des paramètres dedessiner() méthode.

Si vous rendez le fichier Excel au PDF, vous pouvez utiliserDrawObjectEventHandlerDrawObjectEventHandlerclasse avecPdfSaveOptions.DrawObjectEventHandler. De même, si vous convertissez un fichier Excel en image, vous pouvez utiliserDrawObjectEventHandlerDrawObjectEventHandlerclasse avecImageOrPrintOptions.DrawObjectEventHandler.

Obtenez DrawObject et Bound lors du rendu au format Pdf à l’aide de la classe DrawObjectEventHandler

Veuillez consulter l’exemple de code suivant. Il charge leexemple de fichier Excelet l’enregistre soussortie PDF. Lors du rendu en PDF, il utilisePdfSaveOptions.DrawObjectEventHandlerpropriété et capture leDessinerObjet etBondirde cellules et d’objets existants, par exemple des images, etc. Si le type drawObject est Cell, il imprime ses Bound et StringValue. Et si le type drawObject est Image, il imprime son Bound et Shape Name. Veuillez consulter la sortie de la console de l’exemple de code ci-dessous pour plus d’aide.

Exemple de code

// 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.");
}
}

Sortie console

[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

\----------------------