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
| public class PrintingClass:IDisposable
{
static private int m_currentPageIndex;
static private IList<Stream> m_streams;
static Parametres_Imprimante _Parametres_Imprimante =null;
//public DataTable LoadSalesData()
//{
// // Création d' un nouveau DataSet et lire le fichier de données de vente
// // data.xml dans la première DataTable.
// DataSet dataSet = new DataSet();
// dataSet.ReadXml(@"..\..\data.xml");
// return dataSet.Tables[0];
//}
// Routine pour fournir le rapport rendu, afin de
// enregistrer une image pour chaque page du rapport.
//On définit une variable log static qui référence l'instance du logger nommé (nom de la classe principale)
static log4net.ILog log = LogManager.GetLogger(typeof(PrintingClass)); //
protected PrintingClass()
{
//On charge la configuration définie dans le fichier de configuration
log4net.Config.XmlConfigurator.Configure(); //mis dans le c onstructeur pour que xa fonctionne (kamenan 04/05/2016)
log4net.GlobalContext.Properties["versioning"] = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
}
public static Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
// Exporter le rapport donné à titre d'EMF (Enhanced Metafile) fichier.
public static void Export(LocalReport report)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
try
{
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
catch (Exception ex)
{
//****gestion des messages logs (kamenan 04/05/2016)
log.Error("Error: Impossible d'exporter le report sous forme d'image.");
throw new Exception("Error:Impossible d'exporter le report sous forme d'image",ex);
}
}
// Handler pour PrintPageEvents
public static void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
// Ajuster zone rectangulaire avec des marges de l' imprimante.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Dessine un fond blanc pour le rapport
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Dessine le contenu du rapport
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Préparez-vous à la page suivante. Assurez-vous que nous avons pas frapper la fin.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
public static void Print()
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
//****gestion des messages logs (kamenan 04/05/2016)
log.Error("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
//****gestion des messages logs (kamenan 04/05/2016)
log.Error("Error: cannot find the default printer.");
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
PrinterSettings settings = new PrinterSettings();
var imprimanteName = settings.PrinterName;
printDoc.PrinterSettings.PrinterName = NomImprimante();// imprimanteName;
try
{
printDoc.Print();
}
catch (Exception ex)
{
//****gestion des messages logs (kamenan 04/05/2016)
log.Error("Error: Impossible d'imprimer.",ex);
throw new Exception("Error:Impossible d'imprimer.");
}
}
}
// Création d' un rapport local pour Report.rdlc, charger les données,
// exporter le rapport dans un fichier .emf, et l' imprimer.
public static void Run()
{
LocalReport report = new LocalReport();
report.ReportPath = @"..\..\Report.rdlc";
////report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
Export(report);
Print();
}
public static void RunExport(LocalReport report, ReportDataSource datasource)
{
//LocalReport report = new LocalReport();
//report.ReportPath = @"..\..\Report.rdlc";
////report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
Export(report);
Print();
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
public static void Main(string[] args)
{
using (PrintingClass demo = new PrintingClass())
{
//demo.
PrintingClass.Run();
}
}
} |
Partager