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 DrawObject e Bound durante la resa di Excel in PDF o Immagine. Ecco una breve descrizione dei parametri del metodo draw().
-
drawObject: DrawObject sarà inizializzato e restituito durante la resa
-
x: Sinistra di DrawObject
-
y: Alto di DrawObject
-
larghezza: larghezza di DrawObject
-
altezza: altezza di DrawObject
Se stai rendendo un file Excel in PDF, allora puoi utilizzare la classe DrawObjectEventHandler con PdfSaveOptions.DrawObjectEventHandler. Allo stesso modo, se stai rendendo un file Excel in un’immagine, puoi utilizzare la classe DrawObjectEventHandler con ImageOrPrintOptions.DrawObjectEventHandler.
Ottieni DrawObject e Bound durante il rendering in Pdf usando la classe DrawObjectEventHandler
Si prega di consultare il seguente codice di esempio. Carica il file Excel di esempio e lo salva come file PDF di output. Durante il rendering in PDF, utilizza la proprietà PdfSaveOptions.DrawObjectEventHandler e cattura il DrawObject e Bound delle celle esistenti e degli oggetti come immagini ecc. Se il tipo di drawObject è Cell, stampa il suo Bound e il suo StringValue. E se il tipo di drawObject è Image, stampa il suo Bound e il Nome della Forma. Si prega di vedere l’output della console del codice di esempio qui sotto per ulteriore assistenza.
Codice di Esempio
// 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."); | |
} | |
} |
Output della 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
\----------------------