DocumentRenderToSize Method

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public float RenderToSize(
	int pageIndex,
	Graphics graphics,
	float x,
	float y,
	float width,
	float height
)

Parameters

pageIndex
Type: SystemInt32
The 0-based page index.
graphics
Type: System.DrawingGraphics
The object where to render to.
x
Type: SystemSingle
The X coordinate (in world units) of the top left corner of the rendered page.
y
Type: SystemSingle
The Y coordinate (in world units) of the top left corner of the rendered page.
width
Type: SystemSingle
The maximum width (in world units) that can be occupied by the rendered page.
height
Type: SystemSingle
The maximum height (in world units) that can be occupied by the rendered page.

Return Value

Type: Single
The scale that was automatically calculated for the rendered page to fit the specified size.
Examples
Render to a bitmap at a specified location and size.
Document doc = new Document(MyDir + "Rendering.docx");

using (Bitmap bmp = new Bitmap(700, 700))
{
    // User has some sort of a Graphics object. In this case created from a bitmap
    using (Graphics gr = Graphics.FromImage(bmp))
    {
        // The user can specify any options on the Graphics object including
        // transform, anti-aliasing, page units, etc.
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        // Let's say we want to fit the page into a 3" x 3" square on the screen so use inches as units
        gr.PageUnit = GraphicsUnit.Inch;

        // The output should be offset 0.5" from the edge and rotated
        gr.TranslateTransform(0.5f, 0.5f);
        gr.RotateTransform(10);

        // This is our test rectangle
        gr.DrawRectangle(new Pen(Color.Black, 3f / 72f), 0f, 0f, 3f, 3f);

        // User specifies (in world coordinates) where on the Graphics to render and what size
        float returnedScale = doc.RenderToSize(0, gr, 0f, 0f, 3f, 3f);

        // This is the calculated scale factor to fit 297mm into 3"
        Console.WriteLine("The image was rendered at {0:P0} zoom.", returnedScale);

        // One more example, this time in millimeters
        gr.PageUnit = GraphicsUnit.Millimeter;

        gr.ResetTransform();

        // Move the origin 10mm 
        gr.TranslateTransform(10, 10);

        // Apply both scale transform and page scale for fun
        gr.ScaleTransform(0.5f, 0.5f);
        gr.PageScale = 2f;

        // This is our test rectangle
        gr.DrawRectangle(new Pen(Color.Black, 1), 90, 10, 50, 100);

        // User specifies (in world coordinates) where on the Graphics to render and what size
        doc.RenderToSize(1, gr, 90, 10, 50, 100);

        bmp.Save(ArtifactsDir + "Rendering.RenderToSize.png");
    }
}
Examples
Render to a bitmap at a specified location and size (.NetStandard 2.0).
Document doc = new Document(MyDir + "Rendering.docx");

using (SKBitmap bitmap = new SKBitmap(700, 700))
{
    // User has some sort of a Graphics object. In this case created from a bitmap
    using (SKCanvas canvas = new SKCanvas(bitmap))
    {
        // Apply scale transform
        canvas.Scale(70);

        // The output should be offset 0.5" from the edge and rotated
        canvas.Translate(0.5f, 0.5f);
        canvas.RotateDegrees(10);

        // This is our test rectangle
        SKRect rect = new SKRect(0f, 0f, 3f, 3f);
        canvas.DrawRect(rect, new SKPaint
        {
            Color = SKColors.Black,
            Style = SKPaintStyle.Stroke,
            StrokeWidth = 3f / 72f
        });

        // User specifies (in world coordinates) where on the Graphics to render and what size
        float returnedScale = doc.RenderToSize(0, canvas, 0f, 0f, 3f, 3f);

        Console.WriteLine("The image was rendered at {0:P0} zoom.", returnedScale);

        // One more example, this time in millimeters
        canvas.ResetMatrix();

        // Apply scale transform
        canvas.Scale(5);

        // Move the origin 10mm 
        canvas.Translate(10, 10);

        // This is our test rectangle
        rect = new SKRect(0, 0, 50, 100);
        rect.Offset(90, 10);
        canvas.DrawRect(rect, new SKPaint
        {
            Color = SKColors.Black,
            Style = SKPaintStyle.Stroke,
            StrokeWidth = 1
        });

        // User specifies (in world coordinates) where on the Graphics to render and what size
        doc.RenderToSize(0, canvas, 90, 10, 50, 100);

        using (SKFileWStream fs = new SKFileWStream(ArtifactsDir + "Rendering.RenderToSizeNetStandard2.png"))
        {
            bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
        }
    }
}
See Also