Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

winforms reportviewer.print report

this.reportViewer1.LocalReport.Print();
Comment

winforms reportviewer.print report

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;

public static class LocalReportExtensions
{
    public static void Print(this LocalReport report)
    {
        var pageSettings = new PageSettings();
        pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
        pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
        pageSettings.Margins = report.GetDefaultPageSettings().Margins;
        Print(report, pageSettings);
    }

    public static void Print(this LocalReport report, PageSettings pageSettings)
    {
        string deviceInfo =
            $@"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
                <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
                <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
                <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
                <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
                <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
            </DeviceInfo>";

        Warning[] warnings;
        var streams = new List<Stream>();
        var currentPageIndex = 0;

        report.Render("Image", deviceInfo, 
            (name, fileNameExtension, encoding, mimeType, willSeek) => 
            {
                var stream = new MemoryStream();
                streams.Add(stream);
                return stream;
            }, out warnings);

        foreach (Stream stream in streams)
            stream.Position = 0;

        if (streams == null || streams.Count == 0)
            throw new Exception("Error: no stream to print.");

        var printDocument = new PrintDocument();
        printDocument.DefaultPageSettings = pageSettings;
        if (!printDocument.PrinterSettings.IsValid)
            throw new Exception("Error: cannot find the default printer.");
        else
        {
            printDocument.PrintPage += (sender, e) =>
            {
                Metafile pageImage = new Metafile(streams[currentPageIndex]);
                Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);
                e.Graphics.DrawImage(pageImage, adjustedRect);
                currentPageIndex++;
                e.HasMorePages = (currentPageIndex < streams.Count);
                e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
            };
            printDocument.EndPrint += (Sender, e) =>
            {
                if (streams != null)
                {
                    foreach (Stream stream in streams)
                        stream.Close();
                    streams = null;
                }
            };
            printDocument.Print();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to clone something as a parent unity 
Csharp :: unity check if object is being rendered 
Csharp :: DateTime2 error in EF Blazor Web Application 
Csharp :: dictionary and generic class c# 
Csharp :: system.text.json ways to go about getting to the data how to get the data text.json you should use JsonDocument when 
Csharp :: c# get first and last day of current month 
Csharp :: how to collect input from a user in discord bot c# 
Csharp :: check which activity in focus in android 
Csharp :: c# ef dynamic ApplyConfiguration 
Csharp :: cmd.executenonquery() error in c# 
Csharp :: syncfusion worksheet get last row with value 
Csharp :: c sharp switch forms 
Csharp :: how to start commvault services on linux 
Csharp :: .net core executenonqueryasync transaction 
Csharp :: how to add serilog to your asp.net project 
Csharp :: php check syntax error folder 
Csharp :: Get dwgexport setting reivit api 
Csharp :: notification platform not available c# 
Csharp :: C# Func Delegate 
Csharp :: beard styles without mustache Intitle:work with me 
Csharp :: tempdata serializer cannot erorr 
Csharp :: c# servercertificatevalidationcallback 
Csharp :: read dxf file c# 
Csharp :: mouse position to canvas transform 
Csharp :: dotnet core vue in subdirectory 
Csharp :: populate toolstripitems to combobox 
Csharp :: linq dynamic order by descending 
Csharp :: System.InvalidOperationException: No owin.Environment item was found in the context. 
Csharp :: LAST ELEMT OF ARRAY 
Csharp :: C#$ 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =