Hämta DrawObject och Bound vid rendering till PDF med hjälp av DrawObjectEventHandler klassen
Möjliga användningsscenario
Aspose.Cells tillhandahåller en abstrakt klass DrawObjectEventHandler som har en Draw()-metod. Användaren kan implementera DrawObjectEventHandler och använda Draw()-metoden för att få DrawObject och Bound vid rendering av Excel till PDF eller bild. Här är en kort beskrivning av parametrarna till metoden Draw().
-
drawObject: DrawObject kommer att initialiseras och returneras vid rendering
-
x: Vänster om DrawObject
-
y: Topp om DrawObject
-
bredd: Bredd av DrawObject
-
höjd: Höjd av DrawObject
Om du renderar Excel-fil till PDF kan du använda klassen DrawObjectEventHandler med PdfSaveOptions.DrawObjectEventHandler. På samma sätt, om du renderar Excel-fil till bild, kan du använda klassen DrawObjectEventHandler med ImageOrPrintOptions.DrawObjectEventHandler.
Få DrawObject och Bound medan du renderar till Pdf med hjälp av DrawObjectEventHandler-klassen
Vänligen se följande exempelkod. Den laddar prov Excel-filen och sparar den som utdata-PDF. Vid rendering till PDF använder den egenskapen PdfSaveOptions.DrawObjectEventHandler och fångar DrawObject och Bound av befintliga celler och objekt t.ex. bilder osv. Om DrawObject-typen är Cell skriver den ut dess Bound och StringValue. Och om DrawObject-typen är Bild skriver den ut dess Bound och Shape Name. Se konsol-utdata av exempelkoden nedan för mer hjälp.
Exempelkod
// 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); | |
} | |
} | |
} |
Konsoloutput
[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
----------------------