DocumentRenderToScale Method
Renders a document page into a Graphics object to a specified scale.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public SizeF RenderToScale(
	int pageIndex,
	Graphics graphics,
	float x,
	float y,
	float scale
)

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.
scale
Type: SystemSingle
The scale for rendering the page (1.0 is 100%).

Return Value

Type: SizeF
The width and height (in world units) of the rendered page.
Examples
Renders a page of a Word document into a bitmap using a specified zoom factor.
Document doc = new Document(MyDir + "Rendering.doc");

            PageInfo pageInfo = doc.GetPageInfo(0);

            // Let's say we want the image at 50% zoom.
            const float myScale = 0.50f;

            // Let's say we want the image at this resolution.
            const float myResolution = 200.0f;

            Size pageSize = pageInfo.GetSizeInPixels(myScale, myResolution);
#if NETSTANDARD2_0 || __MOBILE__
            using (SKBitmap bitmap = new SKBitmap(pageSize.Width, pageSize.Height))
            {
                using (SKCanvas canvas = new SKCanvas(bitmap))
                {
                    // Scale to compensate for the larger bitmap
                    canvas.Scale(2);

                    // Fill the page background.
                    // If you need to set color without specific options, you can use "canvas.Clear(SKColors.White);"
                    SKPaint paint = new SKPaint
                    {
                        Color = SKColors.White,
                        IsAntialias = true,
                        FilterQuality = SKFilterQuality.High
                    };

                    canvas.DrawPaint(paint);

                    // Render the page using the zoom.
                    doc.RenderToScale(0, canvas, 0, 0, myScale);
                }

                using (SKFileWStream fs = new SKFileWStream(ArtifactsDir + "Rendering.RenderToScale.png"))
                {
                    bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                }
            }
#else
            using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
            {
                img.SetResolution(myResolution, myResolution);

                using (Graphics gr = Graphics.FromImage(img))
                {
                    // You can apply various settings to the Graphics object.
                    gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    // Fill the page background.
                    gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width, pageSize.Height);

                    // Render the page using the zoom.
                    doc.RenderToScale(0, gr, 0, 0, myScale);
                }

                img.Save(ArtifactsDir + "Rendering.RenderToScale.png");
            }
#endif
Examples
Renders individual pages to graphics to create one image with thumbnails of all pages.
// The user opens or builds a document.
            Document doc = new Document(MyDir + "Rendering.doc");

            // This defines the number of columns to display the thumbnails in.
            const int thumbColumns = 2;

            // Calculate the required number of rows for thumbnails.
            // We can now get the number of pages in the document.
            int remainder;
            int thumbRows = Math.DivRem(doc.PageCount, thumbColumns, out remainder);
            if (remainder > 0)
                thumbRows++;

            // Lets say I want thumbnails to be of this zoom.
            const float scale = 0.25f;

            // For simplicity lets pretend all pages in the document are of the same size, 
            // so we can use the size of the first page to calculate the size of the thumbnail.
            Size thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96);

            // Calculate the size of the image that will contain all the thumbnails.
            int imgWidth = thumbSize.Width * thumbColumns;
            int imgHeight = thumbSize.Height * thumbRows;
#if NETSTANDARD2_0 || __MOBILE__
            using (SKBitmap bitmap = new SKBitmap(imgWidth, imgHeight))
            {
                // The user has to provides a Graphics object to draw on.
                // The Graphics object can be created from a bitmap, from a metafile, printer or window.
                using (SKCanvas canvas = new SKCanvas(bitmap))
                {
                    // Fill the "paper" with white, otherwise it will be transparent.
                    canvas.Clear(SKColors.White);

                    for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
                    {
                        int columnIdx;
                        int rowIdx = Math.DivRem(pageIndex, thumbColumns, out columnIdx);

                        // Specify where we want the thumbnail to appear.
                        float thumbLeft = columnIdx * thumbSize.Width;
                        float thumbTop = rowIdx * thumbSize.Height;

                        SizeF size = doc.RenderToScale(pageIndex, canvas, thumbLeft, thumbTop, scale);

                        // Draw the page rectangle.
                        SKRect rect = new SKRect(0, 0, size.Width, size.Height);
                        rect.Offset(thumbLeft, thumbTop);
                        canvas.DrawRect(rect, new SKPaint
                        {
                            Color = SKColors.Black,
                            Style = SKPaintStyle.Stroke
                        });
                    }

                    using (SKFileWStream fs = new SKFileWStream(ArtifactsDir + "Rendering.Thumbnails.png"))
                    {
                        bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
                    }
                }
            }
#else
            using (Bitmap img = new Bitmap(imgWidth, imgHeight))
            {
                // The user has to provides a Graphics object to draw on.
                // The Graphics object can be created from a bitmap, from a metafile, printer or window.
                using (Graphics gr = Graphics.FromImage(img))
                {
                    gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    // Fill the "paper" with white, otherwise it will be transparent.
                    gr.FillRectangle(new SolidBrush(Color.White), 0, 0, imgWidth, imgHeight);

                    for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
                    {
                        int columnIdx;
                        int rowIdx = Math.DivRem(pageIndex, thumbColumns, out columnIdx);

                        // Specify where we want the thumbnail to appear.
                        float thumbLeft = columnIdx * thumbSize.Width;
                        float thumbTop = rowIdx * thumbSize.Height;

                        SizeF size = doc.RenderToScale(pageIndex, gr, thumbLeft, thumbTop, scale);

                        // Draw the page rectangle.
                        gr.DrawRectangle(Pens.Black, thumbLeft, thumbTop, size.Width, size.Height);
                    }

                    img.Save(ArtifactsDir + "Rendering.Thumbnails.png");
                }
            }
#endif
See Also