RenderToScale

Document.RenderToScale method

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

public SizeF RenderToScale(int pageIndex, Graphics graphics, float x, float y, float scale)
Parameter Type Description
pageIndex Int32 The 0-based page index.
graphics Graphics The object where to render to.
x Single The X coordinate (in world units) of the top left corner of the rendered page.
y Single The Y coordinate (in world units) of the top left corner of the rendered page.
scale Single The scale for rendering the page (1.0 is 100%).

Return Value

The width and height (in world units) of the rendered page.

Examples

Shows how to the individual pages of a document to graphics to create one image with thumbnails of all pages.

Document doc = new Document(MyDir + "Rendering.docx");

// Calculate the number of rows and columns that we will fill with thumbnails.
const int thumbColumns = 2;
int thumbRows = Math.DivRem(doc.PageCount, thumbColumns, out int remainder);

if (remainder > 0)
    thumbRows++;

// Scale the thumbnails relative to the size of the first page.
const float scale = 0.25f;
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;

using (Bitmap img = new Bitmap(imgWidth, imgHeight))
{
    using (Graphics gr = Graphics.FromImage(img))
    {
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        // Fill the background, which is transparent by default, in white.
        gr.FillRectangle(new SolidBrush(Color.White), 0, 0, imgWidth, imgHeight);

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

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

            // Render a page as a thumbnail, and then frame it in a rectangle of the same size.
            SizeF size = doc.RenderToScale(pageIndex, gr, thumbLeft, thumbTop, scale);
            gr.DrawRectangle(Pens.Black, thumbLeft, thumbTop, size.Width, size.Height);
        }

        img.Save(ArtifactsDir + "Rendering.Thumbnails.png");
    }
}

Renders individual pages to graphics to create one image with thumbnails of all pages (.NetStandard 2.0).

Document doc = new Document(MyDir + "Rendering.docx");

// Calculate the number of rows and columns that we will fill with thumbnails.
const int thumbnailColumnsNum = 2;
int thumbRows = Math.DivRem(doc.PageCount, thumbnailColumnsNum, out int remainder);

if (remainder > 0)
    thumbRows++;

// Scale the thumbnails relative to the size of the first page. 
const float scale = 0.25f;
Size thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96);

// Calculate the size of the image that will contain all the thumbnails.
int imgWidth = thumbSize.Width * thumbnailColumnsNum;
int imgHeight = thumbSize.Height * thumbRows;

using (SKBitmap bitmap = new SKBitmap(imgWidth, imgHeight))
{
    using (SKCanvas canvas = new SKCanvas(bitmap))
    {
        // Fill the background, which is transparent by default, in white.
        canvas.Clear(SKColors.White);

        for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
        {
            int rowIdx = Math.DivRem(pageIndex, thumbnailColumnsNum, out int 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);

            // Render a page as a thumbnail, and then frame it in a rectangle of the same size.
            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.CreateThumbnailsNetStandard2.png"))
        {
            bitmap.PeekPixels().Encode(fs, SKEncodedImageFormat.Png, 100);
        }
    }
}

See Also