Ottieni DrawObject e Bound durante la resa in PDF utilizzando la classe DrawObjectEventHandler
Possibili Scenari di Utilizzo
Aspose.Cells fornisce una classe astratta DrawObjectEventHandler che ha un metodo Draw(). L’utente può implementare DrawObjectEventHandler e utilizzare il metodo Draw() per ottenere il DrawObject e Bound mentre si renderizza Excel in PDF o immagine. Ecco una breve descrizione dei parametri del metodo Draw().
-
drawObject: DrawObject verrà inizializzato e restituito durante la resa
-
x: Sinistra di DrawObject
-
y: Alto di DrawObject
-
larghezza: larghezza di DrawObject
-
altezza: altezza di DrawObject
Se si sta rendendo il file Excel in PDF, è possibile utilizzare la classe DrawObjectEventHandler con PdfSaveOptions.DrawObjectEventHandler. Allo stesso modo, se si sta rendendo il file Excel in immagine, è possibile utilizzare la classe DrawObjectEventHandler con ImageOrPrintOptions.DrawObjectEventHandler.
Ottieni DrawObject e Bound durante il rendering in Pdf usando la classe DrawObjectEventHandler
Si prega di vedere il seguente codice di esempio. Carica il file Excel di esempio e lo salva come PDF di output. Durante la conversione in PDF, utilizza la proprietà PdfSaveOptions.DrawObjectEventHandler e cattura la DrawObject e il Bound delle celle esistenti e degli oggetti come le immagini, ecc. Se il tipo DrawObject è Cell, stampa il suo Bound e StringValue. E se il tipo DrawObject è Immagine, stampa il suo Bound e il nome della forma. Si prega di vedere l’output della console del codice di esempio qui sotto per ulteriori informazioni.
Codice di Esempio
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Aspose.Cells.Rendering; | |
namespace Aspose.Cells.Examples.CSharp.Rendering | |
{ | |
class GetDrawObjectAndBoundUsingDrawObjectEventHandler | |
{ | |
//Implement the concrete class of DrawObjectEventHandler | |
class clsDrawObjectEventHandler : DrawObjectEventHandler | |
{ | |
public override void Draw(DrawObject drawObject, float x, float y, float width, float height) | |
{ | |
Console.WriteLine(""); | |
//Print the coordinates and the value of Cell object | |
if (drawObject.Type == DrawObjectEnum.Cell) | |
{ | |
Console.WriteLine("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Cell Value]: " + drawObject.Cell.StringValue); | |
} | |
//Print the coordinates and the shape name of Image object | |
if (drawObject.Type == DrawObjectEnum.Image) | |
{ | |
Console.WriteLine("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Shape Name]: " + drawObject.Shape.Name); | |
} | |
Console.WriteLine("----------------------"); | |
} | |
} | |
public static void Run() | |
{ | |
//Load sample Excel file | |
Workbook wb = new Workbook("sampleGetDrawObjectAndBoundUsingDrawObjectEventHandler.xlsx"); | |
//Specify Pdf save options | |
PdfSaveOptions opts = new PdfSaveOptions(); | |
//Assign the instance of DrawObjectEventHandler class | |
opts.DrawObjectEventHandler = new clsDrawObjectEventHandler(); | |
//Save to Pdf format with Pdf save options | |
wb.Save("outputGetDrawObjectAndBoundUsingDrawObjectEventHandler.pdf", opts); | |
} | |
} | |
} |
Output della console
[X]: 153.6035 [Y]: 82.94118 [Width]: 103.2035 [Height]: 14.47059 [Cell Value]: This is sample text.
----------------------
[X]: 267.6917 [Y]: 153.4853 [Width]: 160.4491 [Height]: 128.0647 [Shape Name]: Sun
----------------------