1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Drawing;
using System.Globalization;
namespace ReportPrint
{
/// <summary>
/// Cette classe imprime toutes les pages contenues dans le Report.
/// Les pages sont mises en forme en fonction de leur PageSettings.
/// Pour initialiser l'instance, faite appel à la méthode Print(Report).
/// </summary>
class CPrintReport
{
#region Variables
/*===============================================================
Variables servant à l'impression
===============================================================*/
private int _CurrentPageIndex;
private IList<Stream> _ListStreams;
private PageSettings _PageSettings;
private LocalReport _Report;
#endregion
#region Constructeur et destructeur
/*##############################################################################
# Constructeur
##############################################################################*/
/// <summary>
/// Constructeur de la classe
/// </summary>
/// <param name="_Report">Le Report que l'on souhaite utilisé</param>
public CPrintReport(LocalReport _Report)
{
this._Report = _Report;
}
/*##############################################################################
# Destructeur de la classe
##############################################################################*/
public void Dispose()
{
/*===============================================================
On libère les ressources des flux
===============================================================*/
if (_ListStreams != null)
{
foreach (Stream stream in _ListStreams)
stream.Close();
_ListStreams = null;
}
}
#endregion
#region Méthodes pour l'impression
/// <summary>
/// Impression du Report
/// </summary>
public void Print()
{
/*===============================================================
Mise en forme du Report
===============================================================*/
Export();
/*===============================================================
Test si le flux servant à l'impression existe
===============================================================*/
if (_ListStreams == null || _ListStreams.Count == 0)
throw new Exception("Erreur: Pas de flux pour l'impression.");
/*===============================================================
Récupération de l'imprimante par défaut (impression si l'imprimante existe)
===============================================================*/
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
throw new Exception("Erreur: Impossible de trouver l'imprimante par défaut.");
else
{
_CurrentPageIndex = 0;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
}
/// <summary>
/// Impression du Report
/// </summary>
/// <param name="Settings">Imprimante sur laquelle sera imprimée le report</param>
public void Print(PrinterSettings Settings)
{
/*===============================================================
Mise en forme du Report
===============================================================*/
Export();
/*===============================================================
Test si le flux servant à l'impression existe
===============================================================*/
if (_ListStreams == null || _ListStreams.Count == 0)
throw new Exception("Erreur: Pas de flux pour l'impression.");
/*===============================================================
Récupération de l'imprimante
===============================================================*/
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings = Settings;
if (!printDoc.PrinterSettings.IsValid)
throw new Exception("Erreur: Impossible de trouver l'imprimante.");
else
{
_CurrentPageIndex = 0;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
}
/*##############################################################################
# Création du flux d'image pour chaque page
##############################################################################*/
/// <summary>
/// Création du flux pour chaque page du Report
/// </summary>
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
/*===============================================================
Ajout du flux de la page dans la liste
===============================================================*/
Stream stream = new MemoryStream();
_ListStreams.Add(stream);
return stream;
}
/*##############################################################################
# Mise en forme du Report sous le format EMF (Enhanced Metafile)
##############################################################################*/
/// <summary>
/// Mise en forme du Report sous le format EMF (Enhanced Metafile)
/// </summary>
/// <param name="_Report">Le Report qui sera mis en forme et imprimé</param>
private void Export()
{
/*===============================================================
Récupération de la mise en page du Report
===============================================================*/
ReportPageSettings _ReportPageSettings = _Report.GetDefaultPageSettings();
_PageSettings = new PageSettings();
_PageSettings.PaperSize = _ReportPageSettings.PaperSize;
_PageSettings.Margins = _ReportPageSettings.Margins;
/*===============================================================
Mise en page du Report (Taille et marge)
===============================================================*/
string deviceInfo = string.Format(CultureInfo.InvariantCulture, @"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{0}</PageWidth>
<PageHeight>{1}</PageHeight>
<MarginTop>{2}</MarginTop>
<MarginLeft>{3}</MarginLeft>
<MarginRight>{4}</MarginRight>
<MarginBottom>{5}</MarginBottom>
</DeviceInfo>",
ToInches(_PageSettings.PaperSize.Height),
ToInches(_PageSettings.PaperSize.Width),
ToInches(_PageSettings.Margins.Top),
ToInches(_PageSettings.Margins.Left),
ToInches(_PageSettings.Margins.Right),
ToInches(_PageSettings.Margins.Bottom));
/*===============================================================
Récupération des flux et des warnings
===============================================================*/
Warning[] warnings;
_ListStreams = new List<Stream>();
/*===============================================================
Mise en forme du Report
===============================================================*/
_Report.Render("Image", deviceInfo, CreateStream, out warnings);
/*===============================================================
Initialisation des flux
===============================================================*/
foreach (Stream stream in _ListStreams)
stream.Position = 0;
}
/*##############################################################################
# Impression de chaque page du Report (Evénement PrintPageEvents)
##############################################################################*/
/// <summary>
/// Evénement PrintPageEvents
/// </summary>
private void PrintPage(object sender, PrintPageEventArgs ev)
{
/*===============================================================
Création de la séquence d'opération graphique
===============================================================*/
Metafile pageImage = new Metafile(_ListStreams[_CurrentPageIndex]);
/*===============================================================
On ajuste la zone d'impression
===============================================================*/
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
/*===============================================================
Ajout d'un fond d'écran blanc
===============================================================*/
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
/*===============================================================
Dessine le contenu du Report
===============================================================*/
ev.Graphics.DrawImage(pageImage, adjustedRect);
/*===============================================================
Préparation de la prochaine page à imprimer
===============================================================*/
_CurrentPageIndex++;
ev.HasMorePages = (_CurrentPageIndex < _ListStreams.Count);
}
/*##############################################################################
# Conversions en Inches
##############################################################################*/
public static string ToInches(int hundrethsOfInch)
{
double inches = Math.Round(hundrethsOfInch / 100.0, 2);
return inches.ToString(CultureInfo.InvariantCulture) + "in";
}
#endregion
}
} |
Partager